Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently extract values from nested numpy arrays generated by loadmat function?

Is there a more efficient method in python to extract data from a nested python list such as A = array([[array([[12000000]])]], dtype=object). I have been using A[0][0][0][0], it does not seem to be an efficinet method when you have lots of data like A.

I have also used numpy.squeeeze(array([[array([[12000000]])]], dtype=object)) but this gives me

array(array([[12000000]]), dtype=object)

PS: The nested array was generated by loadmat() function in scipy module to load a .mat file which consists of nested structures.

like image 912
Karma Avatar asked Mar 07 '23 03:03

Karma


2 Answers

Creating such an array is a bit tedious, but loadmat does it to handle the MATLAB cells and 2d matrix:

In [5]: A = np.empty((1,1),object)
In [6]: A[0,0] = np.array([[1.23]])
In [7]: A
Out[7]: array([[array([[ 1.23]])]], dtype=object)
In [8]: A.any()
Out[8]: array([[ 1.23]])
In [9]: A.shape
Out[9]: (1, 1)

squeeze compresses the shape, but does not cross the object boundary

In [10]: np.squeeze(A)
Out[10]: array(array([[ 1.23]]), dtype=object)

but if you have one item in an array (regardless of shape) item() can extract it. Indexing also works, A[0,0]

In [11]: np.squeeze(A).item()
Out[11]: array([[ 1.23]])

item again to extract the number from that inner array:

In [12]: np.squeeze(A).item().item()
Out[12]: 1.23

Or we don't even need the squeeze:

In [13]: A.item().item()
Out[13]: 1.23

loadmat has a squeeze_me parameter.

Indexing is just as easy:

In [17]: A[0,0]
Out[17]: array([[ 1.23]])
In [18]: A[0,0][0,0]
Out[18]: 1.23

astype can also work (though it can be picky about the number of dimensions).

In [21]: A.astype(float)
Out[21]: array([[ 1.23]])

With single item arrays like efficiency isn't much of an issue. All these methods are quick. Things become more complicated when the array has many items, or the items are themselves large.

How to access elements of numpy ndarray?

like image 145
hpaulj Avatar answered Apr 26 '23 22:04

hpaulj


You could use A.all() or A.any() to get a scalar. This would only work if A contains one element.

like image 38
Vermz99 Avatar answered Apr 26 '23 22:04

Vermz99