计算距离
In [25]: def distance(x,y):
m_x = np.array(x)
m_y = np.array(y)
result = sum((m_x - m_y)*np.transpose(m_x - m_y))
return math.sqrt(float(result))
Test:
In [172]: distance([5,10],[7,8])
Out[172]: 2.8284271247461903
scipy库,包含了十几种不同的距离计算方法
>> scipy.spatial.distance
>> help(scipy.spatial.distance)
...
braycurtis -- the Bray-Curtis distance.
canberra -- the Canberra distance.
chebyshev -- the Chebyshev distance.
cityblock -- the Manhattan distance.
correlation -- the Correlation distance.
cosine -- the Cosine distance.
dice -- the Dice dissimilarity (boolean).
euclidean -- the Euclidean distance.
hamming -- the Hamming distance (boolean).
jaccard -- the Jaccard distance (boolean).
kulsinski -- the Kulsinski distance (boolean).
mahalanobis -- the Mahalanobis distance.
matching -- the matching dissimilarity (boolean).
minkowski -- the Minkowski distance.
rogerstanimoto -- the Rogers-Tanimoto dissimilarity (boolean).
russellrao -- the Russell-Rao dissimilarity (boolean).
seuclidean -- the normalized Euclidean distance.
sokalmichener -- the Sokal-Michener dissimilarity (boolean).
sokalsneath -- the Sokal-Sneath dissimilarity (boolean).
sqeuclidean -- the squared Euclidean distance.
wminkowski -- the weighted Minkowski distance.
yule -- the Yule dissimilarity (boolean).
sklearn库,著名的python机器学习库
sklearn.metrics.pairwise.pairwise_distances