1. 程式人生 > >Numpy系列(十二)- 矩陣運算

Numpy系列(十二)- 矩陣運算

提取 是否 相同 val 小數 pos eat sub 矩陣存儲

numpy模塊中的矩陣對象為numpy.matrix,包括矩陣數據的處理,矩陣的計算,以及基本的統計功能,轉置,可逆性等等,包括對復數的處理,均在matrix對象中。 class numpy.matrix(data,dtype,copy):返回一個矩陣,其中data為ndarray對象或者字符形式;dtype:為data的type;copy:為bool類型。

>>> a = np.matrix(‘1 2 7; 3 4 8; 5 6 9‘)
>>> a             #矩陣的換行必須是用分號(;)隔開,內部數據必須為字符串形式(‘ ’),矩
matrix([[1, 2, 7],       #陣的元素之間必須以空格隔開。
[3, 4, 8],
[5, 6, 9]])

>>> b=np.array([[1,5],[3,2]])
>>> x=np.matrix(b)   #矩陣中的data可以為數組對象。
>>> x
matrix([[1, 5],
[3, 2]])

矩陣對象的屬性:

  • matrix.T transpose:返回矩陣的轉置矩陣
  • matrix.H hermitian (conjugate) transpose:返回復數矩陣的共軛元素矩陣
  • matrix.I inverse:返回矩陣的逆矩陣
  • matrix.A base array:返回矩陣基於的數組
  • 矩陣對象的方法:
  • all([axis, out]) :沿給定的軸判斷矩陣所有元素是否為真(非0即為真)
  • any([axis, out]) :沿給定軸的方向判斷矩陣元素是否為真,只要一個元素為真則為真。
  • argmax([axis, out]) :沿給定軸的方向返回最大元素的索引(最大元素的位置).
  • argmin([axis, out]): 沿給定軸的方向返回最小元素的索引(最小元素的位置)
  • argsort([axis, kind, order]) :返回排序後的索引矩陣
  • astype(dtype[, order, casting, subok, copy]):將該矩陣數據復制,且數據類型為指定的數據類型
  • byteswap(inplace) Swap the bytes of the array elements
  • choose(choices[, out, mode]) :根據給定的索引得到一個新的數據矩陣(索引從choices給定)
  • clip(a_min, a_max[, out]) :返回新的矩陣,比給定元素大的元素為a_max,小的為a_min
  • compress(condition[, axis, out]) :返回滿足條件的矩陣
  • conj() :返回復數的共軛復數
  • conjugate() :返回所有復數的共軛復數元素
  • copy([order]) :復制一個矩陣並賦給另外一個對象,b=a.copy()
  • cumprod([axis, dtype, out]) :返回沿指定軸的元素累積矩陣
  • cumsum([axis, dtype, out]) :返回沿指定軸的元素累積和矩陣
  • diagonal([offset, axis1, axis2]) :返回矩陣中對角線的數據
  • dot(b[, out]) :兩個矩陣的點乘
  • dump(file) :將矩陣存儲為指定文件,可以通過pickle.loads()或者numpy.loads()如:a.dump(‘d:\\a.txt’)
  • dumps() :將矩陣的數據轉存為字符串.
  • fill(value) :將矩陣中的所有元素填充為指定的value
  • flatten([order]) :將矩陣轉化為一個一維的形式,但是還是matrix對象
  • getA() :返回自己,但是作為ndarray返回
  • getA1():返回一個扁平(一維)的數組(ndarray)
  • getH() :返回自身的共軛復數轉置矩陣
  • getI() :返回本身的逆矩陣
  • getT() :返回本身的轉置矩陣
  • max([axis, out]) :返回指定軸的最大值
  • mean([axis, dtype, out]) :沿給定軸方向,返回其均值
  • min([axis, out]) :返回指定軸的最小值
  • nonzero() :返回非零元素的索引矩陣
  • prod([axis, dtype, out]) :返回指定軸方型上,矩陣元素的乘積.
  • ptp([axis, out]) :返回指定軸方向的最大值減去最小值.
  • put(indices, values[, mode]) :用給定的value替換矩陣本身給定索引(indices)位置的值
  • ravel([order]) :返回一個數組,該數組是一維數組或平數組
  • repeat(repeats[, axis]) :重復矩陣中的元素,可以沿指定軸方向重復矩陣元素,repeats為重復次數
  • reshape(shape[, order]) :改變矩陣的大小,如:reshape([2,3])
  • resize(new_shape[, refcheck]) :改變該數據的尺寸大小
  • round([decimals, out]) :返回指定精度後的矩陣,指定的位數采用四舍五入,若為1,則保留一位小數
  • searchsorted(v[, side, sorter]) :搜索V在矩陣中的索引位置
  • sort([axis, kind, order]) :對矩陣進行排序或者按軸的方向進行排序
  • squeeze([axis]) :移除長度為1的軸
  • std([axis, dtype, out, ddof]) :沿指定軸的方向,返回元素的標準差.
  • sum([axis, dtype, out]) :沿指定軸的方向,返回其元素的總和
  • swapaxes(axis1, axis2):交換兩個軸方向上的數據.
  • take(indices[, axis, out, mode]) :提取指定索引位置的數據,並以一維數組或者矩陣返回(主要取決axis)
  • tofile(fid[, sep, format]) :將矩陣中的數據以二進制寫入到文件
  • tolist() :將矩陣轉化為列表形式
  • tostring([order]):將矩陣轉化為python的字符串.
  • trace([offset, axis1, axis2, dtype, out]):返回對角線元素之和
  • transpose(*axes) :返回矩陣的轉置矩陣,不改變原有矩陣
  • var([axis, dtype, out, ddof]) :沿指定軸方向,返回矩陣元素的方差
  • view([dtype, type]) :生成一個相同數據,但是類型為指定新類型的矩陣。

代碼示例

>>> 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]])

  

Numpy系列(十二)- 矩陣運算