Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13921

Why dot product of two arrays produces a scalar value but dot product of transposed array produce a matrix

$
0
0

In numpy, I realized the following two calculations produce different results.

a = np.array([1,2,3,4])b = np.array([1,2,3,4])dot_product1 = np.dot(a, b)    <--- I think this should be an errorprint(dot_product1)a = np.array([1,2,3,4])b = np.array([1,2,3,4]).reshape(-1,1)dot_product2 = np.dot(a, b)print(dot_product2)

The dot_product1 is a scalar value 30, but the dot_product2 is a 1x1 matrix, [30].

My understanding of linear algebra is that we cannot calculate dot product of a 1 x 4 matrix with another 1 x 4 matrix. I expect the third line fail but it is successful.

The second part of the code calculates a 1 x 4 matrix and a 4 x 1 matrix, which produces a 1 x 1 matrix. This is what I expected.

Can someone help explain what is the difference between these to calculations?


Viewing all articles
Browse latest Browse all 13921

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>