numpy学习
1.矩阵生成
def generate_matrix():
# 1. 一般矩阵
arr_normal = np.array([(1, 2, 3), [4, 5, 6], [7, 8, 9]], dtype=np.int64) # 如果不写64就会提示报错,可以写成int_
# 2. 零矩阵
arr_zeros = np.zeros((2, 3), dtype=np.int_) # int_带下标表示使用默认的
# 3. 全1矩阵
arr_ones = np.ones([2, 3]) # 矩阵使用圆括号或方括号都可以
# 3.1. 单位矩阵
arr_eye = np.eye(3)
# 4. 生成一个非常接近0的矩阵
arr_empty = np.empty((2, 3), dtype=np.int_)
# 5.生成一维等差数列
arr_arange = np.arange(0, 10, 2) # arange([start,] stop[, step,], dtype=None, *, like=None), # 方括号的表示可选,有无都可以。
# 6.将一个区间划分为n个
arr_linspace = np.linspace(0, 10, 5, dtype=np.float32)
# 7. 随机生成(0-1)的矩阵
arr_random = np.random.random((2, 3))
# 8.reshape将矩阵变形
arr_reshape = np.arange(0, 12).reshape(3, 4)
2.矩阵运算
def matrix_compute():
arr_1 = np.array((1, 2, 3))
arr_2 = np.arange(1, 4) # [1 2 3]
arr_3 = np.arange(0, 4) # [0 1 2 3]
arr_4 = np.arange(4, 8) # [4 5 6 7]
# 1. 加减运算,直接运算即可
result_add = arr_1 + arr_2 # [2 4 6]
# 2. 里边每一项幂运算
result_exp = arr_1 ** 2 # [1 4 9]
# 3. 三角函数
result_tri = np.sin(arr_1) # [0.84147098 0.90929743 0.14112001]
# 4. 判断里边的元素
result_compare = (arr_1 < 2) # [ True False False]
# 5. 矩阵相同位置的数相乘
result_multi = arr_3.reshape(2, 2) * arr_4.reshape(2, 2)
# 5. 矩阵相乘, 两个方法都可以
result_dot = np.dot(arr_3.reshape(2, 2), arr_4.reshape(2, 2))
result_dot2 = arr_3.reshape(2, 2).dot(arr_4.reshape(2, 2))
# 7. 求和、平均值、极大值、极小值(默认是所有元素,可以指定axis),
result_sum = np.sum(arr_3.reshape(2, 2)) # 矩阵所有元素
result_mean = np.mean(arr_3.reshape(2, 2)) # 1.5
result_average = np.average(arr_3.reshape(2, 2)) # 1.5
result_min = np.min(arr_3) # 矩阵所有元素
result_max1 = np.max(arr_3.reshape(2, 2), axis=0) # 以列为单位
result_max2 = np.max(arr_3.reshape(2, 2), axis=1) # 以行为单位
# 8. 求最大值、最小值索引
result_index_min = np.argmin(arr_3.reshape(2, 2)) # 0
result_index_max = np.argmax(arr_3) # 3
# 9. 中位数
result_median = np.median(arr_3) # 1.5
# 10, 排序, 以行为单位
result_sort = np.sort(np.random.random((2, 2)).reshape(2, 2))
# 11. 矩阵转置
result_turn1 = np.transpose(arr_3.reshape(2, 2))
result_turn2 = arr_3.reshape(2, 2).T
# 12. 使超出范围的元素等于边界值
result_clip1 = arr_3.clip(1, 2)
result_clip2 = np.clip(arr_3, 1, 2)
# 13.替换
arr_1[0] = 10 # [10 2 3]
arr_1[0:2] = [20, 30] # [20 30 3]
# 14. 深度拷贝、复制
arr_5 = arr_4.copy() # 深度拷贝,要是直接赋值的话,arr_4 和 arr_5指向的地址一样
# 15. 求方阵的迹, 即主对角元素之和
arr_trace = np.trace(arr_3.reshape(2, 2))
# 16. 求行列式
arr_det = np.linalg.det(arr_3.reshape(2, 2))
# 17. 求逆矩阵
arr_inv = np.linalg.inv(arr_3.reshape(2, 2))
arr_pinv = np.linalg.pinv(arr_3.reshape(2, 2)) # 伪逆算法,不管矩阵可不可逆,都可以算出它的逆矩阵。
# 18. 解多元方程
arr_A = np.array([[1, 2, 1], [2, -1, 3], [3, 1, 2]], dtype=np.float_)
arr_y = np.array([7, 7, 18], dtype=np.float_)
arr_solve = np.linalg.solve(arr_A, arr_y)
3.矩阵输出
def matrix_output():
arr_5 = np.array([[8, 5, 4], [7, 9, 3]])
output1 = (arr_5[0, 1:3]) # [5 4]
output_row = arr_5[0] # [8 5 4]
# 查看形状
shape = arr_5.shape
# 遍历行
for row in arr_5: pass
# 遍历列
for column in arr_5.T: pass
# 遍历每一个元素
for member in arr_5.flat: pass # arr_5.flat是一个迭代器
arr_5.flatten() # 将矩阵变为行向量
4.矩阵合成分割
def matrix_stack():
arr_5 = np.array([[8, 5, 4], [7, 9, 3]])
arr_6 = np.arange(6).reshape(2, 3)
# 1. 纵向合并
result_vstack = np.vstack((arr_5, arr_6)) # vertical stack
# 2. 横向合并
result_hstack = np.hstack((arr_5, arr_6)) # horizontal stack
# 3.分割
result_split_column = np.vsplit(result_vstack, 2) # 纵向分割,上下分割
result_split_row = np.hsplit(result_hstack, 2) # 横向分割,左右分割
分割
import numpy as np
x = np.array([[0., 1., 2., 3., 4., 5., 6., 7.],
[8., 9., 10., 11., 12., 13., 14., 15.]])
b = np.hsplit(x, [1, 4, 6]) # 返回的是按列分割之后的列表 [2,4,6]分别代表分按列分割的索引位置
for i in b:
print(i, end='\n------\n')
结果:
[[0.]
[8.]]
------
[[ 1. 2. 3.]
[ 9. 10. 11.]]
------
[[ 4. 5.]
[12. 13.]]
------
[[ 6. 7.]
[14. 15.]]
------