{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-10T09:16:04.524881Z","iopub.execute_input":"2022-06-10T09:16:04.525323Z","iopub.status.idle":"2022-06-10T09:16:04.535679Z","shell.execute_reply.started":"2022-06-10T09:16:04.525291Z","shell.execute_reply":"2022-06-10T09:16:04.534431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data\n**Data Field Descriptions**\n\n* user_id - Id of user\n\n* book_id - Id of Book\n\n* review_id - Id of review\n\n* rating - rating from 0 to 5\n\n* review_text - review text\n\n* date_added - date added\n\n* date_updated - date updated\n\n* read_at - read at\n\n* started_at - started at\n\n* n_votes - no. of votes\n\n* n_comments - no. of comments","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/goodreads-books-reviews-290312/goodreads_train.csv\")\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-10T09:16:04.543810Z","iopub.execute_input":"2022-06-10T09:16:04.544289Z","iopub.status.idle":"2022-06-10T09:16:18.862284Z","shell.execute_reply.started":"2022-06-10T09:16:04.544193Z","shell.execute_reply":"2022-06-10T09:16:18.861330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"../input/goodreads-books-reviews-290312/goodreads_test.csv\")\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-10T09:16:18.863963Z","iopub.execute_input":"2022-06-10T09:16:18.864400Z","iopub.status.idle":"2022-06-10T09:16:26.104803Z","shell.execute_reply.started":"2022-06-10T09:16:18.864362Z","shell.execute_reply":"2022-06-10T09:16:26.103875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-06-10T09:16:26.106199Z","iopub.execute_input":"2022-06-10T09:16:26.107311Z","iopub.status.idle":"2022-06-10T09:16:26.835919Z","shell.execute_reply.started":"2022-06-10T09:16:26.107269Z","shell.execute_reply":"2022-06-10T09:16:26.834679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-06-10T09:16:26.838559Z","iopub.execute_input":"2022-06-10T09:16:26.839312Z","iopub.status.idle":"2022-06-10T09:16:27.235596Z","shell.execute_reply.started":"2022-06-10T09:16:26.839257Z","shell.execute_reply":"2022-06-10T09:16:27.234332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.hist(bins=20, figsize=(12, 6))","metadata":{"execution":{"iopub.status.busy":"2022-06-10T09:16:27.237336Z","iopub.execute_input":"2022-06-10T09:16:27.237786Z","iopub.status.idle":"2022-06-10T09:16:28.039438Z","shell.execute_reply.started":"2022-06-10T09:16:27.237748Z","shell.execute_reply":"2022-06-10T09:16:28.038096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[[\"review_id\", \"review_text\", \"rating\"]]","metadata":{"execution":{"iopub.status.busy":"2022-06-10T09:16:28.041078Z","iopub.execute_input":"2022-06-10T09:16:28.042008Z","iopub.status.idle":"2022-06-10T09:16:28.098892Z","shell.execute_reply.started":"2022-06-10T09:16:28.041955Z","shell.execute_reply":"2022-06-10T09:16:28.097569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelling with Surprise","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom surprise import Dataset\nfrom surprise import Reader","metadata":{"execution":{"iopub.status.busy":"2022-06-10T09:16:28.100948Z","iopub.execute_input":"2022-06-10T09:16:28.101810Z","iopub.status.idle":"2022-06-10T09:16:28.107457Z","shell.execute_reply.started":"2022-06-10T09:16:28.101754Z","shell.execute_reply":"2022-06-10T09:16:28.106251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reader = Reader(rating_scale=(1, 5))\n\n# Loads Pandas dataframe\ndata = Dataset.load_from_df(train_df[[\"user_id\", \"book_id\", \"rating\"]], reader)\ntrainset = data.build_full_trainset()","metadata":{"execution":{"iopub.status.busy":"2022-06-10T09:16:28.109074Z","iopub.execute_input":"2022-06-10T09:16:28.110198Z","iopub.status.idle":"2022-06-10T09:16:30.835021Z","shell.execute_reply.started":"2022-06-10T09:16:28.110148Z","shell.execute_reply":"2022-06-10T09:16:30.833727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from surprise import SVD\n\nparams = {\n    'n_epochs': 10,\n    'lr_all': 0.005,\n    'reg_all': 0.4\n}\nalgo = SVD(**params)\nalgo.fit(trainset)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T09:16:30.836765Z","iopub.execute_input":"2022-06-10T09:16:30.837617Z","iopub.status.idle":"2022-06-10T09:17:22.048518Z","shell.execute_reply.started":"2022-06-10T09:16:30.837567Z","shell.execute_reply":"2022-06-10T09:17:22.047523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from surprise.model_selection import cross_validate\n\n# # Run 5-fold cross-validation and print results\n# cross_validate(algo, data, measures=['RMSE', 'MAE'], cv=5, verbose=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T09:17:22.050929Z","iopub.execute_input":"2022-06-10T09:17:22.051303Z","iopub.status.idle":"2022-06-10T09:17:22.055763Z","shell.execute_reply.started":"2022-06-10T09:17:22.051271Z","shell.execute_reply":"2022-06-10T09:17:22.054574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# GridSearchCV","metadata":{}},{"cell_type":"code","source":"# from surprise.model_selection import GridSearchCV\n\n# param_grid = {'n_epochs': [5, 10],\n#               'lr_all': [0.002, 0.005],\n#               'reg_all': [0.4, 0.6]}\n\n# gs = GridSearchCV(SVD, param_grid, measures=['rmse', 'mae'], cv=3)\n# gs.fit(data)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T09:17:22.057264Z","iopub.execute_input":"2022-06-10T09:17:22.057872Z","iopub.status.idle":"2022-06-10T09:17:22.070398Z","shell.execute_reply.started":"2022-06-10T09:17:22.057838Z","shell.execute_reply":"2022-06-10T09:17:22.069349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # best RMSE score\n# print(gs.best_score['rmse'])\n\n# # combination of parameters that gave the best RMSE score\n# print(gs.best_params['rmse'])","metadata":{"execution":{"iopub.status.busy":"2022-06-10T09:17:22.071439Z","iopub.execute_input":"2022-06-10T09:17:22.072557Z","iopub.status.idle":"2022-06-10T09:17:22.087881Z","shell.execute_reply.started":"2022-06-10T09:17:22.072514Z","shell.execute_reply":"2022-06-10T09:17:22.086956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictions","metadata":{}},{"cell_type":"code","source":"predictions = [algo.predict(x[0], x[1]).est for x in test_df[[\"user_id\", \"book_id\"]].values]\npredictions = np.round(predictions).astype(\"int32\")\n\nprint(predictions)","metadata":{"execution":{"iopub.status.busy":"2022-06-10T09:18:26.937578Z","iopub.execute_input":"2022-06-10T09:18:26.938052Z","iopub.status.idle":"2022-06-10T09:18:31.019568Z","shell.execute_reply.started":"2022-06-10T09:18:26.938017Z","shell.execute_reply":"2022-06-10T09:18:31.018486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"output = pd.DataFrame({'review_id': test_df[\"review_id\"], 'rating': predictions})\noutput.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-06-10T09:18:35.696496Z","iopub.execute_input":"2022-06-10T09:18:35.697370Z","iopub.status.idle":"2022-06-10T09:18:37.062351Z","shell.execute_reply.started":"2022-06-10T09:18:35.697328Z","shell.execute_reply":"2022-06-10T09:18:37.061287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}