Numpy: selecting 1D row on task 289-6

While on task 289-6

https://app.dataquest.io/m/289/introduction-to-numpy/6/selecting-and-slicing-rows-and-items-from-ndarrays

I expected these two codes to produce the same result but only Option 1 is accepted. What is the difference between the two? How to interpet the double square brackets from Option 2? Thanks.

Option 1: row_0 = taxi[0]
–>
array([ 2016, 1, 1, 5, 0, 2, 4, 21,
2037, 52, 0.8, 5.54, 11.65, 69.99, 1])

Option 2: row_0 = taxi[0:1]
–> array([[ 2016, 1, 1, 5, 0, 2, 4, 21,
2037, 52, 0.8, 5.54, 11.65, 69.99, 1]])

Focus on the brackets there.

array([ 2016, 1, 1, 5, 0, 2, 4, 21, 2037, 52, 0.8, 5.54, 11.65, 69.99, 1])

Has just the one bracket pair containing the numbers.

array([[ 2016, 1, 1, 5, 0, 2, 4, 21, 2037, 52, 0.8, 5.54, 11.65, 69.99, 1]])

Has two bracket pairs containing the numbers.

This is just how it works for Numpy arrays. If you print out the shape for both those cases -

row_0 = taxi[0]
print(row_0.shape)

The above outputs

(15,)

That’s a one-dimensional array with 15 elements
And for the other one,

row_0 = taxi[0:1]
print(row_0.shape)

The above outputs

(1, 15)

That’s a 1x15 two-dimensional array

This has more to do with how indexing and slicing works for Numpy. It can be confusing at times. Usually, from what I know, it’s recommended to be careful of the shapes of the Numpay arrays/matrices that you are working with. Simple print statements to check out their shapes are mostly enough to check that.

2 Likes

Checker is off:


my code is the same as the solution yet the checker finds an error

@djosiasmp

Your range for the slice operation on taxi is incorrect.

taxi[319:501]

It should start from 391 not 319 .

Dah!
Thanks, my dilexia strikes again! (I read it many times)

Select every column for the rows at indexes 391 to 500 inclusive. Assign them to rows_391_to_500 .
I do not know why in code, the solution is:

rows_391_to_500 = taxi[391:501] because I did [391:500]

In Python, when slicing a list, the index after the colon, :, needs to be 1 higher than your intended range.

a = [4, 5, 6, 7]

a[0:2] will output 4, 5. The 2 in 0:2 is not included. If you want to access the first 3 values in the list, you will have to use a[0:3].

That’s what the problem instructions mean by 500 inclusive. They want 500th value to be included as well. So, it should be 391:501.

1 Like