简体   繁体   English

保存到 csv - numpy.ndarray' object 没有属性“附加”

[英]save to csv - numpy.ndarray' object has no attribute 'append'

I'm trying to append the filenames of the images during training into a csv file, but getting an error.我正在尝试将训练期间图像的文件名 append 转换为 csv 文件,但出现错误。 I can print the values okay but cannot append to csv file for some reason.我可以打印值,但由于某种原因不能将 append 打印到 csv 文件。 Here is the full code:这是完整的代码:

  File "train_filename.py", line 140, in <module>
    names_preds.append() + '\n'
AttributeError: 'numpy.ndarray' object has no attribute 'append'
        with open('preds_base_model_teste1016.csv','a') as fd:
            #dict_writer = csv.writer(fd)
            #dict_writer.writerow('Target' + '\n')
            #dict_writer.writerow( ','.join(map(str, preds.detach().tolist())) + '\n')
            #dict_writer.writerow('Prediction' + '\n')
            #dict_writer.writerow( ','.join(map(str, targets.detach().tolist())) + '\n')
            #fd.write('Target' + '\n')
            fd.write(','.join(map(str, preds.detach().tolist())) + '\n')
            #fd.write('Prediction' + '\n')
            fd.write( ','.join(map(str, targets.detach().tolist())) + '\n')
            #fd.write('Image_Name' + '\n')
            np.append(paths)
            #fd.write([filename])

Use concatenate instead of append as described in the docs .文档中所述,使用concatenate而不是append

It does look like you are trying to add nothing to the array, however.但是,看起来您确实试图向数组中添加任何内容。

You should use it like this:你应该像这样使用它:

names_preds.concatenate(some_value) where the value inside concatenate is what you want to add to the array. names_preds.concatenate(some_value)其中concatenate中的值是您要添加到数组中的值。

To save a numpy array to a csv file, there is an easier way:要将 numpy 数组保存到 csv 文件,有一种更简单的方法:

numpy.savetxt("foo.csv", names_preds, delimiter=",")

See details in this answer.请参阅答案中的详细信息。

Replace your line:替换你的行:

np.append(paths)

with

with open('preds_base_model_teste1016.csv','a') as fd:
    np.savetxt(fd, names_preds, delimiter=",")

That will append all of the paths to the file you already specified.这将 append 您已经指定的文件的所有路径。

Update:更新:

Per your question in the comment, to put multiple arrays together so you end up with columns, you could try:根据您在评论中的问题,要将多个 arrays 放在一起,以便最终得到列,您可以尝试:

with open('preds_base_model_teste1016.csv','a') as fd:
    np.savetxt(fd, list(zip(a,b,c)), delimiter=",")

where a , b , and c are the arrays you want as columns.其中abc是您想要作为列的 arrays 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM