0

I have a 2 d numpy array. I need to keep all the rows whose value at a specific column is greater than a certain number. Right now, I have:

f_left = np.where(f_sorted[:,attribute] >= split_point)

And it is failing with: "Index Error: too many indices for array"

How should I do this? I can't figure it out from the numpy website, here

1
  • Can you post sample data: This works fine with a simple case where attribute = 0, split_point = 2, and f_sorted = np.arange(20).reshape((2, 10)). Commented Feb 19, 2016 at 1:34

2 Answers 2

4

You actually don't even need where.

    yy = np.array(range(12)).reshape((4,3))


    yy[yy[:,1] > 2]

Output

array([[ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
Sign up to request clarification or add additional context in comments.

Comments

3
x = np.array([[2,3,4],[5,6,7],[1,2,3],[8,9,10]])

array([[ 2,  3,  4],
       [ 5,  6,  7],
       [ 1,  2,  3],
       [ 8,  9, 10]])

Find the rows where the second element are >=4

x[np.where(x[:,1] >= 4)]

array([[ 5,  6,  7],
       [ 8,  9, 10]])

1 Comment

Awesome, thanks...I would not have gotten there (new to python last semester)

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.