Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access elements of numpy ndarray?

I'm using scipy's loadmat function to load a matlab data file into python.

from scipy.io import loadmat  

data   = loadmat('data.mat')
fields = data['field']

The type of fields is 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 iterate 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:

  • How come, if type(x) returns nump.ndarray it says "too many indices for array"?
  • How can I extract the contents of x into 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
like image 880
Steve Lorimer Avatar asked Jun 23 '16 16:06

Steve Lorimer


People also ask

How do you access elements in NumPy Ndarray?

You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

How do I access Ndarray in Python?

Overview. An array in Python is used to store multiple values or items or elements of the same type in a single variable. We can access elements of an array using the index operator [] . All you need do in order to access a particular element is to call the array you created.

How do I extract an element from a NumPy array?

Using the logical_and() method. The logical_and() method from the numpy package accepts multiple conditions or expressions as a parameter. Each of the conditions or the expressions should return a boolean value. These boolean values are used to extract the required elements from the array.

Where can I find unique values in Ndarray?

With the help of np. unique() method, we can get the unique values from an array given as parameter in np. unique() method. Return : Return the unique of an array.


1 Answers

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

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

loadmat renders those in various ways. There are dictionaries that you have to index by name. There are object arrays (dtype=object). And there are nd numeric or string arrays. You may have to work through several levels to get at the numeric array.

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

Here's an example of such a 0d object array:

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 a 0d array (x.ndim), so it must be indexed with a 0 element tuple, (). For a MATLAB programmer that can seem odd.

In numpy (Python in general), x[a,b,c] is the same as x[(a,b,c)] and ind=(a,b,c); x[ind]. In other words, the arguments in [] are understood to be a tuple of values. (1,2) is a 2 element tuple, (1,) is one element ( (1) is just a grouping), and () is a 0 element tuple. So x[()] is just an extension of the regular nd indexing notation. It isn't a special case.

like image 158
hpaulj Avatar answered Oct 19 '22 20:10

hpaulj