数据科学-矩阵创建运算之NumPy库

Python中主要有两种表示矩阵的方法,一种是matrix类,另一种是二维array,主要区别在于默认的乘法不同,前者默认乘法是矩阵的乘法,后者默认乘法是Hadamard乘法。实际情况中我们使用后一种也就是二维array表示矩阵。

你可以用NumPy提供的专门的函数创建特殊的矩阵,也可以像二维数组那样提取矩阵中的某个元素或某行某列

1.矩阵创建-NumPy

In [1]: import numpy as np

In [2]: from numpy.linalg import inv

In [3]: #创建矩阵

In [4]: A = np.martrix([[1,2],[3,4],[5,6]])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-5f430326d354> in <module>()
----> 1 A = np.martrix([[1,2],[3,4],[5,6]])

AttributeError: module 'numpy' has no attribute 'martrix'

In [5]: A = np.matrix([[1,2],[3,4],[5,6]])

In [6]: print(A)
[[1 2]
 [3 4]
 [5 6]]

In [8]: B = np.array(range(1,7)).reshape(3,2)

In [9]: print(B)
[[1 2]
 [3 4]
 [5 6]]

In [10]: A*A
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-cd6673b04447> in <module>()
----> 1 A*A

H:\Software\Anaconda\lib\site-packages\numpy\matrixlib\defmatrix.py in __mul__(self, other)
    307         if isinstance(other, (N.ndarray, list, tuple)) :
    308             # This promotes 1-D vectors to row vectors
--> 309             return N.dot(self, asmatrix(other))
    310         if isscalar(other) or not hasattr(other, '__rmul__') :
    311             return N.dot(self, other)

ValueError: shapes (3,2) and (3,2) not aligned: 2 (dim 1) != 3 (dim 0)

In [11]: B*B
Out[11]: 
array([[ 1,  4],
       [ 9, 16],
       [25, 36]])

In [12]: #创建特殊矩阵

In [13]: np.zeros((3,2))
Out[13]: 
array([[0., 0.],
       [0., 0.],
       [0., 0.]])

In [14]: np.identity(3)
Out[14]: 
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

In [15]: np.diag([1,2,3])
Out[15]: 
array([[1, 0, 0],
       [0, 2, 0],
       [0, 0, 3]])

In [16]: #矩阵向量提取

In [17]: m = np.array(range(1,10)).reshape(3,3)

In [18]: print(m)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

In [19]: m[[0,2]]
Out[19]: 
array([[1, 2, 3],
       [7, 8, 9]])

In [20]: #提取列向量

In [21]: m[:,[1,2]] #或者 m[:,[False,True,False]]
Out[21]: 
array([[2, 3],
       [5, 6],
       [8, 9]])

2. 矩阵的加减运算以及与数字的乘积与数字运算一样。当矩阵用二维array表示时,直接的乘号表示Hadamard乘积,矩阵的乘法需要使用dot函数。矩阵的转置由transpose函数完成,而逆矩阵由inv函数完成。

In [23]: import numpy as np

In [24]: from numpy.linalg import inv

In [25]: #矩阵的运算

In [26]: n = np.array(range(1,5)).reshape(2,2)

In [27]: n
Out[27]: 
array([[1, 2],
       [3, 4]])

In [28]: np.transpose(n)
Out[28]: 
array([[1, 3],
       [2, 4]])

In [29]: n+n
Out[29]: 
array([[2, 4],
       [6, 8]])

In [30]: n-n
Out[30]: 
array([[0, 0],
       [0, 0]])

In [31]: 3*n
Out[31]: 
array([[ 3,  6],
       [ 9, 12]])

In [32]: #Hadamard乘积

In [33]: n*n
Out[33]: 
array([[ 1,  4],
       [ 9, 16]])

In [34]: #矩阵乘积

In [35]: n.dot(n)
Out[35]: 
array([[ 7, 10],
       [15, 22]])

In [36]: #矩阵的逆矩阵

In [37]: inv(n)
Out[37]: 
array([[-2. ,  1. ],
       [ 1.5, -0.5]])

In [38]: np.dot(inv(n),n)
Out[38]: 
array([[1.0000000e+00, 4.4408921e-16],
       [0.0000000e+00, 1.0000000e+00]])




猜你喜欢

转载自blog.csdn.net/weiyi99999/article/details/80504488