Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indexing Numpy array with list and tuple gives different results?

Sample code:

import numpy as np
a = np.zeros((5,5))
a[[0,1]] = 1     #(list of indices)
print('results with list based indexing\n', a)

a = np.zeros((5,5))
a[(0,1)] = 1   #(tuple of indices)
print('results with tuple based indexing\n',a)

Result:

results with list based indexing
 [[ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]]

results with tuple based indexing
[[  0.   1.   0.   0.   0.]
 [  0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.]]

As you must have noticed, indexing array with list gave a different result than with tuple of same indices. I'm using python3 with numpy version 1.13.3

What is the fundamental difference in indexing a numpy array with list and tuple?

like image 457
Tejas Avatar asked Feb 26 '18 16:02

Tejas


People also ask

How indexing in Numpy and pandas are different?

Pandas has a better performance when a number of rows is 500K or more. Numpy has a better performance when number of rows is 50K or less. Indexing of the pandas series is very slow as compared to numpy arrays. Indexing of numpy Arrays is very fast.

Do Numpy arrays have to have the same data type?

As I mentioned above, NumPy arrays must contain data all of the same type. That means that if your NumPy array contains integers, all of the values must be integers. If it contains floating point numbers, all of the values must be floats.

Are tuples faster than Numpy arrays?

If the code can be vectorized, then numpy will most likely be faster than Python tuples.

What is Numpy fancy indexing?

Fancy indexing is conceptually simple: it means passing an array of indices to access multiple array elements at once. For example, consider the following array: import numpy as np rand = np. random. RandomState(42) x = rand.


1 Answers

By design. Numpy's getitem and setitem syntax does not duck-type, because the different types are used to support different features. This is just a plain old __setitem__:

a[(0,1)] = 1

It's the same as doing a[0,1] = 1. In both cases, ndarray's setitem receives two arguments: a tuple for the index (0, 1) and the value 1.

a[[0,1]] = 1

This is a special case of broadcasting. It would usually be written a[0:2] = 1, but you could also slice/mutate other rows for example a[[0,1,3]]. The scalar 1 gets "stretched" across all columns of rows 0 and 1 in the assignment.

like image 175
wim Avatar answered Nov 01 '22 17:11

wim