【问题标题】:How do I apply a function to every row of a csv file and save the new data into a new file?如何将函数应用于 csv 文件的每一行并将新数据保存到新文件中?
【发布时间】:2022-11-04 17:53:42
【问题描述】:

我有这个 10,000 行的数据集,我正在尝试将卷积核应用于每一行,但我的代码只产生最后一行。

test_data_file = open("mnist_test.csv", 'r')      
test_data_list = test_data_file.readlines()    
test_data_file.close() 

for record in test_data_list:                  # test_data_list is all the values in the test file
        all_values = record.split(',')             # split each record (image) into values seperated by commas
        correct_label = int(all_values[0])         # the first value is the lab
        inputs = (numpy.asfarray(all_values[1:]))    
    
        original = numpy.asfarray(inputs.reshape((28,28)))    # the list is made into an array
        sharpen_kernel = np.array([
                    [0, -1, 0],
                    [-1, 5, -1],
                    [0, -1, 0]])  
    
        matplotlib.rcParams['figure.figsize'] = 20,20      # convolve your image with the kernel
        conv_image = numpy.ones((28,28))
    
    # make a subarray and convolve it with the kernel
        step = 3
        i=0
        while i < 25:
            i+=1
            j = 0
            while j < 25 :
                sub_image = original[i:(i+step),j:(j+step):]    
                sub_image = numpy.reshape(sub_image,(1,(step ** 2)))
                kernel = numpy.reshape(sharpen_kernel, ((step ** 2),1))
                conv_scalar = numpy.dot(sub_image,kernel)
                sharpened[i,j] = conv_scalar
                j+=1
            pass

如何使其产生将新值写入新文件的输出?

【问题讨论】:

  • 你应该开始使用 pandas 库 (pandas.pydata.org)
  • 你想写什么输出?
  • 卷积后具有新值的新 csv 文件。原始 csv 文件包含 10000 个 mnist 测试数据。
  • 这些值在您的代码中的什么位置?

标签: python csv convolution


【解决方案1】:

我建议您使用循环体,并将其移动到函数中

def process(record: str) -> str:
    # your loop's body, assigning your results to output
    return output

with test_data_file = open("mnist_test.csv", 'r'):
    test_data_list = test_data_file.readlines()

with output_file = open("output.csv", 'w'):
    for record in test_data_list:
        output_file.write(process(record))

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 2020-07-22
    相关资源
    最近更新 更多