def compute_document_similarity(X):
'''
From a matrix of unit distances, computes the cosine similarity
then changes to the angular distance (for a proper metric).
'''
S = cdist(X, X, metric='cosine')
S -= 1
S *= -1
S[S > 1] = 1.0
S[S < 0] = 0.0
# Set nan values to zero
S[np.isnan(S)] = 0
# Convert to angular distance (a proper metric)
S = 1 - (np.arccos(S) / np.pi)
assert(not np.isnan(S).any())
assert(not np.isinf(S).any())
return S
评论列表
文章目录