TypeError: mismatch between dtype ('float64') array and format specifier

I have a numpy array with a size of 1000 * 30 * 150. I am trying to save it as a txt file. So far i tried this

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')

both methods give me an error

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')
+4
source share
4 answers

You did not indicate what the purpose of writing a 3-dimensional array to a text file is, do you read it in the future and what format are you looking for, but this is one of the possibilities:

import json
print(json.dumps(mydata, default=lambda x: list(x), indent=4))

If you specify the goal, people will be able to offer more suitable solutions.

+2
source

the problem is that your array is 3-dimensional and cannot be saved in two-dimensional format. Or change it, so it's 2d,

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

python,

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

mydata. , dtype shape.

%.5f, 2d .

savetxt, :

 for row in arr:
   print(format % tuple(row))

format fmt . , , format - '%.5f,%.5f,%.5f,%.5f,%.5f,%....

tuple , 1d- row , format%().

, .


edit - , 1000 * 30 * 150. 1000 , 30 format. (30,150).

open row ? Py3 '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 format`, , .

savetxt 2d . 3d . , csv . , .


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]

-

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

With additional iteration; can add an empty string between blocks

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
+1
source

An alternative to np.savetxt () could be to use the csv module:

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

I ran into a similar TypeError problem with numpy, but the CSV method works fine.

0
source

Source: https://habr.com/ru/post/1683296/


All Articles