What happens to this type of choice?

Answering this question , some others and I were really mistaken in the belief that the following would work:

Let's say that he has

test = [ [ [0], 1 ], [ [1], 1 ] ] import numpy as np nptest = np.array(test) 

What is the reason

 >>> nptest[:,0]==[1] array([False, False], dtype=bool) 

while

 >>> nptest[0,0]==[1],nptest[1,0]==[1] (False, True) 


or
 >>> nptest==[1] array([[False, True], [False, True]], dtype=bool) 

or

 >>> nptest==1 array([[False, True], [False, True]], dtype=bool) 

Is it a degeneration in size that causes it.

+6
python numpy numpy-broadcasting
source share
1 answer

nptest is a 2D array of a dtype object, and the first element of each line is a list.

nptest[:, 0] is a 1D array of a dtype object, each of which is a list.

When you execute nptest[:,0]==[1] , NumPy does not perform an elementary comparison of each nptest[:,0] element with the list [1] . It creates as many-dimensional array as it can from [1] , creating np.array([1]) -array np.array([1]) , and then translates the comparison, comparing each element nptest[:,0] with an integer.

Since not a single list from nptest[:, 0] is equal to 1, all elements of the result are False.

+3
source share

All Articles