【问题标题】:Evaluate which neighbors the k-means algorithm found评估 k-means 算法找到哪些邻居
【发布时间】:2020-11-19 14:49:58
【问题描述】:

我目前正在建立一个推荐系统,并且在我训练了我的神经网络之后。我想找最近的邻居给客户这样的推荐。

我的问题是如何才能最好地评估这部分?

我想使用一个指标(或多个指标)来向我展示所发现的邻居的“好”或“坏”程度或建议。

你知道哪些,我该如何实现它们?

数据框

d = {'purchaseid': [0, 0, 0, 1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 9, 9, 9, 9],
     'itemid': [ 3, 8, 2, 10, 3, 10, 4, 12, 3, 12, 3, 4, 8, 6, 3, 0, 5, 12, 9, 9, 13, 1, 7, 11, 11]}
df = pd.DataFrame(data=d)



   purchaseid  itemid
0           0       3
1           0       8
2           0       2
3           1      10
4           2       3
...         ...    ...

寻找最近的邻居

from keras.models import load_model
from sklearn.cluster import KMeans

# this is a nice rock/oldies playlist
desired_user_id = 500
model_path = 'spotify_NCF_8_[64, 32, 16, 8].h5'
print('using model: %s' % model_path)
model = load_model(model_path)
print('Loaded model!')

mlp_user_embedding_weights = (next(iter(filter(lambda x: x.name == 'mlp_user_embedding', model.layers))).get_weights())

# get the latent embedding for your desired user
user_latent_matrix = mlp_user_embedding_weights[0]
one_user_vector = user_latent_matrix[desired_user_id,:]
one_user_vector = np.reshape(one_user_vector, (1,32))

print('\nPerforming kmeans to find the nearest users/playlists...')
# get 100 similar users
kmeans = KMeans(n_clusters=100, random_state=0, verbose=0).fit(user_latent_matrix)
desired_user_label = kmeans.predict(one_user_vector)
user_label = kmeans.labels_
neighbors = []
for user_id, user_label in enumerate(user_label):
    if user_label == desired_user_label:
        neighbors.append(user_id)
print('Found {0} neighbor users/playlists.'.format(len(neighbors))) 

# get the tracks in similar users' playlists
tracks = []
for user_id in neighbors:
    tracks += list(df[df['pid'] == int(user_id)]['trackindex'])
print('Found {0} neighbor tracks from these users.'.format(len(tracks))) 

users = np.full(len(tracks), desired_user_id, dtype='int32')
items = np.array(tracks, dtype='int32')

print('\nRanking most likely tracks using the NeuMF model...')
# and predict tracks for my user
results = model.predict([users,items],batch_size=100, verbose=0) 
results = results.tolist()
print('Ranked the tracks!')

.
.
.
# And now loop through and get the probability Note: This part has been removed because it is not part of the code

【问题讨论】:

    标签: python k-means metrics knn


    【解决方案1】:

    快速回答:

    有多种完善的指标可用于评估推荐系统。 recmetrics 库中提供了其中大多数的实现。

    略长一点的答案:

    这是 Claire Longo 的 great postrecmetrics' 文档的简短摘要。我真的建议阅读这两本书,以便更好地理解每个指标。她也是recmetrics的作者。

    • K 点的平均精度 (MAP@K) 和 K 点的平均召回率 (MAR@K): 这两个是推荐系统最典型的指标。通常,文章会将其绘制成条形图,K 的值不断增加(因此读者可以比较不同方法在前 1、5、10 等处的性能)。 MAR@K 包含在recmetrics 中,MAP@K 包含在ml_metrics
    • 覆盖率:是模型能够在测试集上推荐的训练数据中项目的百分比。
    • 新颖性衡量推荐系统提出用户不太可能知道的新颖和意想不到的项目的能力。
    • 个性化定义为用户推荐列表之间的1 - cosine_similarity,它量化了模型预测的具体(个性化)程度。
    • Intra-list Similarity定义为根据模型推荐的物品的特征计算的平均余弦相似度,较高的列表内相似度与模型经常推荐相似物品有关。李>

    【讨论】:

    • 非常感谢您的详细回答。对于带有库的 MAP@K(或通常)。该指标采用了两个列表。例如metrics.auc([1,1,1,0,0,0], [0.9,0.8,0.4,0.5,0.2,0.1])。你能告诉我这些名单到底是什么吗?我的正常数据框和带有建议的数据框?
    • 或者是我之前流入k-means的向量并且返回k-means的列表是第二个列表? :D
    猜你喜欢
    • 2023-03-21
    • 2018-06-07
    • 2015-03-06
    • 2015-08-29
    • 2013-07-03
    • 2010-12-05
    • 2017-04-20
    • 2013-04-22
    • 2017-11-23
    相关资源
    最近更新 更多