File size: 19,128 Bytes
6bc88c9 |
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 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "253d3df7",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "66bcd730",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'C:\\\\Users\\\\satka\\\\OneDrive\\\\Desktop\\\\recommendation-system\\\\data'"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%pwd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b4ed7319",
"metadata": {},
"outputs": [],
"source": [
"os.chdir(\"C:/Users/satka/OneDrive/Desktop/recommendation-system/data\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0c897fa0",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"anime_data_24.csv\")\n",
"df_links = pd.read_csv('anime_links.csv')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ec96f943",
"metadata": {},
"outputs": [],
"source": [
"anime = df.merge(df_links,on='name')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6500fbb8",
"metadata": {},
"outputs": [],
"source": [
"anime = anime[['name','sypnopsis','image','type','episodes','status','studios','source','genres','demographic','links']]"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "c28abf0f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Two years after the Summit War in which Straw Hat pirate Luffy lost his brother Ace, the story takes place on the Sabaody Archipelago. The protagonist, a young girl who has a strong admiration for Nami, sets off on a small adventure. This is an ensemble drama that focuses on people who do not \"pursue\" ONE PIECE, depicting the reunion of the Straw Hat Pirates from their perspective.\\n\\n(Source: Official site, translated)'"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'\\n'.join(anime[anime.name ==\"One Piece Fan Letter\"].sypnopsis.to_list())"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "e0f37141",
"metadata": {},
"outputs": [],
"source": [
"#anime = anime.iloc[0:8000]"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "196a74c3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['name', 'sypnopsis', 'image', 'type', 'episodes', 'status', 'studios',\n",
" 'source', 'genres', 'demographic', 'links'],\n",
" dtype='object')"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anime.columns"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "c895f232",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"RangeIndex: 13501 entries, 0 to 13500\n",
"Data columns (total 11 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 name 13501 non-null object\n",
" 1 sypnopsis 13499 non-null object\n",
" 2 image 13492 non-null object\n",
" 3 type 13501 non-null object\n",
" 4 episodes 13501 non-null object\n",
" 5 status 13501 non-null object\n",
" 6 studios 13501 non-null object\n",
" 7 source 13501 non-null object\n",
" 8 genres 11823 non-null object\n",
" 9 demographic 13466 non-null object\n",
" 10 links 13501 non-null object\n",
"dtypes: object(11)\n",
"memory usage: 1.1+ MB\n"
]
}
],
"source": [
"anime.info()"
]
},
{
"cell_type": "code",
"execution_count": 82,
"id": "dc7662fd",
"metadata": {},
"outputs": [],
"source": [
"anime.dropna(inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 83,
"id": "edc035da",
"metadata": {},
"outputs": [],
"source": [
"anime['sypnopsis_length'] = [len(i) for i in anime.sypnopsis]"
]
},
{
"cell_type": "code",
"execution_count": 84,
"id": "99183feb",
"metadata": {},
"outputs": [],
"source": [
"anime = anime[anime['sypnopsis_length'] > 300]"
]
},
{
"cell_type": "code",
"execution_count": 85,
"id": "a932d424",
"metadata": {},
"outputs": [],
"source": [
"anime['tags'] = anime['sypnopsis']+\" \" + anime['type']+\" \" + anime['episodes']+\" \" + anime['status'] +\" \"+ anime['studios'] +\" \"+ anime['source']+\" \" + anime['genres']+\" \" + anime['demographic']"
]
},
{
"cell_type": "code",
"execution_count": 117,
"id": "f76e7a39",
"metadata": {},
"outputs": [],
"source": [
"anime_1 = anime.copy()\n",
"anime_1 = anime_1[['image','name','tags','links']]"
]
},
{
"cell_type": "code",
"execution_count": 118,
"id": "8ab84681",
"metadata": {},
"outputs": [],
"source": [
"anime_1 = anime_1.reset_index(drop=True)\n",
"anime_1 = anime_1.rename({'name':'title'},axis=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "521b43bf",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.feature_extraction.text import CountVectorizer\n",
"\n",
"cv = CountVectorizer(max_features=5000,stop_words='english')"
]
},
{
"cell_type": "code",
"execution_count": 120,
"id": "bb0e6617",
"metadata": {},
"outputs": [],
"source": [
"from nltk.stem.porter import PorterStemmer\n",
"ps = PorterStemmer()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "92d8d53e",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics.pairwise import cosine_similarity"
]
},
{
"cell_type": "code",
"execution_count": 126,
"id": "8c101543",
"metadata": {},
"outputs": [],
"source": [
"def removing_blank_lines(text):\n",
" return text.replace('\\n',\" \")\n",
"\n",
"def removing_pre_suff_ix(text):\n",
" y = []\n",
" \n",
" for i in text.split():\n",
" y.append(ps.stem(i))\n",
" \n",
" return \" \".join(y)\n",
"\n",
"def converting_into_vectors(text):\n",
" vec = cv.fit_transform(text).toarray()\n",
" return vec\n",
"\n",
"def finding_similarity(vec):\n",
" similarity = cosine_similarity(vec)\n",
" return similarity"
]
},
{
"cell_type": "code",
"execution_count": 127,
"id": "01e8e16b",
"metadata": {},
"outputs": [],
"source": [
"anime_1.tags = anime_1.tags.apply(removing_blank_lines)\n",
"anime_1.tags = anime_1.tags.apply(removing_blank_lines)\n",
"vectors = converting_into_vectors(anime_1.tags)\n",
"similarity = finding_similarity(vectors)"
]
},
{
"cell_type": "code",
"execution_count": 131,
"id": "c7b531b9",
"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>image</th>\n",
" <th>title</th>\n",
" <th>tags</th>\n",
" <th>links</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>30</th>\n",
" <td>https://cdn.myanimelist.net/images/anime/12/89...</td>\n",
" <td>Gintama.: Shirogane no Tamashii-hen</td>\n",
" <td>after the fierc battl on rakuyou, the untold p...</td>\n",
" <td>https://myanimelist.net/anime/36838/Gintama__S...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>31</th>\n",
" <td>https://cdn.myanimelist.net/images/anime/1170/...</td>\n",
" <td>Vinland Saga Season 2</td>\n",
" <td>after hi father' death and the destruct of hi ...</td>\n",
" <td>https://myanimelist.net/anime/49387/Vinland_Sa...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>32</th>\n",
" <td>https://cdn.myanimelist.net/images/anime/1741/...</td>\n",
" <td>Monogatari Series: Off & Monster Season</td>\n",
" <td>koyomi araragi spent hi last year of high scho...</td>\n",
" <td>https://myanimelist.net/anime/57864/Monogatari...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>33</th>\n",
" <td>https://cdn.myanimelist.net/images/anime/1792/...</td>\n",
" <td>Jujutsu Kaisen 2nd Season</td>\n",
" <td>the year is 2006, and the hall of tokyo prefec...</td>\n",
" <td>https://myanimelist.net/anime/51009/Jujutsu_Ka...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34</th>\n",
" <td>https://cdn.myanimelist.net/images/anime/1918/...</td>\n",
" <td>Mob Psycho 100 II</td>\n",
" <td>shigeo \"mob\" kageyama is now matur and underst...</td>\n",
" <td>https://myanimelist.net/anime/37510/Mob_Psycho...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>35</th>\n",
" <td>https://cdn.myanimelist.net/images/anime/1643/...</td>\n",
" <td>Boku no Kokoro no Yabai Yatsu 2nd Season</td>\n",
" <td>after an event winter break, kyoutar ichikawa ...</td>\n",
" <td>https://myanimelist.net/anime/55690/Boku_no_Ko...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>36</th>\n",
" <td>https://cdn.myanimelist.net/images/anime/1000/...</td>\n",
" <td>Shingeki no Kyojin: The Final Season</td>\n",
" <td>gabi braun and falco grice have been train the...</td>\n",
" <td>https://myanimelist.net/anime/40028/Shingeki_n...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>37</th>\n",
" <td>https://cdn.myanimelist.net/images/anime/1084/...</td>\n",
" <td>Kizumonogatari III: Reiketsu-hen</td>\n",
" <td>after help reviv the legendari vampir kiss-sho...</td>\n",
" <td>https://myanimelist.net/anime/31758/Kizumonoga...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>38</th>\n",
" <td>https://cdn.myanimelist.net/images/anime/1448/...</td>\n",
" <td>Bocchi the Rock!</td>\n",
" <td>yearn to make friend and perform live with a b...</td>\n",
" <td>https://myanimelist.net/anime/47917/Bocchi_the...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>39</th>\n",
" <td>https://cdn.myanimelist.net/images/anime/7/819...</td>\n",
" <td>Haikyuu!! Karasuno Koukou vs. Shiratorizawa Ga...</td>\n",
" <td>after the victori against aoba jousai high, ka...</td>\n",
" <td>https://myanimelist.net/anime/32935/Haikyuu_Ka...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" image \\\n",
"30 https://cdn.myanimelist.net/images/anime/12/89... \n",
"31 https://cdn.myanimelist.net/images/anime/1170/... \n",
"32 https://cdn.myanimelist.net/images/anime/1741/... \n",
"33 https://cdn.myanimelist.net/images/anime/1792/... \n",
"34 https://cdn.myanimelist.net/images/anime/1918/... \n",
"35 https://cdn.myanimelist.net/images/anime/1643/... \n",
"36 https://cdn.myanimelist.net/images/anime/1000/... \n",
"37 https://cdn.myanimelist.net/images/anime/1084/... \n",
"38 https://cdn.myanimelist.net/images/anime/1448/... \n",
"39 https://cdn.myanimelist.net/images/anime/7/819... \n",
"\n",
" title \\\n",
"30 Gintama.: Shirogane no Tamashii-hen \n",
"31 Vinland Saga Season 2 \n",
"32 Monogatari Series: Off & Monster Season \n",
"33 Jujutsu Kaisen 2nd Season \n",
"34 Mob Psycho 100 II \n",
"35 Boku no Kokoro no Yabai Yatsu 2nd Season \n",
"36 Shingeki no Kyojin: The Final Season \n",
"37 Kizumonogatari III: Reiketsu-hen \n",
"38 Bocchi the Rock! \n",
"39 Haikyuu!! Karasuno Koukou vs. Shiratorizawa Ga... \n",
"\n",
" tags \\\n",
"30 after the fierc battl on rakuyou, the untold p... \n",
"31 after hi father' death and the destruct of hi ... \n",
"32 koyomi araragi spent hi last year of high scho... \n",
"33 the year is 2006, and the hall of tokyo prefec... \n",
"34 shigeo \"mob\" kageyama is now matur and underst... \n",
"35 after an event winter break, kyoutar ichikawa ... \n",
"36 gabi braun and falco grice have been train the... \n",
"37 after help reviv the legendari vampir kiss-sho... \n",
"38 yearn to make friend and perform live with a b... \n",
"39 after the victori against aoba jousai high, ka... \n",
"\n",
" links \n",
"30 https://myanimelist.net/anime/36838/Gintama__S... \n",
"31 https://myanimelist.net/anime/49387/Vinland_Sa... \n",
"32 https://myanimelist.net/anime/57864/Monogatari... \n",
"33 https://myanimelist.net/anime/51009/Jujutsu_Ka... \n",
"34 https://myanimelist.net/anime/37510/Mob_Psycho... \n",
"35 https://myanimelist.net/anime/55690/Boku_no_Ko... \n",
"36 https://myanimelist.net/anime/40028/Shingeki_n... \n",
"37 https://myanimelist.net/anime/31758/Kizumonoga... \n",
"38 https://myanimelist.net/anime/47917/Bocchi_the... \n",
"39 https://myanimelist.net/anime/32935/Haikyuu_Ka... "
]
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anime_1[30:40]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "163acbb5",
"metadata": {},
"outputs": [],
"source": [
"#from sklearn.metrics.pairwise import cosine_similarity\n",
"#similarity = cosine_similarity(vectors)"
]
},
{
"cell_type": "code",
"execution_count": 141,
"id": "404a1f15",
"metadata": {},
"outputs": [],
"source": [
"def recommend(anime):\n",
" anime_index = anime_1[anime_1['title']== anime].index[0]\n",
" distances = np.around(similarity[anime_index],2)\n",
" anime_list = sorted(list(enumerate(distances)),reverse=True,key=lambda x:x[1])[1:8]\n",
"\n",
" for i in anime_list:\n",
" #print(' index no. ', '|', ' title ', '|', ' similarity score ')\n",
" print(i[0], '|', anime_1.iloc[i[0]].title, '|', i[1])\n",
" print(anime_1.iloc[i[0]].links, '\\n')\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 142,
"id": "bfa64fd5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3720 | Radiant | 0.28\n",
"https://myanimelist.net/anime/37202/Radiant \n",
"\n",
"6790 | Dokyuu Hentai HxEros | 0.28\n",
"https://myanimelist.net/anime/40623/Dokyuu_Hentai_HxEros \n",
"\n",
"100 | Jujutsu Kaisen | 0.27\n",
"https://myanimelist.net/anime/40748/Jujutsu_Kaisen \n",
"\n",
"169 | Jujutsu Kaisen 0 Movie | 0.25\n",
"https://myanimelist.net/anime/48561/Jujutsu_Kaisen_0_Movie \n",
"\n",
"666 | Dead Dead Demons Dededede Destruction (OVA) | 0.25\n",
"https://myanimelist.net/anime/58883/Dead_Dead_Demons_Dededede_Destruction_OVA \n",
"\n",
"1651 | Dead Dead Demons Dededede Destruction | 0.25\n",
"https://myanimelist.net/anime/51358/Dead_Dead_Demons_Dededede_Destruction \n",
"\n",
"2390 | True Tears | 0.25\n",
"https://myanimelist.net/anime/2129/True_Tears \n",
"\n"
]
}
],
"source": [
"recommend('Jujutsu Kaisen 2nd Season')"
]
},
{
"cell_type": "code",
"execution_count": 100,
"id": "791c8dca",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(720, np.float64(0.4583333333333335)),\n",
" (123, np.float64(0.37709985557577297)),\n",
" (42, np.float64(0.32076651393589245)),\n",
" (26, np.float64(0.3051285766293647)),\n",
" (110, np.float64(0.29120520167670094))]"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(list(enumerate(similarity[anime_1[anime_1['title']== 'Shingeki no Kyojin Season 3 Part 2'].index[0]])),reverse=True,key=lambda x:x[1])[1:6]"
]
},
{
"cell_type": "code",
"execution_count": 101,
"id": "1564ee97",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"image https://cdn.myanimelist.net/images/anime/1710/...\n",
"title Shingeki no Kyojin Season 2 Movie: Kakusei no ...\n",
"tags eren yeager and other of the 104th train corp ...\n",
"links https://myanimelist.net/anime/36702/Shingeki_n...\n",
"Name: 720, dtype: object"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anime_1.iloc[720]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "438daaf5",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.13.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|