2

Suppose we have an array

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

Now I have below

row_r1 = a[1, :]
row_r2 = a[1:2, :]

print(row_r1.shape)
print(row_r2.shape)

I don't understand why row_r1.shape is (4,) and row_r2.shape is (1,4)

Shouldn't their shape all equal to (4,)?

2
  • 1
    when you specify a range, rather than a single number, it returns that range as an axis. If the range 'happens to be' of size 1, it doen't change the fact that you asked for a slice (in that dimension) rather than a single element (which would remove the dimension. Hope it is clear Commented Apr 8, 2019 at 20:52
  • Documentation is clear. Indexing with a scalar reduces a dimension. Slices don't. Question - how does list indexing work? Commented Apr 8, 2019 at 21:26

2 Answers 2

4

I like to think of it this way. The first way row[1, :], states go get me all values on row 1 like this:

enter image description here

Returning: array([5, 6, 7, 8])

shape

(4,) Four values in a numpy array.

Where as the second row[1:2, :], states go get me a slice of data between index 1 and index 2:

enter image description here

Returning:

array([[5, 6, 7, 8]]) Note: the double brackets

shape

(1,4) Four values in on one row in a np.array.

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

Comments

0

Their shapes are different because they aren't the same thing. You can verify by printing them:

import numpy as np

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

row_r1 = a[1, :]
row_r2 = a[1:2, :]

print("{} is shape {}".format(row_r1, row_r1.shape))
print("{} is shape {}".format(row_r2, row_r2.shape))

Yields:

[5 6 7 8] is shape (4,)
[[5 6 7 8]] is shape (1, 4)

This is because indexing will return an element, whereas slicing will return an array. You can however manipulate them to be the same thing using the .resize() function available to numpy arrays. The code:

import numpy as np

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

row_r1 = a[1, :]
row_r2 = a[1:2, :]

print("{} is shape {}".format(row_r1, row_r1.shape))
print("{} is shape {}".format(row_r2, row_r2.shape))
# Now resize row_r1 to be the same shape
row_r1.resize((1, 4))
print("{} is shape {}".format(row_r1, row_r1.shape))
print("{} is shape {}".format(row_r2, row_r2.shape))

Yields

[5 6 7 8] is shape (4,)
[[5 6 7 8]] is shape (1, 4)
[[5 6 7 8]] is shape (1, 4)
[[5 6 7 8]] is shape (1, 4)

Showing that you are in fact now dealing with the same shaped object. Hope this helps clear it up!

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.