{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":1094,"sourceType":"datasetVersion","datasetId":571}],"dockerImageVersionId":30178,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Anime Rating Prediction w/LGBMRanker\n","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.base import BaseEstimator, TransformerMixin\nfrom lightgbm.sklearn import LGBMRanker\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:16.659268Z","iopub.execute_input":"2023-12-19T17:22:16.659720Z","iopub.status.idle":"2023-12-19T17:22:16.666204Z","shell.execute_reply.started":"2023-12-19T17:22:16.659675Z","shell.execute_reply":"2023-12-19T17:22:16.664580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Processing","metadata":{}},{"cell_type":"code","source":"transactions = pd.read_csv('/kaggle/input/anime-recommendations-database/rating.csv')\n#articles = pd.read_csv('/kaggle/input/anime-recommendations-database/anime.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:16.667965Z","iopub.execute_input":"2023-12-19T17:22:16.668279Z","iopub.status.idle":"2023-12-19T17:22:17.793463Z","shell.execute_reply.started":"2023-12-19T17:22:16.668242Z","shell.execute_reply":"2023-12-19T17:22:17.792053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(transactions)\n#display(articles)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:17.794676Z","iopub.execute_input":"2023-12-19T17:22:17.794905Z","iopub.status.idle":"2023-12-19T17:22:17.807162Z","shell.execute_reply.started":"2023-12-19T17:22:17.794878Z","shell.execute_reply":"2023-12-19T17:22:17.805613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.rating.min(),transactions.rating.max()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:17.809565Z","iopub.execute_input":"2023-12-19T17:22:17.809898Z","iopub.status.idle":"2023-12-19T17:22:17.835527Z","shell.execute_reply.started":"2023-12-19T17:22:17.809857Z","shell.execute_reply":"2023-12-19T17:22:17.834911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['rating']=transactions['rating']+1","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:17.836522Z","iopub.execute_input":"2023-12-19T17:22:17.837484Z","iopub.status.idle":"2023-12-19T17:22:17.870840Z","shell.execute_reply.started":"2023-12-19T17:22:17.837428Z","shell.execute_reply":"2023-12-19T17:22:17.870198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"    anime4user = transactions.groupby('user_id')['anime_id'].unique()\n\n    transactions.groupby('user_id')['rating'].agg(['min', 'max'])\n\n    transactions.groupby(['user_id', 'anime_id'])['rating'].mean()\n\n    transactions.groupby('anime_id')['rating'].value_counts()\n\n    transactions.groupby('user_id')['rating'].value_counts() \\\n\n    transactions.drop_duplicates(['user_id', 'anime_id'])","metadata":{"execution":{"iopub.status.busy":"2023-12-19T16:40:09.672482Z","iopub.execute_input":"2023-12-19T16:40:09.673041Z","iopub.status.idle":"2023-12-19T16:40:13.334715Z","shell.execute_reply.started":"2023-12-19T16:40:09.672980Z","shell.execute_reply":"2023-12-19T16:40:13.334061Z"}}},{"cell_type":"code","source":"data = transactions\ndata","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:17.871808Z","iopub.execute_input":"2023-12-19T17:22:17.872120Z","iopub.status.idle":"2023-12-19T17:22:17.882081Z","shell.execute_reply.started":"2023-12-19T17:22:17.872084Z","shell.execute_reply":"2023-12-19T17:22:17.880823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# train/test setting","metadata":{}},{"cell_type":"code","source":"n=len(data)\nN=list(range(n))\nnum=50000\ntrain=data.iloc[N[0:num]]\ntest=data.iloc[N[num:(num//5)*6]]\n\ntrainX=train.drop('rating',axis=1)\ntrainY=train['rating']\ntestX=test.drop('rating',axis=1)\ntestY=test['rating']","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:17.883251Z","iopub.execute_input":"2023-12-19T17:22:17.883479Z","iopub.status.idle":"2023-12-19T17:22:18.180196Z","shell.execute_reply.started":"2023-12-19T17:22:17.883450Z","shell.execute_reply":"2023-12-19T17:22:18.178745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train)\ndisplay(test)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:18.181071Z","iopub.execute_input":"2023-12-19T17:22:18.181244Z","iopub.status.idle":"2023-12-19T17:22:18.201956Z","shell.execute_reply.started":"2023-12-19T17:22:18.181220Z","shell.execute_reply":"2023-12-19T17:22:18.200462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# prepare train baskets and group sizes","metadata":{}},{"cell_type":"code","source":"train_baskets = train.groupby('user_id').agg({\n    'anime_id': lambda x: list(x),\n    'rating': lambda x: list(x)\n}).reset_index()\ndisplay(train_baskets)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:18.204869Z","iopub.execute_input":"2023-12-19T17:22:18.205650Z","iopub.status.idle":"2023-12-19T17:22:18.284144Z","shell.execute_reply.started":"2023-12-19T17:22:18.205617Z","shell.execute_reply":"2023-12-19T17:22:18.282868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(train_baskets)):\n    train_baskets.loc[i,'count']=len(train_baskets.loc[i,'anime_id'])\n    \ngroup_sizes= train_baskets['count'].astype(int).tolist()   \nprint(group_sizes)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:18.285758Z","iopub.execute_input":"2023-12-19T17:22:18.286038Z","iopub.status.idle":"2023-12-19T17:22:18.336570Z","shell.execute_reply.started":"2023-12-19T17:22:18.285980Z","shell.execute_reply":"2023-12-19T17:22:18.335505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LGBMRanker","metadata":{}},{"cell_type":"code","source":"# Model training\nranker = LGBMRanker(\n    objective=\"lambdarank\",\n    metric=\"ndcg\",\n    boosting_type=\"dart\",\n    n_estimators=1,\n    importance_type='gain',\n    verbose=10\n)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:18.338139Z","iopub.execute_input":"2023-12-19T17:22:18.338679Z","iopub.status.idle":"2023-12-19T17:22:18.344458Z","shell.execute_reply.started":"2023-12-19T17:22:18.338639Z","shell.execute_reply":"2023-12-19T17:22:18.343206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fit","metadata":{}},{"cell_type":"code","source":"ranker.fit(\n    trainX,\n    trainY,\n    group=group_sizes,\n)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:18.346052Z","iopub.execute_input":"2023-12-19T17:22:18.346678Z","iopub.status.idle":"2023-12-19T17:22:18.386442Z","shell.execute_reply.started":"2023-12-19T17:22:18.346644Z","shell.execute_reply":"2023-12-19T17:22:18.385301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{}},{"cell_type":"code","source":"test['preds'] = ranker.predict(testX)\ndisplay(test)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:18.387953Z","iopub.execute_input":"2023-12-19T17:22:18.388389Z","iopub.status.idle":"2023-12-19T17:22:18.410851Z","shell.execute_reply.started":"2023-12-19T17:22:18.388250Z","shell.execute_reply":"2023-12-19T17:22:18.409930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"minp=test['preds'].min()\nmaxp=test['preds'].max()\nprint(minp,maxp)\nminr=test['rating'].min()\nmaxr=test['rating'].max()\nprint(minr,maxr)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:18.412058Z","iopub.execute_input":"2023-12-19T17:22:18.412382Z","iopub.status.idle":"2023-12-19T17:22:18.419233Z","shell.execute_reply.started":"2023-12-19T17:22:18.412353Z","shell.execute_reply":"2023-12-19T17:22:18.418207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['preds2']=test['preds'].apply(lambda x: int((x-minp)*11/(maxp-minp)))\nminp2=test['preds2'].min()\nmaxp2=test['preds2'].max()\nprint(minp2,maxp2)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:18.420707Z","iopub.execute_input":"2023-12-19T17:22:18.420921Z","iopub.status.idle":"2023-12-19T17:22:18.437622Z","shell.execute_reply.started":"2023-12-19T17:22:18.420893Z","shell.execute_reply":"2023-12-19T17:22:18.435505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[['rating','preds','preds2']]","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:18.439924Z","iopub.execute_input":"2023-12-19T17:22:18.440233Z","iopub.status.idle":"2023-12-19T17:22:18.462058Z","shell.execute_reply.started":"2023-12-19T17:22:18.440201Z","shell.execute_reply":"2023-12-19T17:22:18.461260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.lmplot(x='rating', y='preds2', data=test, scatter_kws={'alpha':0.2})\nplt.title('Scatter Plot with Regression Line')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:18.463277Z","iopub.execute_input":"2023-12-19T17:22:18.464578Z","iopub.status.idle":"2023-12-19T17:22:18.915642Z","shell.execute_reply.started":"2023-12-19T17:22:18.464511Z","shell.execute_reply":"2023-12-19T17:22:18.914821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correlation_coefficient = test['rating'].corr(test['preds2'])\nprint(f'Correlation Coefficient: {correlation_coefficient}')","metadata":{"execution":{"iopub.status.busy":"2023-12-19T17:22:18.918827Z","iopub.execute_input":"2023-12-19T17:22:18.919083Z","iopub.status.idle":"2023-12-19T17:22:18.923946Z","shell.execute_reply.started":"2023-12-19T17:22:18.919055Z","shell.execute_reply":"2023-12-19T17:22:18.923398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}