【问题标题】:Items Similarity based on their features基于特征的项目相似度
【发布时间】:2017-06-09 19:26:49
【问题描述】:

我有一个包含项目但没有用户评分的数据集。

项目具有特征(约 400 个特征)。

我想根据特征(行相似度)测量项目之间的相似度。

我将 item-feature 转换为如下所示的二进制矩阵

itemID | feature1 | feature2 | feature3 | feature4 .... 1 | 0 | 1 | 1 | 0 2 | 1 | 0 | 0 | 1 3 | 1 | 1 | 1 | 0 4 | 0 | 0 | 1 | 1
我不知道使用什么(以及如何使用它)来衡量行相似度。

我想,对于 X 项,获得前 k 个相似项。

非常感谢您提供示例代码

【问题讨论】:

    标签: similarity recommendation-engine mahout-recommender


    【解决方案1】:

    您要查找的内容称为相似性度量。快速的 google/SO 搜索将揭示获得两个向量之间相似性的各种方法。以下是python2中余弦相似度的一些示例代码:

    from math import *
    
    def square_rooted(x):
        return round(sqrt(sum([a*a for a in x])),3)
    
    def cosine_similarity(x,y):
        numerator = sum(a*b for a,b in zip(x,y))
        denominator = square_rooted(x)*square_rooted(y)
        return round(numerator/float(denominator),3)
    
    print cosine_similarity([3, 45, 7, 2], [2, 54, 13, 15])
    

    取自:http://dataaspirant.com/2015/04/11/five-most-popular-similarity-measures-implementation-in-python/

    我注意到您希望每个项目的前 k 个相似项目。最好的方法是使用 k 最近邻实现。您可以做的是创建一个 knn 图并从图中返回前 k 个相似项以进行查询。

    nmslib 是一个很好的库。这是具有余弦相似度的 HNSW 方法的 knn 查询from the library 的一些示例代码(您可以使用几种可用方法之一。HNSW 对于您的高维数据特别有效):

    import nmslib
    import numpy
    
    # create a random matrix to index
    data = numpy.random.randn(10000, 100).astype(numpy.float32)
    
    # initialize a new index, using a HNSW index on Cosine Similarity
    index = nmslib.init(method='hnsw', space='cosinesimil')
    index.addDataPointBatch(data)
    index.createIndex({'post': 2}, print_progress=True)
    
    # query for the nearest neighbours of the first datapoint
    ids, distances = index.knnQuery(data[0], k=10)
    
    # get all nearest neighbours for all the datapoint
    # using a pool of 4 threads to compute
    neighbours = index.knnQueryBatch(data, k=10, num_threads=4) 
    

    在代码的最后,每个数据点的 k 个顶部邻居将存储在 neighbours 变量中。您可以将其用于您的目的。

    【讨论】:

    • 感谢您的回复,
    • 只想知道你从哪里得到这些数字([3, 45, 7, 2], [2, 54, 13, 15])??
    • 它们只是样本数据。放入您的数据向量以获得任何两个向量之间的相似性。另外,尝试导航到我添加的链接。它们包含很多相关信息
    猜你喜欢
    • 2015-01-22
    • 2021-11-28
    • 1970-01-01
    • 2015-06-07
    • 2017-08-30
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多