surprise推荐系统实践

协同过滤

# http://surprise.readthedocs.io/en/stable/index.html
# http://files.grouplens.org/datasets/movielens/ml-100k-README.txt
from surprise import KNNBasic,SVD
from surprise import Dataset
from surprise.model_selection import cross_validate

# Load the movielens-100k dataset
data = Dataset.load_builtin('ml-100k')
#协同过滤
algo = KNNBasic()
cross_validate(algo, data, measures=['RMSE', 'MAE'], cv=3, verbose=True)

在这里插入图片描述
交叉验证

from surprise.model_selection import GridSearchCV

#lr_all学习率,reg_all正则化惩罚项
param_grid = {'n_epochs': [5, 10], 'lr_all': [0.002, 0.005],
              'reg_all': [0.4, 0.6]}

#rmse均方误差与fcp协调分数
grid_search = GridSearchCV(SVD, param_grid, measures=['rmse', 'fcp'], cv=3)
grid_search.fit(data)
# best RMSE score
print(grid_search.best_score['rmse'])

# combination of parameters that gave the best RMSE score
print(grid_search.best_params['rmse'])

# best FCP score
print(grid_search.best_score['fcp'])

# combination of parameters that gave the best FCP score
print(grid_search.best_params['fcp'])

在这里插入图片描述

import pandas as pd  
#从字典当中转为表格形式
results_df = pd.DataFrame.from_dict(grid_search.cv_results)
results_df

在这里插入图片描述
矩阵转换并打印推荐

import io  # needed because of weird encoding of u.item file

from surprise import KNNBaseline
from surprise import Dataset
from surprise import get_dataset_dir


def read_item_names():
    """Read the u.item file from MovieLens 100-k dataset and return two
    mappings to convert raw ids into movie names and movie names into raw ids.
    """

    file_name = get_dataset_dir() + '/ml-100k/ml-100k/u.item'
    rid_to_name = {}
    name_to_rid = {}
    with io.open(file_name, 'r', encoding='ISO-8859-1') as f:
        for line in f:
            line = line.split('|')
            rid_to_name[line[0]] = line[1]
            name_to_rid[line[1]] = line[0]

    return rid_to_name, name_to_rid


# First, train the algortihm to compute the similarities between items
data = Dataset.load_builtin('ml-100k')
#数据是一行一行的,优化需要对矩阵分解,转换为原始的比较稀疏的矩阵
trainset = data.build_full_trainset()
#皮尔逊相似度衡量方法,'user_based': False即item_based
sim_options = {'name': 'pearson_baseline', 'user_based': False}
#协同过滤,一种统计方法
algo = KNNBaseline(sim_options=sim_options)
#训练
algo.fit(trainset)

# 基于item的协同过滤,手上有电影名字
rid_to_name, name_to_rid = read_item_names()

# 想知道哪些电影离'Toy Story (1995)'最近
#直接把名字传进去推荐系统不认识,用之前建的模将电影id找出来
toy_story_raw_id = name_to_rid['Toy Story (1995)']

#raw是数据样本中的id,build_full_trainset之后需要转换为矩阵id,inner_id
toy_story_inner_id = algo.trainset.to_inner_iid(toy_story_raw_id)

# 然后get近邻,k=10近邻
toy_story_neighbors = algo.get_neighbors(toy_story_inner_id, k=10)

# 再将inner_id转换为raw_id
#再由raw_id转为电影名字
toy_story_neighbors = (algo.trainset.to_raw_iid(inner_id)
                       for inner_id in toy_story_neighbors)
toy_story_neighbors = (rid_to_name[rid]
                       for rid in toy_story_neighbors)

print()
print('The 10 nearest neighbors of Toy Story are:')
for movie in toy_story_neighbors:
    print(movie)

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值