<jupyter_start><jupyter_text>Sample Evaluation in Surprise  Install Surprise ``` shell$ pip install scikit-surprise```  Using fit() method after train-test split<jupyter_code>from surprise import SVD
from surprise import Dataset
from surprise import accuracy
from surprise.model_selection import train_test_split

# Load the movielens-100k dataset (download it if needed),
data = Dataset.load_builtin('ml-100k')

# sample random trainset and testset
# test set is made of 25% of the ratings.
trainset, testset = train_test_split(data, test_size=.25)

# We'll use the famous SVD algorithm.
algo = SVD()

# Train the algorithm on the trainset, and predict ratings for the testset
algo.fit(trainset)
predictions = algo.test(testset)

# Then compute RMSE
accuracy.rmse(predictions)<jupyter_output>RMSE: 0.9350<jupyter_text>Other available prediction algorithms are:| Algorithm       | Description                                                                                                        ||:----------------|:-------------------------------------------------------------------------------------------------------------------|| `NormalPredictor` | Algorithm predicting a random rating based on the distribution of the training set, which is assumed to be normal. || `BaselineOnly`    | Algorithm predicting the baseline estimate for given user and item.                                                || `KNNBasic`        | A basic collaborative filtering algorithm.                                                                         || `KNNWithMeans`    | A basic collaborative filtering algorithm, taking into account the mean ratings of each user.                      || `KNNWithZScore`   | A basic collaborative filtering algorithm, taking into account the z-score normalization of each user.             || `KNNBaseline`     | A basic collaborative filtering algorithm taking into account a baseline rating.                                   || `SVDpp`           | The SVD++ algorithm, an extension of `SVD` taking into account implicit ratings.                                     || `NMF`             | A collaborative filtering algorithm based on Non-negative Matrix Factorization.                                    || `SlopeOne`        | A simple yet accurate collaborative filtering algorithm.                                                           || `CoClustering`    | A collaborative filtering algorithm based on co-clustering.                                                        | Other available accuracy metrics are `mse`, `mae`, and `fcp` (Fraction of Concordant Pairs).  Base Surprise Evaluation<jupyter_code>from surprise import (NormalPredictor, BaselineOnly, KNNBasic, KNNWithMeans,
                      KNNWithZScore, KNNBaseline, NMF, SlopeOne, CoClustering)
import pandas as pd
import numpy as np
algos = [NormalPredictor(), BaselineOnly(), KNNBasic(), KNNWithMeans(), 
         KNNWithZScore(), KNNBaseline(), SVD(), NMF(), SlopeOne(),
         CoClustering()]
algo_names = ["NormalPredictor", "BaselineOnly", "KNNBasic", "KNNWithMeans",
              "KNNWithZScore", "KNNBaseline", "SVD", "NMF", "SlopeOne",
              "CoClustering"]
i = 0
rmses = np.zeros(len(algos))
mses = np.zeros(len(algos))
maes = np.zeros(len(algos))

for algo in algos:
    print(algo_names[i])
    algo.fit(trainset)
    predictions = algo.test(testset)
    rmses[i] = accuracy.rmse(predictions, verbose=False)
    mses[i] = accuracy.mse(predictions, verbose=False)
    maes[i] = accuracy.mae(predictions, verbose=False)
    i += 1
cols = ['Prediction Algorithm', 'RMSE', 'MSE', 'MAE']
df = pd.DataFrame(columns=cols)
df['Prediction Algorithm'] = algo_names
df['RMSE'] = rmses
df['MSE'] = mses
df['MAE'] = maes
df<jupyter_output><empty_output><jupyter_text>Using predict() method after training on a whole trainset<jupyter_code>from surprise import KNNBasic

# Retrieve the trainset.
trainset = data.build_full_trainset()

# Build an algorithm, and train it.
algo = KNNBasic()
algo.fit(trainset)<jupyter_output>Computing the msd similarity matrix...
Done computing similarity matrix.<jupyter_text>To predict rating for user 196 and item 302 (true rating $r_{ui} = 4$):<jupyter_code>uid = str(196)  # raw user id (as in the ratings file). They are **strings**!
iid = str(302)  # raw item id (as in the ratings file). They are **strings**!

# get a prediction for specific users and items.
pred = algo.predict(uid, iid, r_ui=4, verbose=True)<jupyter_output>user: 196        item: 302        r_ui = 4.00   est = 4.06   {'actual_k': 40, 'was_impossible': False}
{'fraction_non_alphanumeric': 0.12102049716528565, 'fraction_numerical': 0.00981247274313127, 'mean_word_length': 2.501526717557252, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '565293', 'license': 'MIT', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1210, 'n_words': 407, 'path': 'sample_surprise/eval_sample_surprise.ipynb', 'repo_name': 'phdinds-aim/resype', 'size': '4586'}