Help to understand Boolean Indexing mission

https://app.dataquest.io/m/290/boolean-indexing-with-numpy/5/boolean-indexing-with-2-d-ndarrays

In this page, the explanation starts with two graphical examples of boolean indexing, two tables with the numbers from 1 to 12.

In the frist example, the comparison between the boolean array was made with the dimension of the FIRST AXIS of the array to be filtered. No error returned.

In the second example, the comparison between the boolean array was made with the diension of the SECOND AXIS of the array to be filtered. No error returned.

Between the both, there is an example that returns a error.

My first question is: what is the difference between both? How does python know when it have to compare the boolean array with the first axis or the second axis? It was not clearly explained in the mission and I don’t know when to use each of the cases.

In this same screen,there is a moment when it’s said that:

Next, we’ll check for trips with an average speed greater than 20,000 mph:
And the code used was:

# create a boolean array for trips with average
# speeds greater than 20,000 mph
trip_mph_bool = trip_mph > 20000

# use the boolean array to select the rows for
# those trips, and the pickup_location_code,
# dropoff_location_code, trip_distance, and
# trip_length columns
trips_over_20000_mph = taxi[trip_mph_bool,5:9]

print(trips_over_20000_mph)

As we can see above, to create an array with trips with average speed over 20,000 mph, the syntax used was “taxi[trip_mph_bool,5:9]”. Inside the brackets, the boolean array comes first, and after the comma, the slicing of the columns.

But in the examples shown in the beginning of the mission, it was the opposite. Before the comma, comes the slicing, and, after the comma, the boolean array.

It was a bit confusing to me. What is the general rule here? Is there anything that I’m just missing?

2 Likes

image

As said here

The only limitation is that the boolean array must have the same length as the dimension you’re indexing.

If Length of your boolean array does not match length of source array dim you are indexing it will throw IndexError exception.

We are here saying explicitly on which axis to index.
In first example we used arr[bool_1] here it will look for first axis.

In second example arr[:, bool_2] here it will look for second axis as we have used here [:,] (: for first axis, means all elements of first axis)

1 Like


1.The size of bool array and size of the array(axis wise) you want to filter , they have to be the same.
2.It’s not python who will decide along which axis the code will work, It’s you who will decide that your filter will work along which axis(axis=0 / 1st axis(vertically) or axis=1 / 2nd axis(horizontally)).
3. arr[bool_1] = > Here you are asking to work it along 1st axis , See you keep the bool_1 variable where we keep the row index,your column index is not defined here.
4.arr[:, bool_1] => Here you are asking to work it along 2nd axis , See you keep the bool_1 variable where we keep the column index,you selected all row indexes before it by “:” .
5.taxi[trip_mph_bool,5:9] => Here you are asking to work it along 1st axis , See you keep the trip_mph_bool variable where we keep the row index,you selected multiple specific columns indexes(5,6,7 and 8 since 9 is excluded) after it by “5:9”.

" .
I hope I cleared the confusion, Please let me know.

3 Likes

Hi @Sahil ! Could you please help me about how my solution can be marked as solution.
Thanks in advance.

Hi @raisa.jerin.sristy79,

I really appreciate your detailed answer! However, only the topic owner can decide which answer should be marked as a solution. In case the topic is too old and none of the answers is marked as a solution, then you can contact me or one of the moderators. We would be happy to review it and mark your or someone else’s answer as a solution if it answers the question.

:bulb:Tip: I would suggest finding topics that don’t have an answer that is already marked as a solution. Preferably topics that don’t have a satisfactory answer.

Best,
Sahil