sdr2002
sdr2002

Reputation: 552

Change dtype of ndarray from object to float?

I found a strange 3D ndarray(see attached file) that does not show the shape of the last dimension and dtype change(asarray nor astype from 'object' to np.float) does not work.

file

It is an ndarray of ('object','object','float64')dtype for each dimension and the actually shape is (2,3,24) but shape is shown as (2,3), which means 24 dimensional array in (2,3) does not count.

When I try .asarray or .astype to make this array as 'dtype=np.float' for every dimension, I got an error ValueError: setting an array element with a sequence.

Unfortunately, none of the methods previously discussed in here do not work for this case. ValueError: setting an array element with a sequence

Numpy ValueError: setting an array element with a sequence. This message may appear without the existing of a sequence?

Error: Setting an array element with a sequence. Python / Numpy

ValueError :Setting an array element with a sequence using numpy

Python Numpy Error: ValueError: setting an array element with a sequence

How can I change the dtype of the array into np.float for all dimension so that I can utilise typical np operations? Thank you.

*I met this problem by extracting some data points from python's collection.deque library. I stored a series of tuples of dtype (ndarray-float, bool), and pick a few as an ndarray via

array = np.asarray(list(itertools.islice())).

I extract the float-side elements by doing state_array = array[:,:,0]. I wanted to make state_array into purely np.float dtype-d ndarray for every dimension but couldn't. What's happening is that this state_array has 'object' as datatype state_array.asarray() nor np.array(state_array) do not work with the error-code below.

ValueError: setting an array element with a sequence.

Upvotes: 0

Views: 1423

Answers (1)

hpaulj
hpaulj

Reputation: 231335

With your download, I can load the array:

In [850]: data =np.load('../Downloads/strange_array.npy',encoding='latin1')
In [851]: data.shape
Out[851]: (2, 3)

the elements are all the same shape and dtype, so they can be joined into a 3d array:

In [852]: [i.shape for i in data.flat]
Out[852]: [(24,), (24,), (24,), (24,), (24,), (24,)]
In [853]: [i.dtype for i in data.flat]
Out[853]: 
[dtype('float64'),
 dtype('float64'),
 dtype('float64'),
 dtype('float64'),
 dtype('float64'),
 dtype('float64')]

that joining is easiest if we flatten out that 2x3 shape:

In [854]: np.stack(data.ravel()).shape
Out[854]: (6, 24)
In [855]: np.stack(data.ravel()).reshape(2,3,24)

Upvotes: 2

Related Questions