Spaces:
Sleeping
Sleeping
File size: 16,980 Bytes
45a5c11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sistema de recomendação de filmes usando filtro colaborativo"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Preparando conjunto de dados"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Importações do projeto"
]
},
{
"cell_type": "code",
"execution_count": 220,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"from fuzzywuzzy import process\n",
"from sklearn.metrics.pairwise import cosine_similarity\n",
"import math"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Importando conjunto de dados\n",
"ratings: Avaliações dos usuários para cada filme\n",
"\n",
"movies: informações dos filmes que foram avaliados"
]
},
{
"cell_type": "code",
"execution_count": 221,
"metadata": {},
"outputs": [],
"source": [
"ratings = pd.read_csv('../data/reduced/ratings_m10.csv')\n",
"ratings.reindex()\n",
"movies = pd.read_csv('../data/reduced/movies_m10_rich_pre.csv', index_col='movieId')\n",
"movies_title = movies[['title']]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Junção dos dois conjuntos de dados\n",
"união feita pela coluna 'movieId' presente em ambos"
]
},
{
"cell_type": "code",
"execution_count": 222,
"metadata": {},
"outputs": [],
"source": [
"ratings_movies = ratings.merge(movies_title, on='movieId')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Separação do conjunto de dados baseado no timestamp\n",
"Para cada usuário foram divididas 90% das suas avaliações para o conjunto de treino e o restante para o conjunto de teste"
]
},
{
"cell_type": "code",
"execution_count": 223,
"metadata": {},
"outputs": [],
"source": [
"def train_test_column_split(df, group_column, split_column, y_label, train_size):\n",
" df = df.sort_values(by=split_column, ascending=True) \n",
" train = pd.DataFrame(columns=df.columns)\n",
" test = pd.DataFrame(columns=df.columns)\n",
"\n",
" for idx in df[group_column].unique():\n",
" group = df.loc[df[group_column] == idx]\n",
"\n",
" q_user = group[group[split_column].le(group[split_column].quantile(train_size))]\n",
" p_user = group[group[split_column].ge(group[split_column].quantile(train_size))]\n",
"\n",
" train = pd.concat([train, q_user])\n",
" test = pd.concat([test, p_user])\n",
" train = train.sort_index(ascending=True)\n",
" test = test.sort_index(ascending=True)\n",
"\n",
" X_labels = [c for c in df.columns]\n",
"\n",
" X_train = train[X_labels]\n",
" X_test = test[X_labels]\n",
"\n",
" return (X_train, X_test)"
]
},
{
"cell_type": "code",
"execution_count": 224,
"metadata": {},
"outputs": [],
"source": [
"X_train, X_test = train_test_column_split(ratings_movies, 'userId', 'timestamp', 'rating', .9)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Criando uma Pivot Matrix\n",
"Matriz: {userId x movieId}, cada célula corresponde à avaliação de cada usuário para cada filme, em que na ausência será preenchido com 0 (zero)."
]
},
{
"cell_type": "code",
"execution_count": 225,
"metadata": {},
"outputs": [],
"source": [
"#user_movie_mat = ratings_movies.pivot(index='movieId', columns='userId', values='rating').fillna(0)\n",
"user_movie_train = X_train.pivot(index='movieId', columns='userId', values='rating').fillna(0)\n",
"user_movie_test = X_test.pivot(index='movieId', columns='userId', values='rating').fillna(0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Criando Matriz de similaridade dos usuários baseado nas avaliações"
]
},
{
"cell_type": "code",
"execution_count": 226,
"metadata": {},
"outputs": [],
"source": [
"def find_correlation_between_two_users(ratings_df: pd.DataFrame, user1: str, user2: str):\n",
" \"\"\"Find correlation between two users based on their rated movies using Pearson correlation\"\"\"\n",
" rated_movies_by_both = ratings_df[[user1, user2]].dropna(axis=0).values\n",
" user1_ratings = rated_movies_by_both[:, 0].reshape(1, -1)\n",
" user2_ratings = rated_movies_by_both[:, 1].reshape(1, -1)\n",
" return cosine_similarity(user1_ratings, user2_ratings)"
]
},
{
"cell_type": "code",
"execution_count": 227,
"metadata": {},
"outputs": [],
"source": [
"users_list = list(user_movie_train.columns)\n",
"movies_list = list(user_movie_train.index)\n",
"\n",
"#users_similarity_mat = np.array([[find_correlation_between_two_users(user_movie_train, user1, user2) for user1 in users_list] for user2 in users_list])\n",
"##users_similarity_mat = users_similarity_mat.reshape(608, 608)\n",
"#users_similarity_mat = pd.DataFrame(users_similarity_mat, index=users_list, columns=users_list)\n",
"users_similarity_mat = pd.read_pickle('../data/preprocessed/users_similarity_mat_cosim.pkl')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Métodos para prever as notas que o usuário dará para cada filme"
]
},
{
"cell_type": "code",
"execution_count": 228,
"metadata": {},
"outputs": [],
"source": [
"def get_rated_user_for_a_movie(ratings_df: pd.DataFrame, movie: str):\n",
" return ratings_df.loc[movie, :].dropna().index.values\n",
"\n",
"\n",
"def get_top_neighbors(\n",
" similarity_df: pd.DataFrame, user: str, rated_users: str, n_neighbors: int\n",
"):\n",
" return similarity_df[user][rated_users].nlargest(n_neighbors).to_dict()\n",
"\n",
"\n",
"def subtract_bias(rating: float, mean_rating: float):\n",
" return rating - mean_rating\n",
"\n",
"\n",
"def get_neighbor_rating_without_bias_per_movie(\n",
" ratings_df: pd.DataFrame, user: str, movie: str\n",
"):\n",
" \"\"\"Substract the rating of a user from the mean rating of that user to eliminate bias\"\"\"\n",
" mean_rating = ratings_df[user].mean()\n",
" rating = ratings_df.loc[movie, user]\n",
" return subtract_bias(rating, mean_rating)\n",
"\n",
"\n",
"def get_ratings_of_neighbors(ratings_df: pd.DataFrame, neighbors: list, movie: str):\n",
" \"\"\"Get the ratings of all neighbors after adjusting for biases\"\"\"\n",
" return [\n",
" get_neighbor_rating_without_bias_per_movie(ratings_df, neighbor, movie)\n",
" for neighbor in neighbors\n",
" ]\n",
"\n",
"def get_weighted_average_rating_of_neighbors(ratings: list, neighbor_distance: list):\n",
" weighted_sum = np.array(ratings).dot(np.array(neighbor_distance))\n",
" abs_neigbor_distance = np.abs(neighbor_distance)\n",
" return weighted_sum / np.sum(abs_neigbor_distance)\n",
"\n",
"\n",
"def ger_user_rating(ratings_df: pd.DataFrame, user: str, avg_neighbor_rating: float):\n",
" user_avg_rating = ratings_df[user].mean()\n",
" return round(user_avg_rating + avg_neighbor_rating, 2)\n"
]
},
{
"cell_type": "code",
"execution_count": 229,
"metadata": {},
"outputs": [],
"source": [
"def predict_rating(\n",
" df: pd.DataFrame,\n",
" similarity_df: pd.DataFrame,\n",
" user: str,\n",
" movie: str,\n",
" n_neighbors: int = 2,\n",
"):\n",
" \"\"\"Predict the rating of a user for a movie based on the ratings of neighbors\"\"\"\n",
" ratings_df = df.copy()\n",
"\n",
" rated_users = get_rated_user_for_a_movie(ratings_df, movie)\n",
"\n",
" top_neighbors_distance = get_top_neighbors(\n",
" similarity_df, user, rated_users, n_neighbors\n",
" )\n",
" neighbors, distance = top_neighbors_distance.keys(), top_neighbors_distance.values()\n",
"\n",
" #print(f\"Top {n_neighbors} neighbors of user {user}, {movie}: {list(neighbors)}, distance: {list(distance)}\")\n",
"\n",
" ratings = get_ratings_of_neighbors(ratings_df, neighbors, movie)\n",
" avg_neighbor_rating = get_weighted_average_rating_of_neighbors(\n",
" ratings, list(distance)\n",
" )\n",
"\n",
" return ger_user_rating(ratings_df, user, avg_neighbor_rating)"
]
},
{
"cell_type": "code",
"execution_count": 230,
"metadata": {},
"outputs": [],
"source": [
"def adjust_rating(nota):\n",
" if nota < 0:\n",
" return 0\n",
" elif nota > 5:\n",
" return 5\n",
" else:\n",
" # Arredonda para o valor mais próximo em incrementos de 0.5\n",
" return round(nota * 2) / 2\n"
]
},
{
"cell_type": "code",
"execution_count": 231,
"metadata": {},
"outputs": [],
"source": [
"def get_n_recommendations(user: int, n: int, user_movie_mat: pd.DataFrame, movies: pd.DataFrame, n_neighbors: int):\n",
" df = user_movie_mat.copy()\n",
" recommendations = pd.DataFrame(columns=['movieId', 'title', 'pred_rating'])\n",
"\n",
" for movie, _ in df[user].items():\n",
" if df.loc[movie, user] == 0:\n",
" df.loc[movie, user] = predict_rating(user_movie_mat, users_similarity_mat, user, movie, n_neighbors)\n",
" new_row = {'movieId': movie, 'title': movies.loc[movie]['title'], 'pred_rating': adjust_rating(df.loc[movie, user])}\n",
" recommendations.loc[len(recommendations)] = new_row\n",
"\n",
" recommendations = recommendations.sort_values(by='pred_rating', ascending=False)\n",
" return recommendations.head(n) if n > 0 else recommendations"
]
},
{
"cell_type": "code",
"execution_count": 232,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.03"
]
},
"execution_count": 232,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"movie_name = 'White Squall'\n",
"user1 = 1\n",
"movie = process.extractOne(movie_name, movies['title'])[2]\n",
"rating = predict_rating(user_movie_train, users_similarity_mat, user1, movie, 30)\n",
"rating"
]
},
{
"cell_type": "code",
"execution_count": 236,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>movieId</th>\n",
" <th>title</th>\n",
" <th>pred_rating</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>248</th>\n",
" <td>589</td>\n",
" <td>Fallen</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>31</th>\n",
" <td>47</td>\n",
" <td>White Squall</td>\n",
" <td>3.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>542</th>\n",
" <td>1527</td>\n",
" <td>Closer</td>\n",
" <td>3.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>310</th>\n",
" <td>858</td>\n",
" <td>South Park: Bigger, Longer and Uncut</td>\n",
" <td>3.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>364</th>\n",
" <td>1036</td>\n",
" <td>Great Muppet Caper, The</td>\n",
" <td>3.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>385</th>\n",
" <td>1100</td>\n",
" <td>Starman</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>384</th>\n",
" <td>1096</td>\n",
" <td>Flatliners</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>383</th>\n",
" <td>1095</td>\n",
" <td>Blood Simple</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>185</th>\n",
" <td>419</td>\n",
" <td>Henry V</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>62</th>\n",
" <td>147</td>\n",
" <td>Shallow Grave</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>666 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" movieId title pred_rating\n",
"248 589 Fallen 3.5\n",
"31 47 White Squall 3.0\n",
"542 1527 Closer 3.0\n",
"310 858 South Park: Bigger, Longer and Uncut 3.0\n",
"364 1036 Great Muppet Caper, The 3.0\n",
".. ... ... ...\n",
"385 1100 Starman 0.0\n",
"384 1096 Flatliners 0.0\n",
"383 1095 Blood Simple 0.0\n",
"185 419 Henry V 0.0\n",
"62 147 Shallow Grave 0.0\n",
"\n",
"[666 rows x 3 columns]"
]
},
"execution_count": 236,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"user_id = 1\n",
"n_top_neighbors = 30\n",
"n_recommendations = -1\n",
"\n",
"n_recommendations = get_n_recommendations(user_id, n_recommendations, user_movie_train, movies, n_top_neighbors)\n",
"n_recommendations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Avaliação dos resultados"
]
},
{
"cell_type": "code",
"execution_count": 234,
"metadata": {},
"outputs": [],
"source": [
"def eval_ratings():\n",
" test = user_movie_test.copy()\n",
"\n",
" real = []\n",
" preds = []\n",
"\n",
" for user in test.columns:\n",
" for movie, _ in test[user].items():\n",
" if test.loc[movie, user] != 0 and len(n_recommendations[n_recommendations['movieId'] == movie]['pred_rating'].values) > 0:\n",
" title = movies.loc[movie]['title']\n",
" real_rating = test.loc[movie, user]\n",
" pred_rating = n_recommendations[n_recommendations['movieId'] == movie]['pred_rating'].values[0]\n",
" \n",
" real.append(real_rating)\n",
" preds.append(pred_rating)\n",
" \n",
" #print(f'{user:10} - {title:50} - true rating: {real_rating}, pred rating: {pred_rating}, DIFF:{abs(real_rating - pred_rating)}')\n",
"\n",
" MSE = np.square(np.subtract(real, preds)).mean() \n",
" \n",
" RMSE = math.sqrt(MSE)\n",
" print(\"Root Mean Square Error:\\n\")\n",
" print(RMSE)"
]
},
{
"cell_type": "code",
"execution_count": 235,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Root Mean Square Error:\n",
"\n",
"3.0086445530929264\n"
]
}
],
"source": [
"eval_ratings()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|