1

I have a homework assignment to extract a 2-dimensional numpy array out of another 2-dimensional np array by choosing specific columns by condition (not by range).

So I have an array A with shape (3, 50000). I am trying to get a new array with shape (3, x) for some x < 50000 with the original columns ofAthat satisfy the third cell in the column is-0.4 < z < 0.1`.

For example if:

A = [[1,2,3],[2,0.5,0],[9,-2,-0.2],[0,0,0.5]]

I wish to have back:

B = [[2,0.5,0],[9,-2,-0.2]

I have tried to make a bool 1 rank array that holds true on the columns I want, and to some how combine between the two. The problem it's output is 1 rank array which is not what I am looking for. And I got some ValueErrors..

bool_idx = (-0.4 < x_y_z[2] < 0.1)

This code made some troubles:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I can do it with some loops but NumPy got so many beautiful function I am sure I am missing something here..

4
  • 3
    Can you share what you tried ? Commented Apr 4, 2019 at 7:22
  • What I try my methos it returns a 1-rank array.. I tried to use x_y_z[bool_idx] when bool_idx is 1-rank array with true on the right columns and false on the ones I dont want.. but it won't work like that.. Commented Apr 4, 2019 at 7:26
  • Can you show a full runnable example please? Commented Apr 4, 2019 at 7:54
  • What do you mean by full runnable example? What I showed with A and B is not clear enough? Commented Apr 4, 2019 at 7:59

1 Answer 1

3

In Python, the expression -0.4 < x_y_z[2] < 0.1 is roughly equivalent to -0.4 < x_y_z[2] and x_y_z[2] < 0.1. The and operator decides the truth value of each part of the expression by converting it into a bool. Unlike Python lists and tuples, numpy arrays do not support the conversion.

The correct way to specify the condition is with bitwise & (which is unambiguous and non-short-circuiting), rather than the implicit and (which short circuits and is ambiguous in this case):

condition = ((x_y_z[2, :] > - 0.4) & (x_y_z[2, :] < 0.1))

condition is a boolean mask that selects the columns you want. You can select the rows with a simple slice:

selection = x_y_z[:, condition] 
Sign up to request clarification or add additional context in comments.

1 Comment

Yesit works! Thank you so much! I dont understand tho what is the meaning of [2, :]. The second row at any column?

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.