Similarity.py 文件源码

python
阅读 32 收藏 0 点赞 0 评论 0

项目:roleo 作者: tony-hong 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号