{ "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": [ "\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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
imagetitletagslinks
30https://cdn.myanimelist.net/images/anime/12/89...Gintama.: Shirogane no Tamashii-henafter the fierc battl on rakuyou, the untold p...https://myanimelist.net/anime/36838/Gintama__S...
31https://cdn.myanimelist.net/images/anime/1170/...Vinland Saga Season 2after hi father' death and the destruct of hi ...https://myanimelist.net/anime/49387/Vinland_Sa...
32https://cdn.myanimelist.net/images/anime/1741/...Monogatari Series: Off & Monster Seasonkoyomi araragi spent hi last year of high scho...https://myanimelist.net/anime/57864/Monogatari...
33https://cdn.myanimelist.net/images/anime/1792/...Jujutsu Kaisen 2nd Seasonthe year is 2006, and the hall of tokyo prefec...https://myanimelist.net/anime/51009/Jujutsu_Ka...
34https://cdn.myanimelist.net/images/anime/1918/...Mob Psycho 100 IIshigeo \"mob\" kageyama is now matur and underst...https://myanimelist.net/anime/37510/Mob_Psycho...
35https://cdn.myanimelist.net/images/anime/1643/...Boku no Kokoro no Yabai Yatsu 2nd Seasonafter an event winter break, kyoutar ichikawa ...https://myanimelist.net/anime/55690/Boku_no_Ko...
36https://cdn.myanimelist.net/images/anime/1000/...Shingeki no Kyojin: The Final Seasongabi braun and falco grice have been train the...https://myanimelist.net/anime/40028/Shingeki_n...
37https://cdn.myanimelist.net/images/anime/1084/...Kizumonogatari III: Reiketsu-henafter help reviv the legendari vampir kiss-sho...https://myanimelist.net/anime/31758/Kizumonoga...
38https://cdn.myanimelist.net/images/anime/1448/...Bocchi the Rock!yearn to make friend and perform live with a b...https://myanimelist.net/anime/47917/Bocchi_the...
39https://cdn.myanimelist.net/images/anime/7/819...Haikyuu!! Karasuno Koukou vs. Shiratorizawa Ga...after the victori against aoba jousai high, ka...https://myanimelist.net/anime/32935/Haikyuu_Ka...
\n", "
" ], "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 }