<jupyter_start><jupyter_text>Dumb ModelWith every good model comes a dumb model for comparison. In order to see just how good our future models perform, we will set up a dumb algorithm that predicts the average rating for all items in the database. Thus, the predicted rating will be the same no matter what we do.<jupyter_code>class DumbAlgo(AlgoBase):

    def __init__(self):

        # Always call base method before doing anything.
        AlgoBase.__init__(self)

    def fit(self, trainset):

        # Need to call base method first again
        AlgoBase.fit(self, trainset)

        # Compute the average rating. This assumes ratings come in the (userid, itemid, rating) format
        self.the_mean = np.mean([r for (_, _, r) in self.trainset.all_ratings()])

        return self

    def estimate(self, u, i):

        return self.the_mean
dumb_algo = DumbAlgo()

trainset, testset = train_test_split(data, test_size=.25)

dumb_algo.fit(trainset)
dumb_predictions = dumb_algo.test(testset)
accuracy.rmse(dumb_predictions)
dumb_cv = cross_validate(dumb_algo, data, measures=['RMSE'], cv=5, verbose=True)<jupyter_output><empty_output><jupyter_text>Normal PredictorAnother dumb model, this time assuming the ratings fall along a normal distribution and making predictions based on that.<jupyter_code>norm_algo = NormalPredictor()

trainset, testset = train_test_split(data, test_size=.25)

norm_algo.fit(trainset)
norm_predictions = norm_algo.test(testset)
accuracy.rmse(norm_predictions)
cross_validate(norm_algo, data, measures=['RMSE'], cv=5, verbose=True)<jupyter_output><empty_output><jupyter_text>SVD AlgorithmBelow we shall check our parameters and make recommendations using the SVD algorithm<jupyter_code>param_grid = {
    "n_epochs": [10, 15, 20],
    "lr_all": [0.0025, 0.005, 0.0075],
    "reg_all": [0.2, 0.5, 0.8]
}

gs = GridSearchCV(SVD, param_grid, measures=["rmse", "mae"], cv=5, verbose=True)

gs.fit(data)

print(gs.best_score["rmse"])
print(gs.best_params["rmse"])

print(gs.best_score["mae"])
print(gs.best_params["mae"])
algo = SVD(n_epochs=20, lr_all=0.0025, reg_all=0.2, verbose=True)

# let's do train-test-split, where test set is 25% of the ratings
trainset, testset = train_test_split(data, test_size=.25)

# Train the algorithm on the trainset, and predict ratings for the testset
algo.fit(trainset)
predictions = algo.test(testset)
accuracy.rmse(predictions)
svd_cv = cross_validate(algo, data, measures=['RMSE'], cv=5, verbose=True)<jupyter_output><empty_output><jupyter_text>Plotting Error<jupyter_code>models = ['Dummy', 'SVD']

dumb_array = dumb_cv['test_rmse']
svd_array = svd_cv['test_rmse']

dumb_mean = np.mean(dumb_array)
svd_mean = np.mean(svd_array)

rmse = [dumb_mean, svd_mean]

dumb_std = np.std(dumb_array)*3
svd_std = np.std(svd_array)*3

error = [dumb_std, svd_std]

x_pos = np.arange(2)
fig, ax = plt.subplots(figsize=(15,10))
ax.bar(x_pos, rmse, align='center', alpha=0.8, color='#657e93', capsize=15)
ax.set_ylabel('Root Mean Square Error (RMSE)', fontsize=14)
ax.set_xticks(x_pos)
ax.set_xticklabels(models, fontsize=14)
ax.set_title('Model Error Comparison', fontsize=18)

# Save the figure and show
plt.tight_layout()
plt.savefig('bar_plot_with_error_bars.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Example of Recommendations<jupyter_code># Random user selected

listens_df[listens_df['Username'] == 'aaron250401'].head(15)
# In order to make predictions, we will create an anti-set which only contains user/item pairings without existing ratings

pred_algo = SVD(n_epochs=20, lr_all=0.0025, reg_all=0.2, verbose=True)

pred_trainset = data.build_full_trainset()

pred_algo.fit(pred_trainset)
pred_testset = pred_trainset.build_anti_testset()
predictions_test = pred_algo.test(pred_testset)
def get_top_n(user, predictions, n=10):
    '''Return the top-N recommendation for a user from a set of predictions.
    '''

    # First map the predictions to each user.
    top_n = defaultdict(list)
    for uid, iid, true_r, est, _ in predictions:
        top_n[uid].append((iid, est))

    # Then sort the predictions for each user and retrieve the k highest ones.
    for uid, user_ratings in top_n.items():
        user_ratings.sort(key=lambda x: x[1], reverse=True)
        top_n[uid] = user_ratings[:n]

    return top_n[user]
get_top_n('aaron250401', predictions, n=10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11735159817351598, 'fraction_numerical': 0.02214611872146119, 'mean_word_length': 5.442647058823529, 'pattern_counts': {'":': 3, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '312760', 'license': 'MIT', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1567, 'n_words': 385, 'path': 'data/CollabFiltering.ipynb', 'repo_name': 'danishjames/project-05', 'size': '4380'}