Python 电影推荐中的预测值
我一直在尝试使用python中的movielens数据集创建推荐系统。我的目标是确定用户之间的相似性,然后以以下格式为每个用户输出推荐的前五部电影:Python 电影推荐中的预测值,python,pandas,numpy,dataframe,recommendation-engine,Python,Pandas,Numpy,Dataframe,Recommendation Engine,我一直在尝试使用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 我现在使用的数据就是这个数据集 以下是迄今为止的代码: import pandas as pd import n
User-id1 movie-id1 movie-id2 movie-id3 movie-id4 movie-id5
User-id2 movie-id1 movie-id2 movie-id3 movie-id4 movie-id5
我现在使用的数据就是这个数据集
以下是迄今为止的代码:
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中。我正在努力实施。图中给出了公式以及如何使用的示例。正如您所见,它使用了最相似用户的相似性分数
相似性的输出如下所示:例如,这里是user1的相似性:
[(34, 0.19269904365720053) (196, 0.19187531680008307)
(538, 0.14932027335788825) (67, 0.14093020024386654)
(419, 0.11034407313683092) (319, 0.10055810007385564)]
我需要帮助在预测函数中使用这些相似性来预测缺失的电影收视率。如果这个问题解决了,我将不得不为每个用户找到推荐的前5部电影,并以上面的格式输出它们
我目前需要有关预测功能的帮助。任何建议都有帮助。如果您需要更多信息或澄清,请告诉我
感谢您阅读首先,矢量化使复杂问题变得更容易。这里有一些建议可以改进您已有的内容
相似性
数据框架时,请确保跟踪useid,您可以通过将索引和列设置为df.columns
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
....
您面临的错误是什么?我的主要问题是,我不确定如何正确预测没有看过基于相似用户的电影的用户的新值。如果您有相似的用户,观看他们的电影很简单,那么问题是什么?我无法实现上面描述的公式来预测新值来自用户的评分。我不知道如何准确地构造函数。哦,我明白了,谢谢你,你的解决方案真是太棒了!上面代码中的一个简单问题是,我减去了每行的平均值,以使观测值居中,并确保余弦相似性是准确的。这个代码实现了相同的结果吗?不客气。我已经编辑了代码以包含一个标准化。使用scale,我编辑了代码。
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
....