《白话大数据与机器学习》笔记
1. 欧氏距离
- 欧氏距离
- 适用环境:
用于描述两个多维点之间的距离 - Python实现代码
def euclidean(a1, a2):
#a1,a2表示两个向量,每个向量有多个维度
distance = 0 # 欧几里得距离
for item in a1: # item表示被评分物品
if item in a2: # 判断item是否同时在a1和a2中,即是否同时被两个用户评价过
score1 = a1[item] # score代表分数
score2 = a2[item]
distance += pow((score1 - score2), 2)
distance = pow(distance, 0.5)
return distance
2. 曼哈顿距离
- 曼哈顿距离
- 适用环境:
用于描述两个多维点之间的距离,比欧氏距离计算更加方便且消除开方后取近视值带来的误差 - Python实现代码
def manhattan