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)