機器學習演算法原理與程式設計學習(2)

memory丶ysu發表於2017-06-16

基於python語言的矩陣編輯,離不開簡單的矩陣的初始化、矩陣的元素運算,矩陣的乘法、矩陣的轉置以及矩陣的其他操作(行列數、切片、複製和比較),下面就使用python語言來編寫程式碼,進行簡單的基礎練習。

注意:在寫程式碼之前,我們知道先要匯入所使用的庫,需要什麼,在最前面加上即可。

1. 矩陣的初始化

(1)建立一個3*4的全0矩陣和全1矩陣

import numpy as np   #匯入NumPy包
myZero = np.zeros([3,4])
print myZero
myOnes = np.ones([3,4])

輸出結果:

[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]
[[ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]]

(2)生成隨機矩陣和單位陣

import numpy as np   #匯入NumPy包
myRand = np.random.rand(3,4)   #3行4列的0~1之間的隨機數矩陣
print myRand
myEye = np.eye(3)
print myEye

輸出結果:

[[ 0.93474094  0.22083997  0.5754403   0.9886223 ]
 [ 0.65640633  0.42275327  0.07622696  0.87353052]
 [ 0.95146727  0.04091281  0.31654216  0.49595177]]
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

2. 矩陣的元素運算

(1)元素的相加減

數學公式: (A+B)i,j=Ai,j±Bi,j

(A+B)_{i,j}=A_{i,j}\pm B_{i,j}

條件:矩陣的行列數相同

from numpy import *  #匯入NumPy包
myOnes = ones([3,3])
myEye = eye(3)
print myOnes + myEye 
print myOnes - myEye 

輸出結果:

[[ 2.  1.  1.]
 [ 1.  2.  1.]
 [ 1.  1.  2.]]
[[ 0.  1.  1.]
 [ 1.  0.  1.]
 [ 1.  1.  0.]]

(2)矩陣數乘

數學公式:(cA)i,j=cAi,j

(cA)_{i,j} = c\cdot A_{i,j}

from numpy import *
mymatrix = mat([[1,2,3],[4,5,6],[7,8,9,]])
a = 10
print a*mymatrix

輸出結果:

[[10 20 30]
 [40 50 60]
 [70 80 90]]

(3)矩陣所有元素求和

數學公式:sum(A)=mi=1nj=1Ai,j

sum(A) = \sum_{i=1}^{m}\sum_{j=1}^{n}A_{i,j}

其中,1<i<m,1<j<n
1<i<m , 1<j<n

from numpy import *
mymatrix = mat([[1,2,3],[4,5,6],[7,8,9,]])
print sum(mymatrix)

輸出結果: 45

(4)矩陣各元素的積

數學公式:(A×B)i,j=Ai,j×Bi,j

(A\times B)_{i,j} = A_{i,j}\times B_{i,j}

from numpy import *
mymatrix1 = mat([[1,2,3],[4,5,6],[7,8,9,]])
mymatrix2 = 1.5*ones([3,3])
print multiply(mymatrix1,mymatrix2)

輸出結果:

[[  1.5   3.    4.5]
 [  6.    7.5   9. ]
 [ 10.5  12.   13.5]]

(5)矩陣各元素的n次冪:例如 n = 2

數學公式:A2i,j=Ai,j×Ai,j

A_{i,j}^{2} = A_{i,j}\times A_{i,j}

from numpy import *
mymatrix1 = mat([[1,2,3],[4,5,6],[7,8,9,]])
print power(mymatrix1,2)

輸出結果:

[[ 1  4  9]
 [16 25 36]
 [49 64 81]]

3. 矩陣的乘法

數學公式:
[A,B]i,j=Ai,1×B1,j+Ai,2×B2,j++Ai,n×Bn,j

[A,B]_{i,j} = A_{i,1}\times B_{1,j}+A_{i,2}\times B_{2,j}+\cdots +A_{i,n}\times B_{n,j}

=nr=1Ai,rBr,j
=\sum_{r=1}^{n}A_{i,r}B_{r,j}

from numpy import *
mymatrix1 = mat([[1,2,3],[4,5,6],[7,8,9,]])
mymatrix2 = mat([[1],[2],[3]])
print mymatrix1*mymatrix2

輸出結果:

[[14]
 [32]
 [50]]

4. 矩陣的轉置

數學公式:(AT)i,j=Aj,i

(A^{T})_{i,j}=A_{j,i}

# -*- coding: utf-8 -*-
from numpy import *
mymatrix = mat([[1,2,3],[4,5,6],[7,8,9,]])
print mymatrix.T         # 矩陣的轉置
mymatrix.transpose()     # 矩陣的轉置
print mymatrix

輸出結果:

[[1 4 7]
 [2 5 8]
 [3 6 9]]
[[1 2 3]
 [4 5 6]
 [7 8 9]]

5. 矩陣的其他操作:行列數、切片、複製、比較

# -*- coding: utf-8 -*-
from numpy import *
mymatrix = mat([[1,2,3],[4,5,6],[7,8,9,]])
[m,n] = shape(mymatrix)
print "矩陣的行數和列數:",m,n

mysc11 = mymatrix[0]     #按行切片
print "按行切片:",mysc11

mysc12 = mymatrix.T[0]     #按列切片
print "按列切片:",mysc12

mycpmat = mymatrix.copy()   #矩陣的複製
print "複製矩陣:\n",mycpmat

#比較
print "矩陣元素的比較:\n",mymatrix < mymatrix.T

輸出結果:

矩陣的行數和列數: 3 3
按行切片: [[1 2 3]]
按列切片: [[1 4 7]]
複製矩陣:
[[1 2 3]
 [4 5 6]
 [7 8 9]]
矩陣元素的比較:
[[False  True  True]
 [False False  True]
 [False False False]]

相關文章