运算库之numpy(矩阵运算)

矩阵的加减法
    要求:相互计算的矩阵之间的shape相同。
    方法一:ndaray相加减
    方法二:matrix类型数据转换,然后相加减
矩阵的乘法
    要求:相互计算的矩阵之间存在要求及A * B,则A的shape为(m,n),B的shape为(n,l)
    方法一:ndarray计算采用点乘方法np.dot()
    方法二:matrix可以直接使用“*”
矩阵的转置
    行变成列,列称为行
    ndarray的T方法
矩阵的逆运算
    方阵的可逆阵
    np.linalg.inv()
    非方阵的伪阵
    np.linalg.pinv()

矩阵的加减法

代码:

import numpy as np

np.random.seed(22)
data1 = np.arange(20).reshape(4, 5)
data2 = np.random.random(size=20).reshape(4, 5)

# 方法一:
print('data1:\n', data1)
print('data2:\n', data2)
print('data1+data2:\n', data1 + data2)
print('data1+data2:\n',data1 - data2)

# 方法二:
data1_mat = np.mat(data1)
data2_mat = np.mat(data2)
print('data1_mat+data2_mat:\n', data1 + data2)
print('data1_mat+data2_mat:\n',data1 - data2)

输出:

data1:
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
data2:
 [[0.20846054 0.48168106 0.42053804 0.859182   0.17116155]
 [0.33886396 0.27053283 0.69104135 0.22040452 0.81195092]
 [0.01052687 0.5612037  0.81372619 0.7451003  0.18911136]
 [0.00614087 0.77204387 0.95783217 0.70193788 0.29757827]]
data1+data2:
 [[ 0.20846054  1.48168106  2.42053804  3.859182    4.17116155]
 [ 5.33886396  6.27053283  7.69104135  8.22040452  9.81195092]
 [10.01052687 11.5612037  12.81372619 13.7451003  14.18911136]
 [15.00614087 16.77204387 17.95783217 18.70193788 19.29757827]]
data1+data2:
 [[-0.20846054  0.51831894  1.57946196  2.140818    3.82883845]
 [ 4.66113604  5.72946717  6.30895865  7.77959548  8.18804908]
 [ 9.98947313 10.4387963  11.18627381 12.2548997  13.81088864]
 [14.99385913 15.22795613 16.04216783 17.29806212 18.70242173]]
data1_mat+data2_mat:
 [[ 0.20846054  1.48168106  2.42053804  3.859182    4.17116155]
 [ 5.33886396  6.27053283  7.69104135  8.22040452  9.81195092]
 [10.01052687 11.5612037  12.81372619 13.7451003  14.18911136]
 [15.00614087 16.77204387 17.95783217 18.70193788 19.29757827]]
data1_mat+data2_mat:
 [[-0.20846054  0.51831894  1.57946196  2.140818    3.82883845]
 [ 4.66113604  5.72946717  6.30895865  7.77959548  8.18804908]
 [ 9.98947313 10.4387963  11.18627381 12.2548997  13.81088864]
 [14.99385913 15.22795613 16.04216783 17.29806212 18.70242173]]

矩阵的乘法 

代码:

import numpy as np

data1 = np.arange(20).reshape(4, 5)
data2 = np.arange(20).reshape(5, 4)

# 方法一:
print('data1:\n', data1)
print('data2:\n', data2)
mul = np.dot(data1, data2)
print('data1 * data2:\n', mul)

# 方法二:
print('data1 * data2:\n', data1 @ data2)

# 方法三:
data1_mat = np.mat(data1)
data2_mat = np.mat(data2)
print('data1_mat+data2_mat:\n', data1_mat * data2_mat)

输出:

data1:
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
data2:
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]]
data1 * data2:
 [[120 130 140 150]
 [320 355 390 425]
 [520 580 640 700]
 [720 805 890 975]]
data1 * data2:
 [[120 130 140 150]
 [320 355 390 425]
 [520 580 640 700]
 [720 805 890 975]]
data1_mat+data2_mat:
 [[120 130 140 150]
 [320 355 390 425]
 [520 580 640 700]
 [720 805 890 975]]

矩阵的转置

代码: 

import numpy as np

data = np.arange(20).reshape(4, 5)

# 方法一:
print('data:\n', data)
print('矩阵的转置:\n', data.T)

# 方法二:
data_mat = np.mat(data)
print('矩阵的转置:\n', data_mat.T)

 输出:

data:
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
矩阵的转置:
 [[ 0  5 10 15]
 [ 1  6 11 16]
 [ 2  7 12 17]
 [ 3  8 13 18]
 [ 4  9 14 19]]
矩阵的转置:
 [[ 0  5 10 15]
 [ 1  6 11 16]
 [ 2  7 12 17]
 [ 3  8 13 18]
 [ 4  9 14 19]]

 矩阵的逆

代码:

import numpy as np
from numpy.linalg import pinv, inv

# 非方矩阵的伪逆
data = np.arange(20).reshape(4, 5)
print(data)
data_p = pinv(data)
print(data_p)
print(np.dot(data, data_p))

# 方矩阵的逆
data = np.arange(1, 17).reshape(4, 4)
data_p = pinv(data)
print(data_p)

输出:

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
[[-0.176 -0.092 -0.008  0.076]
 [-0.094 -0.048 -0.002  0.044]
 [-0.012 -0.004  0.004  0.012]
 [ 0.07   0.04   0.01  -0.02 ]
 [ 0.152  0.084  0.016 -0.052]]
[[ 0.7  0.4  0.1 -0.2]
 [ 0.4  0.3  0.2  0.1]
 [ 0.1  0.2  0.3  0.4]
 [-0.2  0.1  0.4  0.7]]
[[-0.285  -0.145  -0.005   0.135 ]
 [-0.1075 -0.0525  0.0025  0.0575]
 [ 0.07    0.04    0.01   -0.02  ]
 [ 0.2475  0.1325  0.0175 -0.0975]]

学习地址:

 黑马程序员Python教程,4天快速入门Python数据挖掘,系统精讲+实战案例_哔哩哔哩_bilibili

你可能感兴趣的:(数据挖掘三剑客,矩阵,python,numpy,数据分析,数据挖掘)