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
117 views
in Technique[技术] by (71.8m points)

python - Difference between these array shapes in numpy

What is the difference between 2 arrays whose shapes are-

(442,1) and (442,) ?

Printing both of these produces an identical output, but when I check for equality ==, I get a 2D vector like this-

array([[ True, False, False, ..., False, False, False],
       [False,  True, False, ..., False, False, False],
       [False, False,  True, ..., False, False, False],
       ..., 
       [False, False, False, ...,  True, False, False],
       [False, False, False, ..., False,  True, False],
       [False, False, False, ..., False, False,  True]], dtype=bool)

Can someone explain the difference?

question from:https://stackoverflow.com/questions/27570756/difference-between-these-array-shapes-in-numpy

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

1 Reply

0 votes
by (71.8m points)

An array of shape (442, 1) is 2-dimensional. It has 442 rows and 1 column.

An array of shape (442, ) is 1-dimensional and consists of 442 elements.

Note that their reprs should look different too. There is a difference in the number and placement of parenthesis:

In [7]: np.array([1,2,3]).shape
Out[7]: (3,)

In [8]: np.array([[1],[2],[3]]).shape
Out[8]: (3, 1)

Note that you could use np.squeeze to remove axes of length 1:

In [13]: np.squeeze(np.array([[1],[2],[3]])).shape
Out[13]: (3,)

NumPy broadcasting rules allow new axes to be automatically added on the left when needed. So (442,) can broadcast to (1, 442). And axes of length 1 can broadcast to any length. So when you test for equality between an array of shape (442, 1) and an array of shape (442, ), the second array gets promoted to shape (1, 442) and then the two arrays expand their axes of length 1 so that they both become broadcasted arrays of shape (442, 442). This is why when you tested for equality the result was a boolean array of shape (442, 442).

In [15]: np.array([1,2,3]) == np.array([[1],[2],[3]])
Out[15]: 
array([[ True, False, False],
       [False,  True, False],
       [False, False,  True]], dtype=bool)

In [16]: np.array([1,2,3]) == np.squeeze(np.array([[1],[2],[3]]))
Out[16]: array([ True,  True,  True], dtype=bool)

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

...