Scipy教程 - 线性代数库linalg

版权声明:本文为博主皮皮http://blog.csdn.net/pipisorry原创文章,未经博主允许不得转载。 https://blog.csdn.net/pipisorry/article/details/48791403

http://blog.csdn.net/pipisorry/article/details/48791403

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]

扫描二维码关注公众号,回复: 3848757 查看本文章

皮皮Blog



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

皮皮Blog



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]

皮皮Blog


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]

from:http://blog.csdn.net/pipisorry/article/details/48791403


猜你喜欢

转载自blog.csdn.net/pipisorry/article/details/48791403
今日推荐