Вы можете следовать этому примеру и интегрировать его с вашим кодом.
from surprise import SVD
from surprise import Dataset
from surprise import accuracy
from surprise.model_selection import train_test_split
# Load the movielens-100k dataset (download it if needed),
data = Dataset.load_builtin('ml-100k')
trainset, testset = train_test_split(data, test_size=.25)
algo = SVD()
algo.fit(trainset)
predictions = algo.test(testset)
test = pd.DataFrame(predictions)
test = test.rename(columns={'uid':'userId', 'iid': 'movieId',
'r_ui':'actual', 'est':'prediction'})
cf_model = test.pivot_table(index='userId',
columns='movieId', values='prediction').fillna(0)
def get_users_predictions(user_id, n, model):
recommended_items = pd.DataFrame(model.loc[user_id])
recommended_items.columns = ["predicted_rating"]
recommended_items = recommended_items.sort_values('predicted_rating', ascending=False)
recommended_items = recommended_items.head(n)
return recommended_items.index.tolist()
def get_recs(model, k):
recs = []
for user in model.index:
cf_predictions = get_users_predictions(user, k, model)
recs.append(cf_predictions)
return recs
# Top-10 recommendations for each user
k = 10
recs = get_recs(cf_model, k)
preds = pd.DataFrame(index=cf_model.index)
preds[f'Top-{k} Recommendation'] = recs
preds.head()
Вывод:
| userId | Top-10 Recommendation |
|---------:|:--------------------------------------------------------------------------|
| 1 | ['50', '174', '173', '268', '183', '89', '64', '251', '100', '136'] |
| 10 | ['174', '178', '98', '530', '474', '511', '478', '132', '493', '709'] |
| 100 | ['887', '292', '1235', '268', '1238', '348', '1234', '271', '326', '886'] |
| 101 | ['181', '742', '237', '596', '405', '118', '255', '1028', '717', '928'] |
| 102 | ['50', '98', '96', '187', '168', '79', '530', '435', '228', '185'] |