def sim_inv_mag(M):
'''
Compute similarity matrix and the inverse of the magnitude on its diagonal for vectors.
The 'M' is a matrix containing input vectors.
'''
# base similarity matrix (all dot products)
# replace this with A.dot(A.T).todense() for sparse representation
similarity = np.dot(M, M.T)
# squared magnitude of preference vectors (number of occurrences)
square_mag = np.diag(similarity)
# inverse squared magnitude
inv_square_mag = 1 / square_mag
# if it doesn't occur, set it's inverse magnitude to zero (instead of inf)
inv_square_mag[np.isinf(inv_square_mag)] = 0
# inverse of the magnitude
inv_mag = np.sqrt(inv_square_mag)
return similarity, inv_mag
评论列表
文章目录