Files
task-3-1-3-Matrix-Fundament…/4-16 2509165048.py
2509165048 9ca1bb4630 3-1-3
2026-04-16 16:04:08 +08:00

139 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import numpy as np
image = np.array([
[255, 200, 150, 100],
[180, 140, 110, 80],
[120, 90, 60, 40],
[50, 30, 20, 10]
])
color_image = np.array([
[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
[[255, 255, 0], [0, 255, 255], [255, 0, 255]],
[[128, 128, 128],[100, 100, 100],[50, 50, 50]]
])
vec = np.array([1, 2, 3, 4, 5])
print(vec)
mat = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(mat)
# 全0数组 - 常用于初始化
zeros = np.zeros((3, 4)) # 3行4列的全0矩阵
print(zeros)
# [[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]
# 全1数组
ones = np.ones((2, 3))
# [[1. 1. 1.]
# [1. 1. 1.]]
# 指定范围内的数组
arr = np.arange(0, 10, 2) # 0到10步长2
print(arr) # [0 2 4 6 8]
# 等差数组
lin = np.linspace(0, 1, 5) # 0到1之间均匀取5个数
print(lin) # [0. 0.25 0.5 0.75 1. ]
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.ndim) # 维度数量 = 2是二维数组/矩阵)
print(a.shape) # 形状 = (2, 3) 2行3列
print(a.size) # 总元素数 = 6
print(a.dtype) # 数据类型 = int64
# 对于图像数据,常用 uint8无符号8位整数0-255
image = np.array([[255, 128], [64, 0]], dtype=np.uint8)
print(image.dtype) # uint8
a = np.array([10, 20, 30, 40, 50])
print(a[0]) # 第一个元素 = 10
print(a[-1]) # 最后一个元素 = 50
print(a[1:3]) # 第2到第3个元素 = [20, 30]
print(a[:3]) # 前3个元素 = [10, 20, 30]
print(a[2:]) # 第3个到最后 = [30, 40, 50]
mat = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
])
# 获取特定元素
print(mat[0, 0]) # 第1行第1列 = 1
print(mat[1, 2]) # 第2行第3列 = 7
print(mat[2, -1]) # 第3行最后一列 = 12
# 切片:[行, 列]
print(mat[0, :]) # 第1行所有列 = [1, 2, 3, 4]
print(mat[:, 1]) # 所有行第2列 = [2, 6, 10]
print(mat[0:2, 0:2]) # 取前2行前2列
# [[1 2]
# [5 6]]
# 负索引
print(mat[-1, :]) # 最后一行 = [9, 10, 11, 12]
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# 加减乘除(对应元素运算)
print(a + b) # [5 7 9]
print(a - b) # [-3 -3 -3]
print(a * b) # [4 10 18] 注意:这是元素乘法,不是矩阵乘法
print(a / b) # [0.25 0.4 0.5]
# 标量运算(广播)
print(a * 2) # [2 4 6]
print(a + 10) # [11 12 13]
print(a ** 2) # [1 4 9] 平方
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# 使用 @ 运算符 或 np.dot()
C = A @ B
print(C)
# [[19 22]
# [41 46]]
# 或者
C = np.dot(A, B)
print(C)
a = np.array([1, 2, 3, 4, 5])
print(np.sum(a)) # 求和 = 15
print(np.mean(a)) # 平均值 = 3.0
print(np.max(a)) # 最大值 = 5
print(np.min(a)) # 最小值 = 1
print(np.std(a)) # 标准差
print(np.argmax(a)) # 最大值的索引 = 4
print(np.argmin(a)) # 最小值的索引 = 0
# 对于二维数组,可以指定轴
mat = np.array([[1, 2, 3], [4, 5, 6]])
print(np.sum(mat, axis=0)) # 按列求和 = [5 7 9]
print(np.sum(mat, axis=1)) # 按行求和 = [6 15]
a = np.array([1, 2, 3, 4, 5, 6])
# 展平flatten: 多维变一维
print(a.flatten()) # [1 2 3 4 5 6]
# 改变形状reshape
b = a.reshape(2, 3) # 变成2行3列
print(b)
# [[1 2 3]
# [4 5 6]]
# 转置transpose: 行变列
print(b.T)
# [[1 4]
# [2 5]
# [3 6]]
# 翻转
c = np.array([[1, 2, 3], [4, 5, 6]])
print(np.fliplr(c)) # 左右翻转
# [[3 2 1]
# [6 5 4]]
print(np.flipud(c)) # 上下翻转
# [[4 5 6]
# [1 2 3]]