上传文件至 /
This commit is contained in:
62
龙再飞.py
Normal file
62
龙再飞.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import numpy as np
|
||||||
|
|
||||||
|
image = np.array([[100, 150, 200],[80, 120, 180],[60, 90, 140]], dtype=np.uint8)
|
||||||
|
print(image)
|
||||||
|
darker = image - 20
|
||||||
|
print(darker)
|
||||||
|
crop = image[0:2, 0:2]
|
||||||
|
print(crop)
|
||||||
|
flip_lr = np.fliplr(image)
|
||||||
|
print(flip_lr)
|
||||||
|
|
||||||
|
img = np.array([[255,255,0,0],[255,255,0,0],[0,0,255,255],[0,0,255,255]], dtype=np.uint8)
|
||||||
|
white = np.sum(img == 255)
|
||||||
|
black = np.sum(img == 0)
|
||||||
|
print(white)
|
||||||
|
print(black)
|
||||||
|
print(np.fliplr(img))
|
||||||
|
rot = np.transpose(img)
|
||||||
|
rot90 = np.flipud(rot)
|
||||||
|
print(rot90)
|
||||||
|
|
||||||
|
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)
|
||||||
|
print(vector2)
|
||||||
|
euclidean = np.linalg.norm(vector1 - vector2)
|
||||||
|
print(euclidean)
|
||||||
|
cos = np.dot(vector1, vector2) / (np.linalg.norm(vector1) * np.linalg.norm(vector2))
|
||||||
|
print(cos)
|
||||||
|
|
||||||
|
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(v1)
|
||||||
|
print(v2)
|
||||||
|
def cos_sim(a, b):
|
||||||
|
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
|
||||||
|
print(cos_sim(v1, v2))
|
||||||
|
vocab_new = ["Python", "学习", "数据", "人工智能", "编程", "机器"]
|
||||||
|
doc3 = "机器学习"
|
||||||
|
v3 = text_to_vector(doc3, vocab_new)
|
||||||
|
print(v3)
|
||||||
|
|
||||||
|
def cosine_similarity(a, b):
|
||||||
|
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
|
||||||
|
v1 = np.array([1,0,1,0])
|
||||||
|
v2 = np.array([2,0,0,0])
|
||||||
|
print(v1)
|
||||||
|
print(v2)
|
||||||
|
print(cosine_similarity(v1, v2))
|
||||||
|
v2 = v1 * 2
|
||||||
|
print(cosine_similarity(v1, v2))
|
||||||
Reference in New Issue
Block a user