Starfruit Python Advanced Lecture 8-Array array (1) Array array and matrix matrix

My CSDN blog column: https://blog.csdn.net/yty_7

Github address: https://github.com/yot777/

 

Before Python introduced the numpy module, it could only use one-dimensional lists such as [1,2,3]. After the introduction of the numpy module, it can be expanded to two-dimensional, three-dimensional, and n-dimensional arrays.

Before using an array or matrix, you must add it to the first line of the Python program

import numpy as np

8.1 Definition of arrays and matrices

array: array

matrix (may be abbreviated as mat) : matrix

Matrix is ​​a branch of array . Matrix and array are basically universal when representing two dimensions (for example, both can be transposed).

But in the non-two-dimensional case, there are the following significant differences:

Matrix can only represent a two-dimensional matrix, even if the input is a one-dimensional list, it will be forced into a two-dimensional matrix.

Array can not only represent two-dimensional arrays, but also represent 1, 3, 4, 5 ... n dimensions. Two-dimensional arrays can also be called matrices.

The official Python suggestion is to choose array when both can be used , because array is more flexible and faster.


8.2 Dimensional expressions of arrays and matrices

Dimensional expressions for arrays and matrices (m, n): m is the number of rows, n is the number of columns

Use the shape () function to get the number of rows and columns of an array (or matrix) (m, n)

In particular, the two-dimensional array (1, n) when m = 1 is called a row vector , and the two-dimensional array (m, 1) when n = 1 is called a column vector . Column vectors can be referred to simply as vectors.

Arrays and matrices can be defined directly with the np.array () function, or they can be converted from lists.

Note that the list elements are separated by commas , but the array elements are separated by spaces .

Arrays or matrices can do row and column interchanges, called transposes, implemented with .T or transpose () functions.

#数组和矩阵转置举例
import numpy as np

p=[[1,2]]
p.append([3,4])
p.append([5,6])
#q是二维数组
q=np.array(p)
print("q=\n",q)
print("q的维度是",np.shape(q))
#s是二维数组q的转置
s=q.T
print("s=\n",s)
print("s的维度是",np.shape(s))
#u是矩阵
u=np.matrix(p)
print("u=\n",u)
print("u的维度是",np.shape(u))
#v是矩阵u的转置
v=u.transpose()
print("v=\n",v)
print("v的维度是",np.shape(v))

运行结果:
q=
 [[1 2]
 [3 4]
 [5 6]]
q的维度是 (3, 2)
s=
 [[1 3 5]
 [2 4 6]]
s的维度是 (2, 3)
u=
 [[1 2]
 [3 4]
 [5 6]]
u的维度是 (3, 2)
v=
 [[1 3 5]
 [2 4 6]]
v的维度是 (2, 3)

How to judge the dimensions of an array? The number of consecutive left brackets (also right brackets) is a few-dimensional array .

For example: array ( [ 1,2,3 ] ) is a one-dimensional array, array ( [[ 1,2], [3,4], [5,6 ]] ) is a two-dimensional array,

array ( [[[ 1,2,3], [4,5,6]], [[7,8,9], [10,11,12]], [[13,14,15], [16 , 17,18 ]]] ) is a three-dimensional array.

Examples of list dimensions:

#列表维度举例
import numpy as np

a=np.array([1,2,3])
b=np.array([[1,2],[3,4],[5,6]])
c=np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]])
print("a=\n",a)
print("a的维度是",np.shape(a))
print("b=\n",b)
print("b的维度是",np.shape(b))
print("c=\n",c)
print("c的维度是",np.shape(c))

运行结果:
a=
 [1 2 3]
a的维度是 (3,)
b=
 [[1 2]
 [3 4]
 [5 6]]
b的维度是 (3, 2)
c=
 [[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]

 [[13 14 15]
  [16 17 18]]]
c的维度是 (3, 2, 3)

How to understand the dimension (3, 2, 3) of array c? In fact, it is three (2,3) two-dimensional arrays.

Examples of one-dimensional arrays:

import numpy as np

array1=np.array([1,2,3])
print("array1=\n",array1)
print("array1的维度是",np.shape(array1))

mat1=np.mat([1,2,3])
print("mat1=\n",mat1)
print("mat1的维度是",np.shape(mat1))

运行结果:
array1=
 [1 2 3]
array1的维度是 (3,)
mat1=
 [[1 2 3]]
mat1的维度是 (1, 3)

It can be seen that the matrix forced the one-dimensional list into a two-dimensional matrix (1, 3).

 

The dimension of the matrix cannot exceed 2, otherwise an error will be reported:

#矩阵的维度超过2
import numpy as np

mat5=np.mat([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]])
print("mat5=\n",mat5)
print("mat5的维度是",np.shape(mat5))

运行结果:
mat4=
 [[1 2 3]
 [4 5 6]]
mat4的维度是 (2, 3)
Traceback (most recent call last):
  File "test1.py", line 7, in <module>
    mat5=np.mat([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]])
  File "C:\Users\yty7\AppData\Roaming\Python\Python36\site-packages\numpy\matrixlib\defmatrix.py", line 71, in asmatrix
    return matrix(data, dtype=dtype, copy=False)
  File "C:\Users\yty7\AppData\Roaming\Python\Python36\site-packages\numpy\matrixlib\defmatrix.py", line 151, in __new__
    raise ValueError("matrix must be 2-dimensional")
ValueError: matrix must be 2-dimensional

It can be seen that matrix does not support three-dimensional matrix, so the program reports an error: matrix must be 2-dimensional

 

8.3 Array multiplication and matrix multiplication

To multiply two matrices, use the symbol * , but array as matrix multiplication should use the method.dot ()

If the array is directly multiplied with *, the result is the product of the corresponding elements in the two matrices.

Simple memory method of matrix multiplication: m rows and n columns matrix, the matrix that can be multiplied by it must be n rows, matrix (m, n ) multiplied by matrix ( n , s) to get the new matrix is ​​(m, s)

Examples of numpy.array multiplication and dot () multiplication:

import numpy as np

array1=np.array([[1,2,3],[4,5,6],[7,8,9]])
array2=np.array([[1],[2],[3]])
print("array1=\n",array1)
print("array2=\n",array2)

#数组相乘
array3 = array1*array2
print("array3=\n",array3)

#数组dot()相乘
array4 = array1.dot(array2)
print("array4=\n",array4)

运行结果:
array1=
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
array2=
 [[1]
 [2]
 [3]]
array3=
 [[ 1  2  3]
 [ 8 10 12]
 [21 24 27]]
array4=
 [[14]
 [32]
 [50]]

It can be seen that array is used as the matrix multiplication method. Dot (), if the array is directly multiplied with *, the result is the product of the corresponding elements in the two matrices.

numpy.matrix multiplication example:

import numpy as np

mat1=np.mat([[1,2,3],[4,5,6],[7,8,9]])
mat2=np.mat([[1],[2],[3]])
print("mat1=\n",mat1)
print("mat2=\n",mat2)

#矩阵相乘
mat3 = mat1*mat2
print("mat3=\n",mat3)

#矩阵dot()相乘
mat4 = mat1.dot(mat2)
print("mat4=\n",mat4)

运行结果:
mat1=
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
mat2=
 [[1]
 [2]
 [3]]
mat3=
 [[14]
 [32]
 [50]]
mat4=
 [[14]
 [32]
 [50]]

In fact, in most Python programs, arrays are more commonly used .

 

to sum up:

The numpy array array and matrix matrix are similar in two dimensions .

matrix can only represent a two-dimensional matrix. Array can not only represent two-dimensional arrays, but also represent 1, 3, 4, 5 ... n dimensions.

Dimension expression of arrays and matrices (m, n): m is the number of rows, n is the number of columns.

In particular, the two-dimensional array (1, n) when m = 1 is called a row vector , and the two-dimensional array (m, 1) when n = 1 is called a column vector . Column vectors can be referred to simply as vectors.

To multiply two matrices, use the symbol * , but array as matrix multiplication should use the method.dot ()

If the array is directly multiplied with *, the result is the product of the corresponding elements in the two matrices.

A matrix of m rows and n columns. The matrix that can be multiplied by it must be n rows. The matrix (m, n ) is multiplied by the matrix ( n , s) to get the new matrix is ​​(m, s)

 

My CSDN blog column: https://blog.csdn.net/yty_7

Github address: https://github.com/yot777/

If you think this chapter is helpful to you, welcome to follow, comment and like! Github welcomes your Follow and Star!

 

Published 55 original articles · won praise 16 · views 6111

Guess you like

Origin blog.csdn.net/yty_7/article/details/104395245