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

前端 未结 4 3481
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

    It's a little difficult to duplicate this without some example data, but here is what I came up with.

    arr = np.array([np.array([1,2,3]), np.array([1,2,3,4])])
    arr
    array([array([1, 2, 3]), array([1, 2, 3, 4])], dtype=object)
    np.savetxt('x.txt', arr)
    TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e')
    

    As pointed out by @Artier there is a little snippet at the end of the accepted answer in Write object array to .txt file that points out you can just save the array as a string with fmt='%s'. Using this format seems to solve the problem for me (again I can't recreate your issue exactly without a sample of data).

    np.savetxt('x.txt', arr, fmt='%s')
    

    I would point out that if you are looking to save disparate arrays and want a single location to keep them savez is very useful.

提交回复
热议问题