python矩阵教程_numpy教程:矩阵matrix及其运算
numpy矩陣簡介
NumPy函數庫中存在兩種不同的數據類型(矩陣matrix和數組array),都可以用于處理行列表示的數字元素。雖然它們看起來很相似,但是在這兩個數據類型上執行相同的數學運算可能得到不同的結果,其中NumPy函數庫中的matrix與MATLAB中matrices等價。
numpy模塊中的矩陣對象為numpy.matrix,包括矩陣數據的處理,矩陣的計算,以及基本的統計功能,轉置,可逆性等等,包括對復數的處理,均在matrix對象中。
關于numpy中矩陣和二維數組的取舍
matrix是array的分支,matrix和array在很多時候都是通用的,但官方建議如果兩個可以通用,那就選擇array,因為array更靈活,速度更快,很多人把二維的array也翻譯成矩陣。
matrix的優勢就是相對簡單的運算符號,如矩陣相乘用符號*,但是array相乘得用方法.dot()。
Note: array * mat也是矩陣相乘,而不是點乘。
array的優勢就是不僅僅表示二維,還能表示3、4、5...維,而且在大部分Python程序里,array也是更常用的。
Note:
1. numpy中二維數組不支持求逆運算(給gui),但可以使用scripy中的linalg.inv()函數求逆。
2. lz建議使用二維ndarray代替matrix,結合使用scripy.linalg庫可以實現全部矩陣運算。[Scipy教程 - 線性代數庫linalg]
Matrix objects矩陣對象
創建示例
np.matrix
>>> a = np.matrix(’1 2; 3 4’)
>>> print a
[[1 2]
[3 4]]
>>> np.matrix([[1, 2], [3, 4]])
matrix([[1, 2],
[3, 4]])
Note:
1. class numpy.matrix(data,dtype,copy):返回一個矩陣,其中data為ndarray對象或者字符形式;dtype:為data的type;copy:為bool類型。
2. 矩陣的換行必須是用分號(;)隔開,內部數據必須為字符串形式(‘ ’),矩陣的元素之間必須以空格隔開。
3. 矩陣中的data可以為數組對象。
np.asmatrix
>>> x = np.array([[1, 2], [3, 4]])
>>> m = np.asmatrix(x)
>>> x[0,0] = 5
>>> m
matrix([[5, 2],
[3, 4]])
矩陣對象屬性Attribute
矩陣對象方法Methods
[numpy-ref-1.8.1 - 1.6.2 Matrix objects p120]
Matrix矩陣對象方法使用示例
>>> a = np.asmatrix('0 2 7; 3 4 8; 5 0 9')
>>> a.all()
False
>>> a.all(axis=0)
matrix([[False, False,? True]], dtype=bool)
>>> a.all(axis=1)
matrix([[False],
[ True],
[False]], dtype=bool)
ü? Astype方法
>>> a.astype(float)
matrix([[ 12.,?? 3.,?? 5.],
[ 32.,? 23.,?? 9.],
[ 10., -14.,? 78.]])
ü? Argsort方法
>>> a=np.matrix('12 3 5; 32 23 9; 10 -14 78')
>>> a.argsort()
matrix([[1, 2, 0],
[2, 1, 0],
[1, 0, 2]])
ü? Clip方法
>>> a
matrix([[ 12,?? 3,?? 5],
[ 32,? 23,?? 9],
[ 10, -14,? 78]])
>>> a.clip(12,32)
matrix([[12, 12, 12],
[32, 23, 12],
[12, 12, 32]])
ü? Cumprod方法
>>> a.cumprod(axis=1)
matrix([[??? 12,???? 36,??? 180],
[??? 32,??? 736,?? 6624],
[??? 10,?? -140, -10920]])
ü? Cumsum方法
>>> a.cumsum(axis=1)
matrix([[12, 15, 20],
[32, 55, 64],
[10, -4, 74]])
ü? Tolist方法
>>> b.tolist()
[[12, 3, 5], [32, 23, 9], [10, -14, 78]]
ü? Tofile方法
>>> b.tofile('d:\\b.txt')
ü? compress()方法
>>> from numpy import *
>>> a = array([10, 20, 30, 40])
>>> condition = (a > 15) & (a < 35)
>>> condition
array([False, True, True, False], dtype=bool)
>>> a.compress(condition)
array([20, 30])
>>> a[condition]????????????????????????????????????? # same effect
array([20, 30])
>>> compress(a >= 30, a)????????????????????????????? # this form a
so exists
array([30, 40])
>>> b = array([[10,20,30],[40,50,60]])
>>> b.compress(b.ravel() >= 22)
array([30, 40, 50, 60])
>>> x = array([3,1,2])
>>> y = array([50, 101])
>>> b.compress(x >= 2, axis=1)?????????????????????? # illustrates
the use of the axis keyword
array([[10, 30],
[40, 60]])
>>> b.compress(y >= 100, axis=0)
array([[40, 50, 60]])
The Matrix class numpy矩陣類
建立矩陣
Note: numpy.mat(data, dtype=None)?? Interpret the input as a matrix.
Unlike matrix, asmatrix does not make a copy if the input is already a matrix or an ndarray. Equivalent to matrix(data, copy=False).
[numpy-ref-1.8.1 - 3.1.7 The Matrix class p484]
Matrix library矩陣庫(numpy.matlib)
This module contains all functions in the numpy namespace, with the following replacement functions that return matrices instead of ndarrays.
Functions that are also in the numpy namespace and return matrices
Replacement functions in matlib
[numpy-ref-1.8.1 - 3.21 Matrix library p940]
總結
以上是生活随笔為你收集整理的python矩阵教程_numpy教程:矩阵matrix及其运算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 早报:阿里巴巴公布三季度财报 苹果将支持
- 下一篇: python比较三个数_python经典