Matrix Operations

Matrix operations are used in the description of many machine learning algorithms. Some operations can be used directly to solve key equations, whereas others provide useful shorthand or foundation in the description and the use of more complex matrix operations. In this tutorial, you will discover important linear algebra matrix operations used in the description of machine learning methods. After completing this tutorial, you will know:

  • The Transpose operation for flipping the dimensions of a matrix.
  • The Inverse operations used in solving systems of linear equations.
  • The Trace and Determinant operations used as shorthand notation in other matrix operations.

1.1 Tutorial Overview

This tutorial is divided into 5 parts; they are:

1. Transpose

2. Inverse
3. Trace

4. Determinant

5. Rank

1.2 Transpose

A defined matrix can be transposed, which creates a new matrix with the number of columns and rows flipped. This is denoted by the superscript T next to the matrix A^{T}.

                                                        C = A^{T}

   An invisible diagonal line can be drawn through the matrix from top left to bottom right on which the matrix can be flipped to give the transpose.

We can transpose  a matrix  in NumPy by calling the T attribute.

# Example of creating a transpose of a matrix
# transpose matrix
from numpy import array
# define matrix
A = array([
    [1, 2],
    [3, 4],
    [5, 6]
])
print(A)

# calculate transpose
C = A.T
print(C)

Running the example first prints the matrix as it is defined, then the transposed version.

1.3 Inverse

Matrix inversion is a process that finds another matrix that when multiplied with the matrix,results in an identity matrix.Given a matrix A, find matrix B, such that AB=I^{n} or BA = I^{n}.

                                        AB = BA = I^{n}

                 The operation of inverting a matrix is indicated by a -1 superscript next to the matrix; for example , A^{-1}. The result of the operation is referred to as the inverse of the original matrix;for example ,B is the inverse of A.

                                        B = A^{-1}

A matrix is invertible if there exists another matrix that results in the identity matrix, where not all matrices are invertible. A square matrix that is not invertible is referred to as singular.

A matrix can be inverted in NumPy using the inv() function.

# Example of creating the inverse of a matrix
# invert matrix
from numpy import array
from numpy.linalg import inv
# define matrix
A = array([
    [1.0, 2.0],
    [3.0, 4.0]
])
print(A)

# invert matrix
B = inv(A)
print(B)

# multiply A and B
I = A.dot(B)
print(I)

First, we define a small 2 × 2 matrix, then calculate the inverse of the matrix, and then confirm the inverse by multiplying it with the original matrix to give the identity matrix. Running the example prints the original, inverse, and identity matrices.

 Note, your specific results may vary given differences in floating point precision on different hardware and software versions. Matrix inversion is used as an operation in solving systems of equations framed as matrix equations where we are interested in finding vectors of unknowns. A good example is in finding the vector of coefficient values in linear regression.

1.4 Trace

A trace of a square matrix is the sum of the values on the main diagonal of the matrix (top-left to bottom-right).

The trace operator gives the sum of all of the diagonal entries of a matrix

The operation of calculating a trace on a square matrix is described using the notation tr(A) where A is the square matrix on which the operation is being performed.

                                                        tr(A)

            The trace is calculated  as the sum of the diagonal values:for example , in the case of a 3 X 3 matrix:

                                        tr(A) = a_{1,1} + a_{2,2} + a_{3,3}

Or, using array notation:

                                        tr(A) = A[0,0] + A[1,1] + A[2, 2] 

We can calculate the trace of a matrix in NumPy using the trace() function.

# Example of creating the trace of a matrix
# matrix trace
from numpy import array
from numpy import trace
# define matrix
A = array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
print(A)

# calculate  trace
B = trace(A)
print(B)

First, a 3 × 3 matrix is created and then the trace is calculated. Running the example, first the array is printed and then the trace.

 Alone, the trace operation is not interesting, but it offers a simpler notation and it is used as an element in other key matrix operations.

1.5 Determinant

The determinant of a square matrix is a scalar representation of the volume of the matrix.

        The determinant describes the relative geometry of the vectors that make up the rows of the matrix. More specifically, the determinant of a matrix A tells you the volume of a box with sides given by rows of A.

        It is denoted by the det(A) notation or |A|, where A is the matrix on which we are calculating the determinant.

                                                det(A)

        The determinant of a square matrix is calculated from the elements of the matrix. More technically, the determinant is the product of all the eigenvalues of the matrix. Eigenvalues are introduced in the lessons on matrix factorization. The intuition for the determinant is that it describes the way a matrix will scale another matrix when they are multiplied together. For example, a determinant of 1 preserves the space of the other matrix. A determinant of 0 indicates that the matrix cannot be inverted.

        The determinant of a square matrix is a single number. [...] It tells immediately whether the matrix is invertible. The determinant is a zero when the matrix has no inverse.

In NumPy, the determinant of a matrix can be calculated using the det() function.

 

# Example of creating the determinant of a matrix
# matrix determinant
from numpy import array
from numpy.linalg import det
# define matrix
A = array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

print(A)

# calculate determinant
B = det(A)
print(B)

First, a 3 × 3 matrix is defined, then the determinant of the matrix is calculated. Running the example first prints the defined matrix and then the determinant of the matrix.

 1.6 Rank

The rank of a matrix is the estimate of the number of linearly independent rows or columns in a matrix. The rank of a matrix M is often denoted as the function rank().

                                                rank(A)

An intuition for rank is to consider it the number of dimensions spanned by all of the vectors within a matrix. For example, a rank of 0 suggest all vectors span a point, a rank of 1 suggests all vectors span a line, a rank of 2 suggests all vectors span a two-dimensional plane. The rank is estimated numerically, often using a matrix decomposition method. A common approach is to use the Singular-Value Decomposition or SVD for short. NumPy provides the matrix rank() function for calculating the rank of an array. It uses the SVD method to estimate the rank. The example below demonstrates calculating the rank of a matrix with scalar values and another vector with all zero values.

# Example of calculating the rank of vectors
# vector rank
from numpy import array
from numpy.linalg import matrix_rank
# rank
v1 = array([1, 2, 3])
print(v1)
vr1 = matrix_rank(v1)
print(vr1)
# zero rank
v2 = array([0, 0, 0, 0, 0])
print(v2)
vr2 = matrix_rank(v2)
print(vr2)

Running the example prints the first vector and its rank of 1, followed by the second zero vector and its rank of 0.

 The next example makes it clear that the rank is not the number of dimensions of the matrix, but the number of linearly independent directions. Three examples of a 2 × 2 matrix are provided demonstrating matrices with rank 0, 1 and 2.

# matrix rank 
from numpy import array
from numpy.linalg import matrix_rank
# rank 0
M0 = array([
    [0, 0],
    [0, 0]
])
print(M0)

mr0 = matrix_rank(M0)
print(mr0)

# rank 1
M1 = array([
    [1, 2],
    [1, 2]
])
print(M1)
mr1 = matrix_rank(M1)
print(mr1)
# rank 2
M2 = array([
    [1,2],
    [3,4]
])

print(M2)

mr2 = matrix_rank(M2)
print(mr2)

Running the example first prints a zero 2 × 2 matrix followed by the rank, then a 2 × 2 with a rank 1 and finally a 2 × 2 matrix with a rank of 2.

1.7 Extensions

This section lists some ideas for extending the tutorial that you may wish to explore.

  • Modify each example using your own small contrived array data.
  • Write your own functions to implement one operation.
  • Research one example where each operation was used in machine learning.

1.8 Summary

In this tutorial, you discovered important linear algebra matrix operations used in the description of machine learning methods. Specifically, you learned:

  • The Transpose operation for flipping the dimensions of a matrix.
  • The Inverse operations used in solving systems of linear equations.
  • The Trace and Determinant operations used as shorthand notation in other matrix operations.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值