基于surprise库的推荐系统

surprise官网:http://surprise.readthedocs.io/en/stable/index.html
dateset:http://files.grouplens.org/datasets/movielens/ml-100k-README.txt

from surprise import KNNBasic,SVD
from surprise import Dataset
from surprise import evaluate, print_perf

# Load the movielens-100k dataset (download it if needed),
# and split it into 3 folds for cross-validation.
data = Dataset.load_builtin('ml-100k')
data.split(n_folds=3)

# We'll use the famous KNNBasic algorithm.
# 协同过滤
algo = KNNBasic()

# Evaluate performances of our algorithm on the dataset.
# 评估:均方误差、绝对误差
perf = evaluate(algo, data, measures=['RMSE', 'MAE'])

print_perf(perf)

基于皮尔逊相似度的物品推荐:

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import os
import io

from surprise import KNNBaseline
from surprise import Dataset


def read_item_names():


    file_name = ('./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



data = Dataset.load_builtin('ml-100k')

# 将数据集转换成矩阵形式
trainset = data.build_full_trainset()

# 物品推荐-相似度衡量方法:皮尔逊相似度
sim_options = {'name': 'pearson_baseline', 'user_based': False}
algo = KNNBaseline(sim_options=sim_options)
algo.train(trainset)

# 得到数据中的电影id
rid_to_name, name_to_rid = read_item_names()
toy_story_raw_id = name_to_rid['Now and Then (1995)']

# 得到矩阵中的电影id
toy_story_inner_id = algo.trainset.to_inner_iid(toy_story_raw_id)

# 电影相似推荐
toy_story_neighbors = algo.get_neighbors(toy_story_inner_id, k=10)

输出结果:

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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值