How to select a string from a complex numpy array?

I read 10-20 different questions / answers and cannot find such an example. I want to select rows from a numpy array as follows:

test = [ [ [0], np.zeros((250,250)), np.array([0,0]) ], [ [0], np.zeros((250,250)), np.array([1,1]) ], [ [1], np.zeros((250,250)), np.array([2,2]) ], [ [2], np.zeros((250,250)), np.array([2,2]) ], [ [2], np.zeros((250,250)), np.array([2,2]) ] ] 

Now convert the list to a numpy array and print the first column:

 nptest = np.array(test) print (nptest[:,0]) > output is: [[0] [0] [1] [2] [2]] 

Now try to select only the row where the first element = 1

 just_1s = nptest[nptest[:,0] == 1] print (just_1s) > output is [] 

I do not understand this conclusion.

In my actual set of problems, I have 100 from each of them with an arbitrary number of rows with values ​​0-15 in the first column. Using the above sample data, the desired result would be three numpy arrays as follows:

 just_0s = [[ [0], np.zeros((250,250)), np.array([0,0]) ], [ [0], np.zeros((250,250)), np.array([1,1]) ] ] just_1s = [[ [1], np.zeros((250,250)), np.array([2,2]) ]] just_2s = [[ [2], np.zeros((250,250)), np.array([2,2]) ], [ [2], np.zeros((250,250)), np.array([2,2]) ] ] 
0
python numpy
source share
3 answers

Use list comprehension

 just_1s = [el for el in nptest if el[0] == [1]] 

But there really is no need to work with nd.array s using the source list in order

 just_1s = [el for el in test if el[0] == [1]] 


If your point is why you get [] when doing nptest[nptest[:, 0] == [1]] (note that I'm testing [1] instead of 1 ), I don’t know . Because, according to me (and Sam Marinelli), it should have worked. We are wrong.


But if you come closer, it turns out that
 just_1s = nptest[ nptest[:,0].tolist().index([1]) ] 

works fine, but only if [1] is unique. This is not so, for example, [2] .

+1
source share

This list creates an array of (5,3) objects:

 In [47]: nptest=np.array(test) In [49]: nptest.shape Out[49]: (5, 3) In [50]: nptest.dtype Out[50]: dtype('O') In [51]: nptest[:,0] Out[51]: array([list([0]), list([0]), list([1]), list([2]), list([2])], dtype=object) 

The first column is an array of (1d) lists. In my new version of numpy , more explicit.

Performing an equality test in an array or even a list of lists is not easy. After trying a few things, I found this:

 In [52]: arow = nptest[:,0] In [56]: [x[0]==1 for x in arow] Out[56]: [False, False, True, False, False] In [57]: mask = [x[0]==1 for x in arow] In [58]: nptest[mask,:] Out[58]: array([[list([1]), array([[ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], ..., [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.]]), array([2, 2])]], dtype=object) 

Or we could turn an array of lists into an array of numbers:

 In [60]: np.array(arow.tolist()) Out[60]: array([[0], [0], [1], [2], [2]]) In [61]: np.array(arow.tolist())==1 Out[61]: array([[False], [False], [ True], [False], [False]], dtype=bool) 

Or check [1] instead of 1. Contains matching lists, not their contents.

 In [64]: [x==[1] for x in arow] Out[64]: [False, False, True, False, False] 
+1
source share

I believe the problem is with the nptest[:, 0] == 1 expression. You have already shown that nptest[:, 0] returns [[0], [0], [1], [2], [2]] , as expected. You can see that none of these values ​​is equal to 1 , so nptest[nptest[:, 0] == 1] will be empty. Instead, try nptest[nptest[:, 0] == [1]] .

0
source share

All Articles