Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...