Types of Matrices

 

 

# Example of creating a triangular matrices
# triangular matrices
from numpy import array
from numpy import tril
from numpy import triu
# define square matrix
M = array([
    [1, 2, 3],
    [1, 2, 3],
    [1, 2, 3]
])
print(M)
# lower triangular matrix
lower = tril(M)
print(lower)

# upper triangular matrix
upper = triu(M)
print(upper)

 

 1.5 Diaginal Matrix

A diagonal matrix is one where values outside of the main diagonal have a zero value, where the main diagonal is taken from the top left of the matrix to the bottom right. A diagonal matrix is often denoted with the variable D and may be represented as a full matrix or as a vector of values on the main diagonal.

Diagonal matrices consist mostly of zeros and have non-zero entries only along the main diagonal.

                                                                                                — Page 40, Deep Learning, 2016.

 NumPy provides the function diag() that can create a diagonal matrix from an existing matrix, or transform a vector into a diagonal matrix. The example below defines a 3 × 3 square matrix, extracts the main diagonal as a vector, and then creates a diagonal matrix from the extracted vector.

# diagonal matrix
from numpy import array
from numpy import diag
# define square matrix
M = array([
    [1, 2, 3],
    [1, 2, 3],
    [1, 2, 3]
])
print(M)
# extract diagonal vector
d = diag(M)
print(d)
# Create diagonal matrix from vector
D = diag(d)
print(D)

Running the example first prints the defined matrix, followed by the vector of the main diagonal and the diagonal matrix constructed from the vector. 

 1.6 Identity Matrix

An identity matrix is a square matrix that does not change a vector when multiplied. The values of an identity matrix are known. All of the scalar values along the main diagonal (top-left to bottom-right) have the value one, while all other values are zero.

 In NumPy, an identity matrix can be created with a specific size using the identity() function. The example below creates an I 3 identity matrix.

# Identity matrix
from numpy import identity
I = identity(3)
print(I)

Running the example prints the created identity matrix.

 1.7 Orthogonal Matrix

 The example below creates this orthogonal matrix and checks the above equivalences.

# orthogonal matrix
from numpy import array
from numpy.linalg import inv
# define orthogonal matrix
Q = array([
    [1, 0],
    [0, -1]
])
print(Q)


# inverse equivalence
V = inv(Q)
print(Q.T)
print(V)

# identity equivalence
I = Q.dot(Q.T)
print(I)

Running the example first prints the orthogonal matrix, the inverse of the orthogonal matrix, and the transpose of the orthogonal matrix are then printed and are shown to be equivalent. Finally, the identity matrix is printed which is calculated from the dot product of the orthogonal matrix with its transpose. 

 Note, sometimes a number close to zero can be represented as -0 due to the rounding of floating point precision. Just take it as 0.0. Orthogonal matrices are useful tools as they are computationally cheap and stable to calculate their inverse as simply their transpose.

1.8 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 for creating each matrix type.
  • Research one example where each type of array was used in machine learning.

1.9 Summary

In this tutorial, you discovered a suite of different types of matrices from the field of linear algebra that you may encounter in machine learning. Specifically, you learned:

  • Square, symmetric, triangular, and diagonal matrices that are much as their name suggests.
  • Identity matrices that are all zero values except along the main diagonal where the values are 1.
  • Orthogonal matrices that generalize the idea of perpendicular vectors and have useful computational properties.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值