![](/img/trans.png)
[英]TypeError: Mismatch between array dtype and format specifier. How to save a dataframe with different value types as a txt file?
[英]TypeError: Mismatch between array dtype ('float64') and format specifier
我有一個尺寸為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維的,不能以二維格式保存。 要么重塑它,所以它是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)
您沒有提到將三維數組寫入文本文件的目的是什么,將來是否會將其讀回來,以及您要查找的格式,但這是一種可能性:
import json
print(json.dumps(mydata, default=lambda x: list(x), indent=4))
如果您澄清目的,人們將能夠提出更合適的解決方案。
告訴我們mydata
。 特別是它的dtype
和shape
。
要以%.5f
格式保存,它需要是一個二維數組。
savetxt
大致:
for row in arr:
print(format % tuple(row))
其中format
是根據您的fmt
參數構造的,以及數組中的數字列。 看起來您的數組有大量列,因此format
為'%.5f,%.5f,%.5f,%.5f,%.5f,%...
string。
需要使用tuple
將該1d數組row
轉換為使用format%()
的元組。
如果數組是更高的維度或對象數組,則會出現問題。
編輯 - 所以你說數組是1000 * 30 * 150。 所以它試圖迭代1000行,30看起來像那個format
的大小。 但它不能應用於(30,150)
陣列。
使用open
和row
迭代,你會得到同樣的錯誤嗎? 在Py3中,您可能需要使用'wb'. Iterating yourself on the first dimension means each
打開'wb'. Iterating yourself on the first dimension means each
'wb'. Iterating yourself on the first dimension means each
savetxt call works with a 30x150 array. It will iterate on the 30, and try to format rows of 150. The would create a larger
call works with a 30x150 array. It will iterate on the 30, and try to format rows of 150. The would create a larger
格式`,但我認為這將運行。
無論如何, savetxt
是為2d數字數組設計的。 3d需要某種軟糖。 請記住, csv
閱讀器也不是為3d陣列設計的。 他們期望具有一致列的行由簡單的分隔符分隔。
In [260]: arr = np.arange(24).reshape(4,3,2)
它可以使用3d - 如果允許使用%s
格式化每個子行:
In [261]: np.savetxt('test',arr, fmt='%s')
In [262]: cat test
[0 1] [2 3] [4 5]
[6 7] [8 9] [10 11]
[12 13] [14 15] [16 17]
[18 19] [20 21] [22 23]
3d數字格式 - 錯誤
In [263]: np.savetxt('test',arr, fmt='%d')
....
TypeError: Mismatch between array dtype ('int32') and format specifier ('%d %d %d')
重塑3d到2d - 保存工作:
In [264]: np.savetxt('test',arr.reshape(-1,2), fmt='%d')
In [265]: cat test
0 1
2 3
4 5
6 7
8 9
...
22 23
通過額外的迭代; 可以在塊之間添加一個空行
In [267]: with open('test','wb') as f:
...: for row in arr:
...: np.savetxt(f, row, '%d',delimiter=', ')
...:
In [268]: cat test
0, 1
2, 3
4, 5
6, 7
...
22, 23
np.savetxt()的替代方法可能是使用csv模塊:
with open("filename.","w+") as my_csv: # writing the file as my_csv
csvWriter = csv.writer(my_csv,delimiter=',') # using the csv module to write the file
csvWriter.writerows(array_2d) # write every row in the matrix
我遇到了與numpy類似的TypeError問題,但CSV方法似乎工作正常。
如果您希望在mydata[i,:,:]
的軸上以格式化的行和列寫出數據,目的是以更易讀的表格格式生成內容,請參閱以下答案: 如何編寫多維數組到文本文件? 由@JoeKington。 我的代碼在每個切片的行和列中添加了一個循環,因為我找不到我在實現原始代碼時得到的TypeError的任何其他解析:
with open('test.txt', 'w') as outfile:
# Add header giving shape of array
# Any line starting with "#" will be ignored by numpy.loadtxt
outfile.write('# Array shape: {0}\n'.format(x_train.shape))
# Iterating through a ndimensional array produces slices along
# the last axis. This is equivalent to data[i,:,:] in this case
sliceCount = 0
for data_slice in x_train:
# Keep track of the slice numbers
outfile.write('# New slice %d\n'%sliceCount)
# Work through each row and column of the 2d numpy array inside the
# slice, writing each column number to file in format of your choosing
for row in data_slice:
for col in row:
itemStr = "%8.6f,"%col
outfile.write(itemStr)
outfile.write("\n")
sliceCount += 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.