Tools
Text

# Matrix/Vector Operations – Linear Algebra

Lesson 9 Chapter 3

This section is dedicated to what we may mostly use in Machine Learning. Operations on vectors and matrices. Let's take a look:

# Import Numpy library
import numpy as np

# Create two vectors and two matrices
v = np.array([0,8]).reshape(-1,1)
u = np.array([1,4]).reshape(-1,1)
A = np.array([[2,1],[5,2]])
B = np.array([[2,1],[5,2]])

# Dot porduct of two vectors with two approaches
print('v.u = ', v.dot(u.transpose()))
print('v.u = ', np.dot(v, u.transpose()))

# Porduct of a vector with a matrix
print('A.v = ', A.dot(v))
print('A.v = ', np.dot(A, v))

# Matrix product with three approaches
print('A.B = ', A.dot(B))
print('A.B = ', np.dot(A, B))
print('A.B = ', np.matmul(A,B))

Let's do a practice. Run the above code and answer the following questions:

• What is the shape and rank of and ?
• In lines 11 and 12, did we have to use ".transpose()"? Why?
• Instead of calculating 'v.u' how would you calculate 'u.v'?
• Take a look at lines 15 and 16. Instead of 'A.v', can we calculate 'v.A'?

I have used np.matmul, in one of the previous posts. Now, let's discussed the frequently used operations that we use in Machine Learning: Sum and mean over a matrix, or along with a specific dimension:

# Import Numpy library
import numpy as np

# Create a matrix
A = np.array([[2,1,3,4],[5,2,9,4]])
print('A=', A)

# Sum and mean over the matrix
print('sum(A) = ', np.sum(A))
print('mean(A) ', np.mean(A))

# Sum and mean over axiz zero (rows)
print('Sum over rows = ', np.sum(A, axis=0))
print('Mean over rows = ', np.mean(A, axis=0))

# Sum and mean over axiz one (colums)
print('Sum over columns = ', np.sum(A, axis=1))
print('Mean over columns = ', np.mean(A, axis=1))

A= [[2 1 3 4]
[5 2 9 4]]
sum(A) =  30
mean(A)  3.75
Sum over rows =  [ 7  3 12  8]
Mean over rows =  [3.5 1.5 6.  4. ]
Sum over columns =  [10 20]
Mean over columns =  [2.5 5. ]

NOTE: When we take the sum/mean over a specific axis, the result is an array in which that dimension is squeezed to one dimension. The example above shows if we take the sum/mean over the dimension zero (one), the resulting array has only one row (column), and the number of columns (rows) is equal to the number of columns (rows) in the main matrix .