I have tried the following code but didn\'t find the difference between np.dot and np.multiply with np.sum
Here is np.dot
If Y
and A2
are (1,N) arrays, then np.dot(Y,A.T)
will produce a (1,1) result. It is doing a matrix multiplication of a (1,N) with a (N,1). The N's
are summed, leaving the (1,1).
With multiply
the result is (1,N). Sum all values, and the result is a scalar.
If Y
and A2
were (N,) shaped (same number of elements, but 1d), the np.dot(Y,A2)
(no .T
) would also produce a scalar. From np.dot
documentation:
For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors
Returns the dot product of a and b. If a and b are both scalars or both 1-D arrays then a scalar is returned; otherwise an array is returned.
squeeze
reduces all size 1 dimensions, but still returns an array. In numpy
an array can have any number of dimensions (from 0 to 32). So a 0d array is possible. Compare the shape of np.array(3)
, np.array([3])
and np.array([[3]])
.