上传文件至 /
This commit is contained in:
109
樊豐铭37.py
Normal file
109
樊豐铭37.py
Normal file
@@ -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)
|
||||||
Reference in New Issue
Block a user