距离计算
python种距离计算包括两种方式,一是用scipy.spatial.distance里面包括的距离很多 [‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘correlation’, ‘dice’, ‘hamming’, ‘jaccard’, ‘kulsinski’, ‘mahalanobis’, ‘matching’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’] 但是,该方法不支持稀疏矩阵;Y =scipy.spatial.distance.pdist(X, ’euclidean’)计算的是一维向量,
from scipy import spatial
x=[[0,2,3],[2,0,6],[3,6,0]]
y=spatial.distance.pdist(x)
scipy.spatial.distance.cdist(XA, XB, metric=’euclidean’, p=2, V=None, VI=None, w=None)计算的是高于一维的向量
spatial.distance.cdist(sim[0].reshape((1, 2)), sim[1].reshape((1, 2)), metric='cosine')
另一种方式是用sklearn中的sklearn.metrics.pairwise.
pairwise_distances
(X, Y=None, metric='euclidean', n_jobs=1, **kwds)