Matrix-Vector Multiplication

Matrix vector multiplication is a widely used linear algebra property particularly in regression modeling where a vector of parameters is multipled to a matric of features to predict outcomes.

Suppose a matrix $A$ and a vector $b$ given as:

$$ A = \begin{pmatrix} a_{1,1} & a_{1,2} \\ a_{2,1} & a_{2,2} \\ a_{3,1} & a_{3,2} \end{pmatrix}, b= \begin{pmatrix} b_{1} \\ b_{2} \end{pmatrix} $$

Their product is:

$$ C = Ab = \begin{pmatrix} a_{1,1}* b_{1} + a_{1,2}* b_{2} \\ a_{2,1}* b_{1} + a_{2,2}* b_{2} \\ a_{3,1}* b_{1} + a_{3,2}*b_{2} \end{pmatrix} $$

Example:

Given matrix $A$ and vector $b$ as:

$$A = \begin{pmatrix} 2 & 3 \\ 1 & 5 \\ 6 & 9 \end{pmatrix}, b= \begin{pmatrix} 2 \\ 3 \end{pmatrix} $$

Then:

$$ C = Ab = \begin{pmatrix} 2*2 +3*3 \\ 1* 2 + 5*3 \\ 6*2 + 9*2 \end{pmatrix} = \begin{pmatrix} 4+9 \\ 2+15 \\ 12+27 \end{pmatrix} = \begin{pmatrix} 13 \\ 17 \\ 39 \end{pmatrix}$$

Implementation in Numpy

import numpy as np
A = np.array([2,3,1,5,6,9]).reshape(3,2)
b = np.array([2,3])

A.dot(b)
array([13, 17, 39])