Last answered:

01 Nov 2021

Posted on:

30 Oct 2021

0

Resolved: Am I wrong when applied dot product with row vector and column vector it give result a scalar?

Because when I type in google colab as follow:
x = np.array([2,8,-4])
y = np.array([1,-7,3])
np.dot(x,(y.T))
it gave a result -66

2 answers ( 1 marked as helpful)
Posted on:

30 Oct 2021

0

I'm sorry there is some mistake, it should be when I multiply column vector with row vector, so the code is np.dot((x.T),y) but its still give me a scalar value

Instructor
Posted on:

01 Nov 2021

0

Hey Julinar,

Thank you for you question!

Let's examine your code step by step. First, you declare a vector x:

x = np.array([2,8,-4])

Typing x.shape returns (3, ) which indicates that we have simply stored three ordered elements in the memory of the computer. Therefore, Python is not able to perform transposition on vectors. What we need to do is to reshape the variable, namely:

x = x.reshape(1, 3)

In this way, our vector now has dimensions (1, 3) i.e., 1 row and 3 columns. Let us reshape y in the same way:

y = y.reshape(1, 3)

Now, we are able to perform the dot product between x.T and y. Note that x.T will change the dimension of x to be (3, 1) instead. Therefore, the code

np.dot((x.T),y)

takes the dot product of a matrix having a shape of (3, 1) with a second matrix of shape (1, 3). This should result in a 3x3 matrix and this is exactly what we get upon executing the code:

array([[  2, -14,   6],
       [  8, -56,  24],
       [ -4,  28, -12]])



Hope this helps! If you still feel uncertain, I suggest you study our video on transposing a matrix in Python :)

https://learn.365datascience.com/courses/mathematics/transpose-of-a-matrix/

Keep up the good work!

Kind regards,
365 Hristina

Submit an answer