使用Surprise构建推荐系统

import pandas as pd
from surprise import Dataset
from surprise import Reader
critics={'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5,
      'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5,
      'The Night Listener': 3.0},
     'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5,
      'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0,
      'You, Me and Dupree': 3.5},
     'Michael Phillips': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,
      'Superman Returns': 3.5, 'The Night Listener': 4.0},
     'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,
      'The Night Listener': 4.5, 'Superman Returns': 4.0,
      'You, Me and Dupree': 2.5},
     'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
      'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0,
      'You, Me and Dupree': 2.0},
     'Jack Matthews': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
      'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},
     'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}}
dat = []
for i in critics:
    for j in critics[i]: 
        dat.append([i, j, critics[i][j]])

df = pd.DataFrame(dat, columns = ['user', 'item', 'rating'])
df.head()
user item rating
0 Lisa Rose Lady in the Water 2.5
1 Lisa Rose Snakes on a Plane 3.5
2 Lisa Rose Just My Luck 3.0
3 Lisa Rose Superman Returns 3.5
4 Lisa Rose You, Me and Dupree 2.5
from surprise import Dataset
from surprise import Reader

# Loads Pandas dataframe
reader = Reader(rating_scale=(1, 5))
data = Dataset.load_from_df(df[["user", "item", "rating"]], reader)
from surprise import KNNBasic

sim_options_item = {'name': 'cosine',
               'user_based': False  # compute  similarities between items
                   }
sim_options_user = {'name': 'cosine',
               'user_based': True  # compute  similarities between items
                   }
algo_userCF = KNNBasic(sim_options=sim_options_user)
algo_itemCF = KNNBasic(sim_options=sim_options_item)
from surprise.model_selection import cross_validate

# Run 5-fold cross-validation and print results.
cross_validate(algo_userCF, data, measures=['RMSE', 'MAE'], cv=5, verbose=True)
Computing the cosine similarity matrix...
Done computing similarity matrix.
Computing the cosine similarity matrix...
Done computing similarity matrix.
Computing the cosine similarity matrix...
Done computing similarity matrix.
Computing the cosine similarity matrix...
Done computing similarity matrix.
Computing the cosine similarity matrix...
Done computing similarity matrix.
Evaluating RMSE, MAE of algorithm KNNBasic on 5 split(s).

                  Fold 1  Fold 2  Fold 3  Fold 4  Fold 5  Mean    Std     
RMSE (testset)    0.5594  1.0800  0.7700  0.3077  0.7342  0.6902  0.2544  
MAE (testset)     0.4107  0.9477  0.6591  0.2540  0.6568  0.5857  0.2376  
Fit time          0.00    0.00    0.00    0.00    0.00    0.00    0.00    
Test time         0.00    0.00    0.00    0.00    0.00    0.00    0.00    
{'test_rmse': array([0.55942374, 1.07995147, 0.76995934, 0.30772061, 0.73418814]),
 'test_mae': array([0.41069342, 0.94772278, 0.65913578, 0.25399999, 0.65678592]),
 'fit_time': (0.001051187515258789,
  0.0003440380096435547,
  0.0001010894775390625,
  9.703636169433594e-05,
  9.989738464355469e-05),
 'test_time': (0.00010585784912109375,
  9.703636169433594e-05,
  8.988380432128906e-05,
  0.00010228157043457031,
  8.320808410644531e-05)}
algo_userCF.predict('Toby', 'The Night Listener')
Prediction(uid='Toby', iid='The Night Listener', r_ui=None, est=3.6392076086091056, details={'actual_k': 4, 'was_impossible': False})
algo_userCF.predict('Toby', 'Lady in the Water')
Prediction(uid='Toby', iid='Lady in the Water', r_ui=None, est=2.7929167902350653, details={'actual_k': 5, 'was_impossible': False})
algo_userCF.predict('Toby', 'Just My Luck')
Prediction(uid='Toby', iid='Just My Luck', r_ui=None, est=2.283675166153972, details={'actual_k': 2, 'was_impossible': False})
# Run 5-fold cross-validation and print results.
cross_validate(algo_itemCF, data, measures=['RMSE', 'MAE'], cv=5, verbose=True)
Computing the cosine similarity matrix...
Done computing similarity matrix.
Computing the cosine similarity matrix...
Done computing similarity matrix.
Computing the cosine similarity matrix...
Done computing similarity matrix.
Computing the cosine similarity matrix...
Done computing similarity matrix.
Computing the cosine similarity matrix...
Done computing similarity matrix.
Evaluating RMSE, MAE of algorithm KNNBasic on 5 split(s).

                  Fold 1  Fold 2  Fold 3  Fold 4  Fold 5  Mean    Std     
RMSE (testset)    1.1602  0.7796  0.8432  1.3696  1.1644  1.0634  0.2202  
MAE (testset)     0.8664  0.6102  0.7048  0.9182  0.9780  0.8155  0.1371  
Fit time          0.00    0.00    0.00    0.00    0.00    0.00    0.00    
Test time         0.00    0.00    0.00    0.00    0.00    0.00    0.00    
{'test_rmse': array([1.16017293, 0.77964545, 0.84315228, 1.3695608 , 1.1644471 ]),
 'test_mae': array([0.86642863, 0.61020025, 0.70480684, 0.91823044, 0.97800152]),
 'fit_time': (0.00036406517028808594,
  0.00010514259338378906,
  0.00011110305786132812,
  0.00010418891906738281,
  0.00012087821960449219),
 'test_time': (0.0001251697540283203,
  9.417533874511719e-05,
  8.082389831542969e-05,
  7.796287536621094e-05,
  8.320808410644531e-05)}