{"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-10-21T18:09:20.691345Z","iopub.execute_input":"2022-10-21T18:09:20.691713Z","iopub.status.idle":"2022-10-21T18:09:20.701536Z","shell.execute_reply.started":"2022-10-21T18:09:20.691686Z","shell.execute_reply":"2022-10-21T18:09:20.700517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn\nimport sklearn.model_selection\nfrom xgboost import XGBClassifier\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.metrics import mean_absolute_error\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.model_selection import cross_val_score","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:26:19.954054Z","iopub.execute_input":"2022-10-21T18:26:19.95451Z","iopub.status.idle":"2022-10-21T18:26:19.959853Z","shell.execute_reply.started":"2022-10-21T18:26:19.954477Z","shell.execute_reply":"2022-10-21T18:26:19.959135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath = \"/kaggle/input/goodreads-books-reviews-290312/\"\ndf = pd.read_csv(filepath + 'goodreads_train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:09:20.712491Z","iopub.execute_input":"2022-10-21T18:09:20.712724Z","iopub.status.idle":"2022-10-21T18:09:32.629996Z","shell.execute_reply.started":"2022-10-21T18:09:20.712703Z","shell.execute_reply":"2022-10-21T18:09:32.628585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:09:32.632077Z","iopub.execute_input":"2022-10-21T18:09:32.632461Z","iopub.status.idle":"2022-10-21T18:09:32.648516Z","shell.execute_reply.started":"2022-10-21T18:09:32.632429Z","shell.execute_reply":"2022-10-21T18:09:32.647218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:09:32.64998Z","iopub.execute_input":"2022-10-21T18:09:32.650267Z","iopub.status.idle":"2022-10-21T18:09:32.659027Z","shell.execute_reply.started":"2022-10-21T18:09:32.650241Z","shell.execute_reply":"2022-10-21T18:09:32.65797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing","metadata":{}},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:09:32.661595Z","iopub.execute_input":"2022-10-21T18:09:32.661862Z","iopub.status.idle":"2022-10-21T18:09:32.995889Z","shell.execute_reply.started":"2022-10-21T18:09:32.661839Z","shell.execute_reply":"2022-10-21T18:09:32.994964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"has_read_info\"] = df[\"read_at\"].isnull()\ndf[\"has_start_info\"] = df[\"started_at\"].isnull() ","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:09:32.997344Z","iopub.execute_input":"2022-10-21T18:09:32.997659Z","iopub.status.idle":"2022-10-21T18:09:33.072556Z","shell.execute_reply.started":"2022-10-21T18:09:32.997634Z","shell.execute_reply":"2022-10-21T18:09:33.071253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numerical_cols = [\"book_id\", \"n_votes\", \"n_comments\", \"has_read_info\", \"has_start_info\"]\ncategorical_cols = [\"user_id\"]\nother_cols = [\"review_text\", \"rating\"]\n\nquant_df = df[numerical_cols + categorical_cols + validation_cols]","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:20:02.954086Z","iopub.execute_input":"2022-10-21T18:20:02.954491Z","iopub.status.idle":"2022-10-21T18:20:02.986508Z","shell.execute_reply.started":"2022-10-21T18:20:02.954461Z","shell.execute_reply":"2022-10-21T18:20:02.985372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = df[\"rating\"]\nX = df.drop([\"rating\"], axis=1)\n\nX_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size = 0.1, random_state=1)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:20:04.337936Z","iopub.execute_input":"2022-10-21T18:20:04.338719Z","iopub.status.idle":"2022-10-21T18:20:05.184166Z","shell.execute_reply.started":"2022-10-21T18:20:04.338687Z","shell.execute_reply":"2022-10-21T18:20:05.18334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numerical_transformer = SimpleImputer(strategy='constant')\n\ncategorical_transformer = Pipeline(steps=[\n#     ('imputer', SimpleImputer(strategy='most_frequent')),\n    ('onehot', OneHotEncoder(handle_unknown='ignore'))\n])\n\npreprocessor = ColumnTransformer(\n    transformers=[\n#         ('num', numerical_transformer, numerical_cols),\n        ('cat', categorical_transformer, categorical_cols)\n    ]\n)\n\npipeline = Pipeline(steps=[\n    ('preprocessor', preprocessor),\n    ('model', XGBClassifier(n_estimators=500))\n])\n","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:23:00.72566Z","iopub.execute_input":"2022-10-21T18:23:00.72605Z","iopub.status.idle":"2022-10-21T18:23:00.733322Z","shell.execute_reply.started":"2022-10-21T18:23:00.72601Z","shell.execute_reply":"2022-10-21T18:23:00.732345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipeline.fit(X_train, y_train, early_stopping_rounds=5, eval_set=[(X_test, y_test)])\ny_pred = pipeline.predict(X_test)\nscore = sklearn.metrics.accuracy_score(y_test, y_pred)\nprint(\"Score: {:.8f}\".format(score))","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:23:02.120246Z","iopub.execute_input":"2022-10-21T18:23:02.121565Z","iopub.status.idle":"2022-10-21T18:23:55.019046Z","shell.execute_reply.started":"2022-10-21T18:23:02.121492Z","shell.execute_reply":"2022-10-21T18:23:55.018093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_absolute_error(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:11:51.869031Z","iopub.execute_input":"2022-10-21T18:11:51.87007Z","iopub.status.idle":"2022-10-21T18:11:51.877202Z","shell.execute_reply.started":"2022-10-21T18:11:51.870026Z","shell.execute_reply":"2022-10-21T18:11:51.876218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = -1 * cross_val_score(pipeline, X_test, y_test,\n                              cv=5,\n                              scoring='neg_mean_absolute_error')\n\nscores","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:26:21.878571Z","iopub.execute_input":"2022-10-21T18:26:21.87898Z","iopub.status.idle":"2022-10-21T18:26:53.891313Z","shell.execute_reply.started":"2022-10-21T18:26:21.878946Z","shell.execute_reply":"2022-10-21T18:26:53.890344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}