From e0ff4cec03700bdeb2b86cfb0966438a879e7fb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A8=8A=E8=B1=90=E9=93=AD?= <2509165037@student.example.com> Date: Thu, 16 Apr 2026 15:55:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 樊豐铭37.py | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 樊豐铭37.py diff --git a/樊豐铭37.py b/樊豐铭37.py new file mode 100644 index 0000000..cdc45dc --- /dev/null +++ b/樊豐铭37.py @@ -0,0 +1,109 @@ +#练习一 +import numpy as np + +image = np.array([ + [100, 150, 200], + [80, 120, 180], + [60, 90, 140] +], dtype=np.uint8) + +print("原图:") +print(image) +print("-" * 30) + +image_dark = image - 20 +print("变暗20后:") +print(image_dark) +print("-" * 30) + +image_crop = image[0:2, 0:2] +print("裁剪左上角 2×2 区域:") +print(image_crop) +print("-" * 30) + +image_flip = np.fliplr(image) +print("水平翻转后:") +print(image_flip) + +#练习二 +import numpy as np + +img = np.array([ + [255, 255, 0, 0 ], + [255, 255, 0, 0 ], + [0, 0, 255, 255], + [0, 0, 255, 255] +], dtype=np.uint8) + +print("=== 原图 ===") +print(img) +print("-" * 30) + +white_count = np.sum(img == 255) +black_count = np.sum(img == 0) + +print("白色像素(255)数量:", white_count) +print("黑色像素(0)数量:", black_count) +print("-" * 30) + +img_flip_lr = np.fliplr(img) +print("=== 水平翻转后 ===") +print(img_flip_lr) +print("-" * 30) + +img_rot90_ccw = np.flipud(img.T) +print("=== 逆时针旋转90度后 ===") +print(img_rot90_ccw) + +#练习三 +import numpy as np + +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("vector1:", vector1) +print("vector2:", vector2) +print("-" * 40) + +euclidean_dist = np.linalg.norm(vector1 - vector2) +print("欧几里得距离:", euclidean_dist) + +cos_sim = np.dot(vector1, vector2) / (np.linalg.norm(vector1) * np.linalg.norm(vector2)) +print("余弦相似度:", cos_sim) + +#练习四 +import numpy as np + +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("===== 原始结果 =====") +print("doc1向量:", v1) +print("doc2向量:", v2) + +cos_sim = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)) +print("余弦相似度:", cos_sim) +print("-" * 40) + +vocab_new = ["Python", "学习", "数据", "人工智能", "编程", "机器"] +doc3 = "机器学习" + +v3 = text_to_vector(doc3, vocab_new) +print("===== 新增词汇后 =====") +print("新词汇表:", vocab_new) +print("doc3 = 机器学习 的向量:", v3) \ No newline at end of file