In [1]:
import numpy as np

Dot Product

Dot product (return a matrix/array) and vector product (return a single value)


In [28]:
w = np.array([1, 2, 3])
w_mat = np.expand_dims(w, axis=1)  # col vec as mat
d = 3
T = 10
X = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])


t = 0
np.dot(w.T, X[:, [t]])


Out[28]:
array([30])

In [29]:
np.dot(w, X[:, t])  # all vector


Out[29]:
30

In [31]:
np.dot(w, X[:, [t]])


Out[31]:
array([30])

maintain matrix


In [32]:
np.dot(w_mat.T, X[:, [t]])


Out[32]:
array([[30]])

In [33]:
np.dot(w_mat.T, X)


Out[33]:
array([[30, 36, 42]])

In [35]:
np.dot(w_mat, X)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-35-d75f597fe870> in <module>()
----> 1 np.dot(w_mat, X)

ValueError: shapes (3,1) and (3,3) not aligned: 1 (dim 1) != 3 (dim 0)

Col vector by default


In [34]:
np.dot(w, X)  # more tensor-feel


Out[34]:
array([30, 36, 42])

Stype

Suffix


In [39]:
X


Out[39]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

In [40]:
X.mean(axis=0) # np.mean(X, axis=0)


Out[40]:
array([ 4.,  5.,  6.])

In [41]:
X.sum(axis=0)


Out[41]:
array([12, 15, 18])

In [42]:
X.dot(X)


Out[42]:
array([[ 30,  36,  42],
       [ 66,  81,  96],
       [102, 126, 150]])

In [43]:
# However, element-wise must be np.xxx
np.divide(X, 2)


Out[43]:
array([[0, 1, 1],
       [2, 2, 3],
       [3, 4, 4]])

In [44]:
np.divide(X, 2.0)


Out[44]:
array([[ 0.5,  1. ,  1.5],
       [ 2. ,  2.5,  3. ],
       [ 3.5,  4. ,  4.5]])