How to access numpy ndarray elements?

I am using scipy loadmat function to load matlab data file in python.

 from scipy.io import loadmat data = loadmat('data.mat') fields = data['field'] 

Type fields - numpy.ndarray :

 print 'fields type={}'.format(type(fields)) print 'fields dtype={}'.format(fields.dtype) print 'fields shape={}'.format(fields.shape) 
 fields type=<type 'numpy.ndarray'> fields dtype=object fields shape=(5,) 

I nditer over the array using nditer :

 for x in np.nditer(fields, flags=['refs_ok']): print 'x={}'.format(x) print 'x type={}'.format(type(x)) print 'x dtype={}'.format(x.dtype) print 'x shape={}'.format(x.shape) break 
 x=[u'ACE'] x type=<type 'numpy.ndarray'> x dtype=object x shape=() 

IndexError:

If I try to access the first element of x , I get an IndexError :

 x[0] 
 --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-102-8c374ae22096> in <module>() 17 print 'type={}'.format(type(x)) 18 print 'dtype={}'.format(x.dtype) ---> 19 x[0] 20 break 21 IndexError: too many indices for array 

Questions:

  • Why, if type(x) returns nump.ndarray , it says "too many indexes for the array"?
  • How can I extract the contents of x to a string?

Here are the versions I'm using:

 print 'python version: {}'.format(sys.version) print 'numpy version: {}'.format(numpy.__version__) print 'scipy version: {}'.format(scipy.__version__) 
 python version: 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] numpy version: 1.11.0 scipy version: 0.17.1 
+6
source share
1 answer

Without looking at your mistakes in detail, I can point out some pitfalls.

.MAT will contain MATLAB matrices (always 2d or higher), cells and structures.

loadmat displays them in various ways. There are dictionaries that you must index by name. There are object arrays (dtype = object). And there are numeric or string arrays. You may need to work through several levels to get a numerical array.

Check the "shape" (size) of the array and its "dtype". If the form is () and the dtype object, then extract it using y=x[()] .

Here is an example of such an array of 0d objects:

 In [4]: y=np.arange(3) In [5]: x=np.empty((), dtype=object) In [6]: x[()]=y In [7]: x Out[7]: array(array([0, 1, 2]), dtype=object) In [8]: x.shape Out[8]: () In [9]: x.dtype Out[9]: dtype('O') In [10]: x[0] ... IndexError: too many indices for array In [11]: x[()] Out[11]: array([0, 1, 2]) 

x - is an array 0d (x.ndim), so it should be indexed via the tuple 0 elements () . For the MATLAB programmer, which may seem strange.

In numpy (in general, Python), x[a,b,c] matches x[(a,b,c)] and ind=(a,b,c); x[ind] ind=(a,b,c); x[ind] . In other words, the arguments in [] are understood as a set of values. (1,2) is a 2-element tuple, (1,) is one element ( (1) is just a grouping), and () is the root element. So x[()] is just an extension of the regular nd indexing notation. This is not a special case.

+6
source

All Articles