surprise官方網址:http://surprise.readthedocs.io/en/stable/index.html
from surprise import KNNBasic
from surprise import Dataset
from surprise.model_selection import cross_validate
data = Dataset.load_builtin('ml-100k')
algo = KNNBasic()
cross_validate(algo, data, measures=['RMSE', 'MAE'], cv=3, verbose=True)
- 參數選擇
from surprise import SVD
from surprise import Dataset
from surprise.model_selection import GridSearchCV
param_grid = {'n_epochs':[5,10], 'lr_all':[0.001,0.002,0.005], 'reg_all':[0.2,0.4,0.6]}
grid_search = GridSearchCV(SVD, param_grid, measures=['RMSE','FCP'], cv=3)
data = Dataset.load_builtin('ml-100k')
grid_search.fit(data)
grid_search.best_score
{‘rmse’: 0.954073445182626, ‘fcp’: 0.6997103004396088}
grid_search.best_params
{‘rmse’: {‘n_epochs’: 10, ‘lr_all’: 0.005, ‘reg_all’: 0.2},
‘fcp’: {‘n_epochs’: 10, ‘lr_all’: 0.005, ‘reg_all’: 0.6}}
import pandas as pd
results_df = pd.Dataframe.from_dict(grid_search.cv_results)
results_df
得出推薦結果
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.fit(trainset)
rid_to_name, name_to_rid = read_item_names()
toy_story_raw_id = name_to_rid['Now and Then (1995)']
print(toy_story_raw_id)
toy_story_inner_id = algo.trainset.to_inner_iid(toy_story_raw_id)
print(toy_story_inner_id)
toy_story_neighbors = algo.get_neighbors(toy_story_inner_id, k=10)
toy_story_neighbors
‘1053’
961
[291, 82, 366, 528, 179, 101, 556, 310, 431, 543]
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('The 10 nearest neighbors of Toy Story are:')
for movie in toy_story_neighbors:
print(movie)
The 10 nearest neighbors of Toy Story are:
While You Were Sleeping (1995)
Batman (1989)
Dave (1993)
Mrs. Doubtfire (1993)
Groundhog Day (1993)
Raiders of the Lost Ark (1981)
Maverick (1994)
French Kiss (1995)
Stand by Me (1986)
Net, The (1995)