Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the difference between (4,) and (4,1) for the shape in Numpy?

I have two ndarray A and B, one has the shape (4,) and another (4,1).

When I want to calculate the cosine distance using this, it throws some exceptions that complains the two objects are not aligned

Does anyone have ideas about this? Thanks!

like image 443
Hanfei Sun Avatar asked Jan 15 '23 03:01

Hanfei Sun


1 Answers

One is a 1-dimensional array, the other is a 2-dimensional array.

Example:

>>> import numpy as np
>>> a = np.arange(4).reshape(4,1)
>>> a
array([[0],
       [1],
       [2],
       [3]])
>>> a.ravel()
array([0, 1, 2, 3])
>>> a.squeeze()
array([0, 1, 2, 3])
>>> a[:,0]
array([0, 1, 2, 3])
>>>
>>> a[:,0].shape
(4,)
like image 77
mgilson Avatar answered Jan 20 '23 20:01

mgilson