简体   繁体   中英

Select subset of numpy.ndarray based on other array's values

I have two numpy.ndarrays and I would like to select a subset of Array #2 based on the values in Array #1 (Criteria: Values > 1):

#Array 1 - print(type(result_data):
<class 'numpy.ndarray'>
#print(result_data):
[ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  3  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
  ...
  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  1  3  3  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1 -1 -1 -1 -1 -1 -1 -1 -1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1]

#Array #2 - print(type(test_data):
<class 'numpy.ndarray'>
#print(test_data):
[[-1.38693584  0.76183275]
 [-1.38685102  0.76187584]
 [-1.3869291   0.76186742]
 ..., 
 [-1.38662322  0.76160456]
 [-1.38662322  0.76160456]
 [-1.38662322  0.76160456]]

I tried:

x=0
selArray = np.empty
for i in result_data:
    x+=1
    if i > 1:
         selArray = np.append(selArray,[test_data[x].T[0],test_data[x].T[1]])

...but this gives me:

#print(type(selArray)):
<class 'numpy.ndarray'>
#print(selArray):
[<built-in function empty> -1.3868538952656493 0.7618747030055314
 -1.3868543839578398 0.7618746157390688 -1.3870217784863983
 0.7618121504051398 -1.3870217784863983 0.7618121504051398
 -1.3870217784863983 0.7618121504051398 -1.3869304105000566
...
 -1.3869682317849474 0.7617139232748376 -1.3869103741202438
 0.7616839734248734 -1.3868025127724706 0.7616153994385625
 -1.3869751607420777 0.761730050117126 -1.3866515941520503
 0.7615994122226143 -1.3866515941520503 0.7615994122226143]

Clearly, [] are missing around elements - and I don't understand where the <built-in function empty> comes from.

It turned out to be pretty straight forward:

selArray = test_data[result_data_>1]

See also possible solution in comment from Nain!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM