Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

python - Confusion in size of a numpy array

Python numpy array 'size' confuses me a lot

a = np.array([1,2,3])
a.size = (3, )
------------------------
b = np.array([[2,1,3,5],
             [2,2,5,1],
             [3,6,99,5]])
b.size = (3,4)

'b' makes sense since it has 3 rows and 4 columns in each But how is 'a' size = (3, ) ? Shouldn't it be (1,3) since its 1 row and 3 columns?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No, a numpy.ndarray with shape (1, 3) would look like:

np.array([[1,2,3]])

Think about how the shape corresponds to indexing:

arr[0, ...]  #First row

I still have three more options, namely:

arr[0,0]
arr[0,1]
arr[0,2]

Try doing that with a 1 dimensional array


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...