Files
task-3-1-3-Matrix-Fundament…/40吴承恩.py
2026-04-16 15:56:50 +08:00

132 lines
3.8 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.

# ==========================================
# 练习1图像矩阵操作 (3×3灰度图像)
# ==========================================
import numpy as np
# 定义3×3灰度图像
print("===== 练习1开始 =====")
image = np.array([
[100, 150, 200],
[80, 120, 180],
[60, 90, 140]
], dtype=np.uint8)
print("原图: ")
print(image)
# 1. 变暗20每个像素值减20
image_dark = image - 20
print("\n变暗20后: ")
print(image_dark)
# 2. 裁剪左上角保留2×2区域
image_crop = image[0:2, 0:2]
print("\n裁剪左上角后: ")
print(image_crop)
# 3. 水平翻转使用np.fliplr()
image_flip = np.fliplr(image)
print("\n水平翻转后: ")
print(image_flip)
print("===== 练习1结束 =====\n")
# ==========================================
# 练习2看图写代码 (4×4图像矩阵)
# ==========================================
print("===== 练习2开始 =====")
# 定义4×4图像矩阵
img = np.array([
[255, 255, 0, 0],
[255, 255, 0, 0],
[0, 0, 255, 255],
[0, 0, 255, 255]
], dtype=np.uint8)
print("原始4x4图像:")
print(img)
# 1. 统计白色(255)和黑色(0)像素数量
white_count = np.sum(img == 255)
black_count = np.sum(img == 0)
print(f"\n白色像素(255)数量: {white_count}")
print(f"黑色像素(0)数量: {black_count}")
# 2. 水平翻转并打印
img_flipped = np.fliplr(img)
print("\n水平翻转后的图像:")
print(img_flipped)
# 3. 逆时针旋转90度 (转置 + 上下翻转)
# 方法:先转置(.T),再上下翻转(np.flipud)
img_rotated = np.flipud(img.T)
print("\n逆时针旋转90度后的图像:")
print(img_rotated)
print("===== 练习2结束 =====\n")
# ==========================================
# 练习3特征向量计算
# ==========================================
print("===== 练习3开始 =====")
# 定义两个特征图
feature_map1 = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]])
feature_map2 = np.array([[1, 1, 1], [1, 0, 0], [1, 0, 0]])
# 展平为向量
vector1 = feature_map1.flatten()
vector2 = feature_map2.flatten()
print("feature_map1 展平向量 vector1: ", vector1)
print("feature_map2 展平向量 vector2: ", vector2)
# 1. 计算欧几里得距离
# 公式: sqrt(sum((a-b)^2))
euclidean_dist = np.linalg.norm(vector1 - vector2)
print(f"\nvector1 和 vector2 的欧几里得距离: {euclidean_dist:.4f}")
# 2. 计算余弦相似度
# 公式: dot(a,b) / (||a|| * ||b||)
cos_sim = np.dot(vector1, vector2) / (np.linalg.norm(vector1) * np.linalg.norm(vector2))
print(f"vector1 和 vector2 的余弦相似度: {cos_sim:.4f}")
print("===== 练习3结束 =====\n")
# ==========================================
# 练习4文本向量化 (预习挑战)
# ==========================================
print("===== 练习4开始 =====")
# 原始词汇表
vocab = ["Python", "学习", "数据", "人工智能", "编程"]
# 两句话
doc1 = "Python学习编程"
doc2 = "Python人工智能数据"
# 定义向量化函数
def text_to_vector(text, vocab):
words = text.split()
vector = np.zeros(len(vocab))
for i, word in enumerate(vocab):
vector[i] = words.count(word)
return vector
# 生成向量
v1 = text_to_vector(doc1, vocab)
v2 = text_to_vector(doc2, vocab)
print("doc1向量: ", v1)
print("doc2向量: ", v2)
# 2. 计算余弦相似度
cos_sim_text = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
print(f"\ndoc1 和 doc2 的余弦相似度: {cos_sim_text:.4f}")
# 3. 修改vocab加入"机器"测试doc3 = "机器学习"
print("\n--- 修改词汇表并测试 doc3 ---")
new_vocab = ["Python", "学习", "数据", "人工智能", "编程", "机器"]
doc3 = "机器学习"
v3 = text_to_vector(doc3, new_vocab)
print(f"新词汇表: {new_vocab}")
print(f"doc3('{doc3}')的向量: ", v3)
print("===== 练习4结束 =====")