{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:07.697276Z","iopub.execute_input":"2022-12-15T12:48:07.699471Z","iopub.status.idle":"2022-12-15T12:48:07.712332Z","shell.execute_reply.started":"2022-12-15T12:48:07.699390Z","shell.execute_reply":"2022-12-15T12:48:07.710827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:07.718622Z","iopub.execute_input":"2022-12-15T12:48:07.718943Z","iopub.status.idle":"2022-12-15T12:48:20.873988Z","shell.execute_reply.started":"2022-12-15T12:48:07.718909Z","shell.execute_reply":"2022-12-15T12:48:20.872813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:20.875871Z","iopub.execute_input":"2022-12-15T12:48:20.876643Z","iopub.status.idle":"2022-12-15T12:48:20.888001Z","shell.execute_reply.started":"2022-12-15T12:48:20.876595Z","shell.execute_reply":"2022-12-15T12:48:20.886457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:20.892990Z","iopub.execute_input":"2022-12-15T12:48:20.893584Z","iopub.status.idle":"2022-12-15T12:48:20.919764Z","shell.execute_reply.started":"2022-12-15T12:48:20.893545Z","shell.execute_reply":"2022-12-15T12:48:20.918310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df[\"book_id\"].unique())","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:20.921824Z","iopub.execute_input":"2022-12-15T12:48:20.922346Z","iopub.status.idle":"2022-12-15T12:48:20.978256Z","shell.execute_reply.started":"2022-12-15T12:48:20.922297Z","shell.execute_reply":"2022-12-15T12:48:20.977001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop([\"review_id\", \"review_text\", \"date_added\", \"date_updated\", \"read_at\", \"started_at\", \"n_votes\", \"n_comments\"], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:20.979783Z","iopub.execute_input":"2022-12-15T12:48:20.980973Z","iopub.status.idle":"2022-12-15T12:48:21.009739Z","shell.execute_reply.started":"2022-12-15T12:48:20.980923Z","shell.execute_reply":"2022-12-15T12:48:21.008182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:21.011793Z","iopub.execute_input":"2022-12-15T12:48:21.012456Z","iopub.status.idle":"2022-12-15T12:48:21.031082Z","shell.execute_reply.started":"2022-12-15T12:48:21.012396Z","shell.execute_reply":"2022-12-15T12:48:21.029434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:21.033228Z","iopub.execute_input":"2022-12-15T12:48:21.033746Z","iopub.status.idle":"2022-12-15T12:48:21.044420Z","shell.execute_reply.started":"2022-12-15T12:48:21.033694Z","shell.execute_reply":"2022-12-15T12:48:21.042739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"movie_rating_count = (df.groupby(by = [\"book_id\"])[\"rating\"].count().reset_index().rename(columns = {\"rating\": \"rating_count\"})[[\"book_id\", \"rating_count\"]])\nmovie_rating_count.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:21.046794Z","iopub.execute_input":"2022-12-15T12:48:21.048097Z","iopub.status.idle":"2022-12-15T12:48:21.141022Z","shell.execute_reply.started":"2022-12-15T12:48:21.048044Z","shell.execute_reply":"2022-12-15T12:48:21.139074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"movie_rating_count.describe()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:21.142903Z","iopub.execute_input":"2022-12-15T12:48:21.143447Z","iopub.status.idle":"2022-12-15T12:48:21.173365Z","shell.execute_reply.started":"2022-12-15T12:48:21.143399Z","shell.execute_reply":"2022-12-15T12:48:21.172032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_rating_count = df.merge(movie_rating_count, left_on = 'book_id', right_on = 'book_id', how = 'left')","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:21.178520Z","iopub.execute_input":"2022-12-15T12:48:21.179058Z","iopub.status.idle":"2022-12-15T12:48:21.395240Z","shell.execute_reply.started":"2022-12-15T12:48:21.178979Z","shell.execute_reply":"2022-12-15T12:48:21.394049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_rating_count","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:21.396881Z","iopub.execute_input":"2022-12-15T12:48:21.397386Z","iopub.status.idle":"2022-12-15T12:48:21.413433Z","shell.execute_reply.started":"2022-12-15T12:48:21.397337Z","shell.execute_reply":"2022-12-15T12:48:21.412188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_rating_count = df_rating_count[(df_rating_count[\"rating_count\"]>50)]","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:21.418573Z","iopub.execute_input":"2022-12-15T12:48:21.419642Z","iopub.status.idle":"2022-12-15T12:48:21.464632Z","shell.execute_reply.started":"2022-12-15T12:48:21.419598Z","shell.execute_reply":"2022-12-15T12:48:21.463552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_rating_count.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:21.465999Z","iopub.execute_input":"2022-12-15T12:48:21.466838Z","iopub.status.idle":"2022-12-15T12:48:21.474262Z","shell.execute_reply.started":"2022-12-15T12:48:21.466802Z","shell.execute_reply":"2022-12-15T12:48:21.473072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df_rating_count.drop([\"rating_count\"], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:21.476301Z","iopub.execute_input":"2022-12-15T12:48:21.477189Z","iopub.status.idle":"2022-12-15T12:48:21.495141Z","shell.execute_reply.started":"2022-12-15T12:48:21.477141Z","shell.execute_reply":"2022-12-15T12:48:21.493604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:21.497080Z","iopub.execute_input":"2022-12-15T12:48:21.497553Z","iopub.status.idle":"2022-12-15T12:48:21.510457Z","shell.execute_reply.started":"2022-12-15T12:48:21.497503Z","shell.execute_reply":"2022-12-15T12:48:21.509099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"book_features_df=df.pivot_table(index='book_id',columns='user_id',values='rating').fillna(0)\nbook_features_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:21.512313Z","iopub.execute_input":"2022-12-15T12:48:21.513160Z","iopub.status.idle":"2022-12-15T12:48:23.297854Z","shell.execute_reply.started":"2022-12-15T12:48:21.513111Z","shell.execute_reply":"2022-12-15T12:48:23.296463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"book_features_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:23.299328Z","iopub.execute_input":"2022-12-15T12:48:23.299709Z","iopub.status.idle":"2022-12-15T12:48:23.308057Z","shell.execute_reply.started":"2022-12-15T12:48:23.299674Z","shell.execute_reply":"2022-12-15T12:48:23.306570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.sparse import csr_matrix\nbook_features_df_matrix = csr_matrix(book_features_df.values)\n\nfrom sklearn.neighbors import NearestNeighbors\n\n\nmodel_knn = NearestNeighbors(metric = 'cosine', algorithm = 'brute')\nmodel_knn.fit(book_features_df_matrix)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:23.310255Z","iopub.execute_input":"2022-12-15T12:48:23.310629Z","iopub.status.idle":"2022-12-15T12:48:24.790965Z","shell.execute_reply.started":"2022-12-15T12:48:23.310567Z","shell.execute_reply":"2022-12-15T12:48:24.789669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"book_features_df_matrix.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:24.792937Z","iopub.execute_input":"2022-12-15T12:48:24.793383Z","iopub.status.idle":"2022-12-15T12:48:24.800734Z","shell.execute_reply.started":"2022-12-15T12:48:24.793345Z","shell.execute_reply":"2022-12-15T12:48:24.799599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"query_index = np.random.choice(book_features_df.shape[0])\nprint(query_index)\ndistances, indices = model_knn.kneighbors(book_features_df.iloc[query_index,:].values.reshape(1, -1), n_neighbors = 6)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:24.802277Z","iopub.execute_input":"2022-12-15T12:48:24.802671Z","iopub.status.idle":"2022-12-15T12:48:24.821731Z","shell.execute_reply.started":"2022-12-15T12:48:24.802635Z","shell.execute_reply":"2022-12-15T12:48:24.820327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(0, len(distances.flatten())):\n    if i == 0:\n        print('Recommendations for {0}:\\n'.format(book_features_df.index[query_index]))\n    else:\n        print('{0}: {1}, with distance of {2}:'.format(i, book_features_df.index[indices.flatten()[i]], distances.flatten()[i]))","metadata":{"execution":{"iopub.status.busy":"2022-12-15T12:48:24.824062Z","iopub.execute_input":"2022-12-15T12:48:24.824559Z","iopub.status.idle":"2022-12-15T12:48:24.833380Z","shell.execute_reply.started":"2022-12-15T12:48:24.824508Z","shell.execute_reply":"2022-12-15T12:48:24.831705Z"},"trusted":true},"execution_count":null,"outputs":[]}]}