0

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.

5
  • For starters, you want to call empty: ` selArray = np.empty(some_shape)` Commented Dec 12, 2017 at 7:57
  • You can always use np.where(condition you want to apply on first array) to get the corresponding indices and then retrieve the elements from the second array. Commented Dec 12, 2017 at 7:57
  • Thanks so much! This works - I also found out something I'll add as a possible answer below.. Commented Dec 12, 2017 at 7:59
  • What is the point of transposing when you can just select those elements directly? Besides, you don't even need to do that. Just test_data[result_data > 1] is enough Commented Dec 12, 2017 at 7:59
  • Hi Coldspeed, I added this as the correct answer (found it out myself a minute ago). Many thanks! Commented Dec 12, 2017 at 8:02

1 Answer 1

1

It turned out to be pretty straight forward:

selArray = test_data[result_data_>1]

See also possible solution in comment from Nain!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.