TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e')

前端 未结 4 3482
Happy的楠姐
Happy的楠姐 2020-12-29 04:36

I have the following array:

X = np.array([image_array_to_vector1,image_array_to_vector2,image_array_to_vector3,image_array_to_vector4])

A p

4条回答
  •  滥情空心
    2020-12-29 05:01

    In essence savetxt is doing:

    for row in your_array:
        print(fmt % tuple(row))
    

    where fmt is constructed your fmt parameter (or the default one) and the number of columns, and the delimiter.

    You have a 1d array of objects (arrays). So the write/print will be

     print(fmt % tuple(element))
    

    %s is the only format that can handle an array (or other general object).

    savetxt is meant to be used with 2d numeric arrays, the kind of thing that will produce need csv columns. Trying to use it on other things like this object array is going to give you headaches.

    In [2]: A = np.empty((3,),dtype=object)
    In [3]: A[:] = [np.arange(3),np.arange(1,4), np.arange(-3,0)]
    In [4]: A
    Out[4]: array([array([0, 1, 2]), array([1, 2, 3]), array([-3, -2, -1])], dtype=object)
    
    In [6]: np.savetxt('test',A,fmt='%s')
    In [7]: cat test
    [0 1 2]
    [1 2 3]
    [-3 -2 -1]
    

    Iterating on a 1d array it must be skipping the tuple. In any case the best you can do is a %s format. Otherwise write your own custom file writer. savetxt isn't anything special or powerful.

    In [9]: for row in A:
       ...:     print('%s'%row)  
    [0 1 2]
    [1 2 3]
    [-3 -2 -1]
    

提交回复
热议问题