我一直在尝试使用python中的movielens数据集创建推荐系统。我的目标是确定用户之间的相似性,然后以这种格式为每个用户输出前五个推荐电影:
User-id1 movie-id1 movie-id2 movie-id3 movie-id4 movie-id5
User-id2 movie-id1 movie-id2 movie-id3 movie-id4 movie-id5
我现在使用的数据是这个ratings数据集。
以下是目前的代码:
import pandas as pd
import numpy as np
from sklearn import cross_validation as cv
from sklearn.metrics.pairwise import pairwise_distances
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.metrics import mean_squared_error
from math import sqrt
import scipy.sparse as sp
from scipy.sparse.linalg import svds
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('ratings.csv')
df.drop('timestamp', axis=1, inplace=True)
n_users = df.userId.unique().shape[0]
n_items = df.movieId.unique().shape[0]
#Pivot table so users are rows and movies are columns, ratings are then values
df = df.pivot(index='userId', columns='movieId', values='rating')
#subtract row mean from each rating to center data
df = df.sub(df.mean(axis=1), axis=0)
#copy to fill in predictions
c1 = df.copy()
c1 = c1.fillna('a')
#second copy to find which values were filled in and return the highest rated values
c2 = c1.copy()
#fill NAN with 0
df = df.fillna(0)
#Get cosine similarity between rows
similarity = pd.DataFrame(cosine_similarity(df))
#get top 5 similar profiles
tmp = similarity.apply(lambda row: sorted(zip(similarity.columns, row), key=lambda c: -c[1]), axis=1)
tmp = tmp.ix[:,1:6]
l = np.array(tmp)
##Prediction function - does not work needs improvement
def predict(df, c1, l):
for i in range(c1.shape[0]):
for j in range(i+1, c1.shape[1]):
try:
if c1.iloc[i][j] == 'a':
num = df[l[i][0][0]]*l[i][0][1] + df[l[i][1][0]]*l[i][1][1] + df[l[i][2][0]]*l[i][2][1] + df[l[i][3][0]]*l[i][3][1] + df[l[i][4][0]]*l[i][4][1]
den = l[i][0][1] + l[i][1][0] + l[i][2][0] + l[i][3][0] + l[i][4][0]
c1[i][j] = num/den
except:
pass
return c1
res = predict(df, c1, l)
print(res)
res = predict(df, c1, l)
print(res)
我正在尝试实现预测功能。我想预测缺失值并将它们添加到c1。我正在尝试实施this。该公式以及如何使用它的一个例子如图所示。如您所见,它使用了最相似用户的相似度得分。
相似性的输出如下所示:例如,这里是user1的相似性:
[(34, 0.19269904365720053) (196, 0.19187531680008307)
(538, 0.14932027335788825) (67, 0.14093020024386654)
(419, 0.11034407313683092) (319, 0.10055810007385564)]
我需要帮助在预测功能中使用这些相似性来预测丢失的电影评级。如果这个问题得到解决,我将不得不为每个用户找到推荐的前5部电影,并以上述格式输出。
我目前需要有关预测功能的帮助。任何建议都有帮助如果您需要更多信息或说明,请与我们联系。
感谢您阅读
答案 0 :(得分:3)
首先,矢量化使复杂问题变得更加容易。这里有一些建议可以改善你已有的东西
similarity
数据框时,请务必跟踪useIds,您可以将索引和列设置为df.columns
以下是我编辑的代码版本,包括预测实现:
```
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.preprocessing import scale
def predict(l):
# finds the userIds corresponding to the top 5 similarities
# calculate the prediction according to the formula
return (df[l.index] * l).sum(axis=1) / l.sum()
# use userID as columns for convinience when interpretering the forumla
df = pd.read_csv('ratings.csv').pivot(columns='userId',
index='movieId',
values='rating')
similarity = pd.DataFrame(cosine_similarity(
scale(df.T.fillna(-1000))),
index=df.columns,
columns=df.columns)
# iterate each column (userID),
# for each userID find the highest five similarities
# and use to calculate the prediction for that user,
# use fillna so that original ratings dont change
res = df.apply(lambda col: ' '.join('{}'.format(mid) for mid in col.fillna(
predict(similarity[col.name].nlargest(6).iloc[1:])).nlargest(5).index))
print(res)
```
这是输出的示例
userId
1 1172 1953 2105 1339 1029
2 17 39 150 222 265
3 318 356 1197 2959 3949
4 34 112 141 260 296
5 597 1035 1380 2081 33166
dtype: object
无论用户是否已观看/评分过,上面的代码都会推荐前5名。为了解决这个问题,我们可以在选择建议时将原始评级的值重置为0,如下所示\
res = df.apply(lambda col: ' '.join('{}'.format(mid) for mid in (0 * col).fillna(
predict(similarity[col.name].nlargest(6).iloc[1:])).nlargest(5).index))
输出
userId
1 2278 4085 3072 585 256
2 595 597 32 344 316
3 590 457 150 380 253
4 1375 2571 2011 1287 2455
5 480 590 457 296 165
6 1196 7064 26151 260 480
....