大厂笔试题
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

34 lines
1.1 KiB

import math
# 定义向量x和y
x = (0, 1, 0, 1)
y = (1, 0, 1, 0)
# 计算余弦相似度
def cosine(x, y):
numerator = sum([x[i] * y[i] for i in range(len(x))])
denominator = math.sqrt(sum([x[i] ** 2 for i in range(len(x))])) * math.sqrt(sum([y[i] ** 2 for i in range(len(y))]))
return numerator / denominator
# 计算相关系数
def correlation(x, y):
n = len(x)
sum_x = sum(x)
sum_y = sum(y)
sum_xy = sum([x[i] * y[i] for i in range(n)])
sum_x2 = sum([x[i] ** 2 for i in range(n)])
sum_y2 = sum([y[i] ** 2 for i in range(n)])
numerator = n * sum_xy - sum_x * sum_y
denominator = math.sqrt((n * sum_x2 - sum_x ** 2) * (n * sum_y2 - sum_y ** 2))
return numerator / denominator
# 计算Jaccard系数
def jaccard(x, y):
intersection = sum([x[i] and y[i] for i in range(len(x))])
union = sum([x[i] or y[i] for i in range(len(x))])
return intersection / union
# 输出结果
print('余弦相似度:', cosine(x, y))
print('相关系数:', correlation(x, y))
print('Jaccard系数:', jaccard(x, y))