"evalue": "cannot import name 'Constant' from 'constants' (/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/constants.py)",
"output_type": "stream",
"output_type": "error",
"text": [
"traceback": [
"The autoreload extension is already loaded. To reload it, use:\n",
"\u001b[0;31mImportError\u001b[0m: cannot import name 'Constant' from 'constants' (/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/constants.py)"
]
]
}
}
],
],
...
@@ -59,7 +56,7 @@
...
@@ -59,7 +56,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"id": "aafd1712",
"id": "aafd1712",
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
...
@@ -69,59 +66,18 @@
...
@@ -69,59 +66,18 @@
"text": [
"text": [
"Computing the msd similarity matrix...\n",
"Computing the msd similarity matrix...\n",
"Done computing similarity matrix.\n",
"Done computing similarity matrix.\n",
"user: 11 item: 364 r_ui = 4.00 est = 3.42 {'was_impossible': True, 'reason': 'User and/or item is unknown.'}\n"
"RMSE with Jaccard similarity: 0.9870653791755158\n"
"RMSE with Jaccard similarity: 1.0910225374454734\n"
]
]
}
}
],
],
...
...
%% Cell type:markdown id:f4a8f664 tags:
%% Cell type:markdown id:f4a8f664 tags:
# Custom User-based Model
# Custom User-based Model
The present notebooks aims at creating a UserBased class that inherits from the Algobase class (surprise package) and that can be customized with various similarity metrics, peer groups and score aggregation functions.
The present notebooks aims at creating a UserBased class that inherits from the Algobase class (surprise package) and that can be customized with various similarity metrics, peer groups and score aggregation functions.
%% Cell type:code id:00d1b249 tags:
%% Cell type:code id:00d1b249 tags:
``` python
``` python
# reloads modules automatically before entering the execution of code
# reloads modules automatically before entering the execution of code
The autoreload extension is already loaded. To reload it, use:
ImportError Traceback (most recent call last)
%reload_ext autoreload
Cell In[1], line 14
10 import pandas as pd
11 # -- add new imports here --
12
13 # local imports
---> 14 from constants import Constant as C
15 from loaders import load_ratings,load_items
16 from surprise import KNNWithMeans, accuracy, AlgoBase, PredictionImpossible
ImportError: cannot import name 'Constant' from 'constants' (/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/constants.py)
%% Cell type:markdown id:22716aa3 tags:
%% Cell type:markdown id:22716aa3 tags:
# 1. Loading Data
# 1. Loading Data
Prepare a dataset in order to help implementing a user-based recommender system
Prepare a dataset in order to help implementing a user-based recommender system
%% Cell type:code id:aafd1712 tags:
%% Cell type:code id:aafd1712 tags:
``` python
``` python
# Create Surprise Dataset from the pandas DataFrame and Reader
# Create Surprise Dataset from the pandas DataFrame and Reader
surprise_data=load_ratings(surprise_format=True)
surprise_data=load_ratings(surprise_format=True)
trainset=surprise_data.build_full_trainset()
trainset=surprise_data.build_full_trainset()
testset=trainset.build_anti_testset()
testset=trainset.build_anti_testset()
sim_options={
'name':'msd',# Mean Squared Difference (Mean Square Error)
Quelque soit les neighbours (1,2,3) la valeur du ratings ne change pas
Quelque soit les neighbours (1,2,3) la valeur du ratings ne change pas
%% Cell type:markdown id:c8890e11 tags:
%% Cell type:markdown id:c8890e11 tags:
1).Predictions with min_k = 1: In this case, the model makes predictions without considering any minimum number of neighbors. Each prediction is made solely based on the similarity between the target user and other users who have rated the same items. Consequently, we observe varying prediction values for different items. For instance, for user 15 and item 942, the predicted rating is 3.777, while for item 64, the predicted rating is only 0.922. This indicates that the model heavily relies on the ratings from users who may have rated only a single item in common with the target user, leading to potentially erratic predictions.
1).Predictions with min_k = 1: In this case, the model makes predictions without considering any minimum number of neighbors. Each prediction is made solely based on the similarity between the target user and other users who have rated the same items. Consequently, we observe varying prediction values for different items. For instance, for user 15 and item 942, the predicted rating is 3.777, while for item 64, the predicted rating is only 0.922. This indicates that the model heavily relies on the ratings from users who may have rated only a single item in common with the target user, leading to potentially erratic predictions.
2). Predictions with min_k = 2: Here, a minimum of 2 neighbors are required to make a prediction. This introduces a bit of regularization, ensuring that predictions are made based on a slightly broader consensus. We notice that the predictions are somewhat similar to those with min_k = 1, but there are slight changes in some ratings. For example, the rating for item 5054 changes from 3.010 to 2.694. This suggests that the model is slightly more conservative in its predictions due to the requirement of at least two neighbors.
2). Predictions with min_k = 2: Here, a minimum of 2 neighbors are required to make a prediction. This introduces a bit of regularization, ensuring that predictions are made based on a slightly broader consensus. We notice that the predictions are somewhat similar to those with min_k = 1, but there are slight changes in some ratings. For example, the rating for item 5054 changes from 3.010 to 2.694. This suggests that the model is slightly more conservative in its predictions due to the requirement of at least two neighbors.
3). Predictions with min_k = 3: With a minimum of 3 neighbors, the model becomes even more conservative. It requires a stronger consensus among users before making predictions. As a result, we see more uniformity in the predicted ratings compared to the previous cases. For example, for item 6322, the prediction changes from 1.711 (min_k = 1) to 2.694 (min_k = 2) and finally to 2.694 again (min_k = 3). This indicates that the model is increasingly cautious as it demands more agreement among neighbors before making predictions
3). Predictions with min_k = 3: With a minimum of 3 neighbors, the model becomes even more conservative. It requires a stronger consensus among users before making predictions. As a result, we see more uniformity in the predicted ratings compared to the previous cases. For example, for item 6322, the prediction changes from 1.711 (min_k = 1) to 2.694 (min_k = 2) and finally to 2.694 again (min_k = 3). This indicates that the model is increasingly cautious as it demands more agreement among neighbors before making predictions
%% Cell type:code id:cc806424 tags:
%% Cell type:code id:cc806424 tags:
``` python
``` python
defanalyse_min_support(knn_model,testset):
defanalyse_min_support(knn_model,testset):
# Rétablir min_k à 2
# Rétablir min_k à 2
knn_model.min_k=2
knn_model.min_k=2
# Modifier min_support de 1 à 3 et observer actual_k
# Modifier min_support de 1 à 3 et observer actual_k
formin_supportinrange(1,4):
formin_supportinrange(1,4):
knn_model.sim_options['min_support']=min_support
knn_model.sim_options['min_support']=min_support
predictions_min_support=knn_model.test(testset[:30])# Prendre les 30 premières prédictions pour l'affichage
predictions_min_support=knn_model.test(testset[:30])# Prendre les 30 premières prédictions pour l'affichage
print(f"\nPrédictions avec min_support = {min_support}:")
print(f"\nPrédictions avec min_support = {min_support}:")