Home > other >  Dot product with numpy gives array with size (n, )
Dot product with numpy gives array with size (n, )

Time:10-05

I am trying to get the dotproduct of two arrays in python using the numpy package. I get as output an array of size (n,). It says that my array has no column while I do see the results when I print it. Why does my array have no column and how do I fix this? My goal is to calculate y - np.dot(x,b). The issue is that y is (124, 1) while np.dot(x,b) is (124,) Thanks

CodePudding user response:

It seems that you are trying to subtract two arrays of a different shape. Fortunately, it is off by a single additional axis, so there are two ways of handling it.

(1) You slice the y array to match the shape of the dot(x,b) array:

y = y[:,0]
print(y-np.dot(x,b))

(2) You add an additional axis on the np.dot(x,b) array:

dot = np.dot(x,b)
dot = dot[:,None]
print(y-dot)

Hope this helps

CodePudding user response:

it may depends on the dimension of your array

For example :

a = [1, 0]
b = [[4, 1], [2, 2]]
c = np.dot(a,b)

gives

array([4, 1])

and its shape is (2,)

but if you change a like :

a = [[1, 0],[1,1]]

then result is :

array([[4, 1], [6, 3]])

and its shape is (2,2)

  • Related