Welcome!

Saving with numpy

I had a problem, for which I needed to google a bit until I've solved it. I had an array of arrays with different datatypes:

When I saved it with the numpy.save()  and open it with numpy.load() the datatypes were not the same anymore, all was converted to a string with length 2.

arr = [['Peter', 63, True], ['Marie', 52, False]]
numpy.save('persons', arr)
print numpy.load('persons.npy')
>> [['Pe', '63', 'Tr'], ['Ma', '52', 'Fa']]

The solution is to transform the pyhton array to a numpy array first while setting the dtype=np.dtype(object)

arr = [['Peter', 63, True], ['Marie', 52, False]]
numpy.save('persons', np.array(arr, dtype=numpy.dtype(object))
print numpy.load('persons.npy')
>> [['Peter', 63, True], ['Marie', 52, False]]

<< Zurück zur vorherigen Seite