繁体   English   中英

将二维 numpy 数组附加到 txt 文件

[英]Appending 2-D numpy array to txt file

我的程序在执行时打印一个变量predictions ,它是一个二维 numpy 数组。 我正在尝试将输出写入文本文件。 我通过使用predictions.shape确保形状是 2D 并使用type(predictions)检查类型为<class numpy.ndarray>

我收到的错误是:

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    predictor()
  File "/Users/owner/Desktop/algo/predict.py", line 146, in predictor
    predictions.savetxt("predictions.txt", "a", delimiter=',', fmt='%d',  header='', footer='')
AttributeError: 'numpy.ndarray' object has no attribute 'savetxt'

这是在我调用 python 模块中的函数后返回的。

我确定我正确导入了 numpy,我使用的savetxt()函数不正确吗?

我尝试在 Stack 上搜索但找不到任何东西,谢谢。

是的,savetext 方法不是数组上的方法,而是 numpy 包中的方法。 您可以通过调用方法并传入数组来使用它:

numpy.savetxt("predictions.txt", predictions, delimiter=',', fmt='%d',  header='', footer='')

np.savetext(..)如果您确实import numpy as np

暂无
暂无

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

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