ai didi

python - 类型错误 : Mismatch between array dtype ('float64' ) and format specifier

转载 作者:太空狗 更新时间:2023-10-30 00:53:37 24 4
gpt4 key购买 nike

我有一个尺寸为 1000*30*150 的 numpy 数组。我正在尝试将其保存为 txt 文件。到目前为止我已经试过了

np.savetxt("test.txt", mydata, fmt='%.5f', delimiter=",")
#and
with open('test.txt', 'w') as f:
for row in mydata:
np.savetxt(f, row, delimiter=',', fmt='%.5f')

两种方法都报错

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/numpy/lib/npyio.py", line 1254, in savetxt
fh.write(asbytes(format % tuple(row) + newline))
TypeError: only length-1 arrays can be converted to Python scalars

During handling of the above exception, another exception occurred:

Traceback (most recent call last):


np.savetxt("test.txt", train, fmt='%.5f', delimiter=",")
File "/usr/local/lib/python3.5/dist-packages/numpy/lib/npyio.py", line 1258, in savetxt
% (str(X.dtype), format))
TypeError: Mismatch between array dtype ('float64') and format specifier ('%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f,%.5f')

最佳答案

问题是您的数组是 3 维的,无法以二维格式保存。要么 reshape 它,使其成为 2d:

mydata = mydata.reshape(mydata.shape[0],mydata.shape[1]*mydata.shape[2])
np.savetxt('text.txt',mydata,fmt='%.5f',delimiter=',')

或者如果您不需要将其作为文本文件读取并且只想稍后在 python 中重新加载它,请使用:

np.save('text.npy',mydata)

关于python - 类型错误 : Mismatch between array dtype ('float64' ) and format specifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45580330/

24 4 0
文章推荐: c# - Ajax - 'Origin localhost is not allowed by Access-Control-Allow-Origin'
文章推荐: c# - 记录 Windows 服务
文章推荐: c# - 删除除一个标签外的所有 html 标签
文章推荐: python - Pandas 数据帧 : How to select rows on multiple conditions?
太空狗
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com