{"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\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport nltk\nfrom nltk.tokenize import RegexpTokenizer\nfrom nltk.stem import PorterStemmer\nfrom nltk.corpus import stopwords\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import roc_auc_score, f1_score\nfrom scipy.sparse import hstack\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\n\nsub_path = \"../input/goodreads-books-reviews-290312/goodreads_sample_submission.csv\"\ntest_path = \"../input/goodreads-books-reviews-290312/goodreads_test.csv\"\ntrain_path = \"../input/goodreads-books-reviews-290312/goodreads_train.csv\"\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-31T12:03:50.597424Z","iopub.execute_input":"2022-10-31T12:03:50.597857Z","iopub.status.idle":"2022-10-31T12:03:50.607692Z","shell.execute_reply.started":"2022-10-31T12:03:50.597827Z","shell.execute_reply":"2022-10-31T12:03:50.605843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(train_path)\ntest_df = pd.read_csv(test_path)\nsub_df = pd.read_csv(sub_path)","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:08:43.977484Z","iopub.execute_input":"2022-10-31T12:08:43.978102Z","iopub.status.idle":"2022-10-31T12:09:08.50077Z","shell.execute_reply.started":"2022-10-31T12:08:43.978052Z","shell.execute_reply":"2022-10-31T12:09:08.499029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:09:14.71182Z","iopub.execute_input":"2022-10-31T12:09:14.71239Z","iopub.status.idle":"2022-10-31T12:09:14.739166Z","shell.execute_reply.started":"2022-10-31T12:09:14.712349Z","shell.execute_reply":"2022-10-31T12:09:14.736381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:09:17.776073Z","iopub.execute_input":"2022-10-31T12:09:17.776521Z","iopub.status.idle":"2022-10-31T12:09:17.798626Z","shell.execute_reply.started":"2022-10-31T12:09:17.776489Z","shell.execute_reply":"2022-10-31T12:09:17.796726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:09:21.056121Z","iopub.execute_input":"2022-10-31T12:09:21.056623Z","iopub.status.idle":"2022-10-31T12:09:21.480748Z","shell.execute_reply.started":"2022-10-31T12:09:21.05653Z","shell.execute_reply":"2022-10-31T12:09:21.479052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:09:25.036978Z","iopub.execute_input":"2022-10-31T12:09:25.037627Z","iopub.status.idle":"2022-10-31T12:09:25.278734Z","shell.execute_reply.started":"2022-10-31T12:09:25.037575Z","shell.execute_reply":"2022-10-31T12:09:25.276671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.drop([\"user_id\", \"book_id\", \"review_id\", \"date_added\", \"date_updated\", \"read_at\", \"started_at\"], inplace=True, axis=1)\ntest_df.drop([\"user_id\", \"book_id\", \"review_id\", \"date_added\", \"date_updated\", \"read_at\", \"started_at\"], inplace=True, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:11:01.760127Z","iopub.execute_input":"2022-10-31T12:11:01.760615Z","iopub.status.idle":"2022-10-31T12:11:01.809145Z","shell.execute_reply.started":"2022-10-31T12:11:01.760578Z","shell.execute_reply":"2022-10-31T12:11:01.807049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# delete all rows with column 'rating' has value 0\nindex = train_df[ (train_df['rating'] == 0)].index\ntrain_df.drop(index , inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:11:28.676554Z","iopub.execute_input":"2022-10-31T12:11:28.677239Z","iopub.status.idle":"2022-10-31T12:11:28.815438Z","shell.execute_reply.started":"2022-10-31T12:11:28.677192Z","shell.execute_reply":"2022-10-31T12:11:28.814001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(index)","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:11:41.450023Z","iopub.execute_input":"2022-10-31T12:11:41.450457Z","iopub.status.idle":"2022-10-31T12:11:41.460221Z","shell.execute_reply.started":"2022-10-31T12:11:41.450425Z","shell.execute_reply":"2022-10-31T12:11:41.458901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.reset_index(inplace=True, drop=True)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:11:59.765864Z","iopub.execute_input":"2022-10-31T12:11:59.766316Z","iopub.status.idle":"2022-10-31T12:11:59.782198Z","shell.execute_reply.started":"2022-10-31T12:11:59.766282Z","shell.execute_reply":"2022-10-31T12:11:59.781012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:12:11.515006Z","iopub.execute_input":"2022-10-31T12:12:11.515453Z","iopub.status.idle":"2022-10-31T12:12:11.528102Z","shell.execute_reply.started":"2022-10-31T12:12:11.515415Z","shell.execute_reply":"2022-10-31T12:12:11.526078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[:,'review_text'] = train_df.review_text.apply(lambda x : str.lower(x))\ntest_df.loc[:,'review_text'] = test_df.review_text.apply(lambda x : str.lower(x))","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:12:25.065501Z","iopub.execute_input":"2022-10-31T12:12:25.066094Z","iopub.status.idle":"2022-10-31T12:12:28.64226Z","shell.execute_reply.started":"2022-10-31T12:12:25.066045Z","shell.execute_reply":"2022-10-31T12:12:28.640378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_text = train_df['review_text']\ntest_text = test_df['review_text']\nall_text = pd.concat([train_text, test_text])","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:12:39.293099Z","iopub.execute_input":"2022-10-31T12:12:39.29351Z","iopub.status.idle":"2022-10-31T12:12:39.355856Z","shell.execute_reply.started":"2022-10-31T12:12:39.293481Z","shell.execute_reply":"2022-10-31T12:12:39.354573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from nltk.corpus import stopwords\n\nstopwords = stopwords.words('english')\nstopwords = stopwords[:116]\nstopwords = stopwords.extend(['d', 'll', 're', 's', 've'])","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:13:32.045865Z","iopub.execute_input":"2022-10-31T12:13:32.046415Z","iopub.status.idle":"2022-10-31T12:13:32.052731Z","shell.execute_reply.started":"2022-10-31T12:13:32.046373Z","shell.execute_reply":"2022-10-31T12:13:32.051772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nword_vectorizer = TfidfVectorizer(\n    sublinear_tf=True,\n    strip_accents='unicode',\n    analyzer='word',\n    token_pattern=r'\\w{1,}',\n    stop_words=stopwords,\n    ngram_range=(1, 1),\n    max_features=10000)\nword_vectorizer.fit(all_text)\ntrain_word_features = word_vectorizer.transform(train_text)\ntest_word_features = word_vectorizer.transform(test_text)","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:13:43.085692Z","iopub.execute_input":"2022-10-31T12:13:43.086177Z","iopub.status.idle":"2022-10-31T12:21:22.683519Z","shell.execute_reply.started":"2022-10-31T12:13:43.086142Z","shell.execute_reply":"2022-10-31T12:21:22.681292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features = hstack([train_word_features])\ntest_features = hstack([test_word_features])","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:26:52.252359Z","iopub.execute_input":"2022-10-31T12:26:52.253172Z","iopub.status.idle":"2022-10-31T12:26:52.888507Z","shell.execute_reply.started":"2022-10-31T12:26:52.253124Z","shell.execute_reply":"2022-10-31T12:26:52.887592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_target = train_df['rating']\nclassifier = LogisticRegression(solver='sag')\nclassifier.fit(train_features, train_target)","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:26:57.168378Z","iopub.execute_input":"2022-10-31T12:26:57.169118Z","iopub.status.idle":"2022-10-31T12:28:28.445529Z","shell.execute_reply.started":"2022-10-31T12:26:57.169082Z","shell.execute_reply":"2022-10-31T12:28:28.444146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LogisticRegression(solver='sag')","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:28:56.833814Z","iopub.execute_input":"2022-10-31T12:28:56.8343Z","iopub.status.idle":"2022-10-31T12:28:56.84153Z","shell.execute_reply.started":"2022-10-31T12:28:56.834263Z","shell.execute_reply":"2022-10-31T12:28:56.84066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = classifier.predict(test_features)","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:29:01.951224Z","iopub.execute_input":"2022-10-31T12:29:01.95272Z","iopub.status.idle":"2022-10-31T12:29:02.255028Z","shell.execute_reply.started":"2022-10-31T12:29:01.952658Z","shell.execute_reply":"2022-10-31T12:29:02.253987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:29:06.913448Z","iopub.execute_input":"2022-10-31T12:29:06.913865Z","iopub.status.idle":"2022-10-31T12:29:06.921466Z","shell.execute_reply.started":"2022-10-31T12:29:06.913834Z","shell.execute_reply":"2022-10-31T12:29:06.920161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df['rating'] = test_pred","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:29:11.55146Z","iopub.execute_input":"2022-10-31T12:29:11.55193Z","iopub.status.idle":"2022-10-31T12:29:11.560029Z","shell.execute_reply.started":"2022-10-31T12:29:11.551882Z","shell.execute_reply":"2022-10-31T12:29:11.558643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:29:14.214334Z","iopub.execute_input":"2022-10-31T12:29:14.214757Z","iopub.status.idle":"2022-10-31T12:29:14.947493Z","shell.execute_reply.started":"2022-10-31T12:29:14.214726Z","shell.execute_reply":"2022-10-31T12:29:14.946428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.rating.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:29:18.032158Z","iopub.execute_input":"2022-10-31T12:29:18.032762Z","iopub.status.idle":"2022-10-31T12:29:18.046283Z","shell.execute_reply.started":"2022-10-31T12:29:18.032729Z","shell.execute_reply":"2022-10-31T12:29:18.044938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.rating.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-10-31T12:29:20.964211Z","iopub.execute_input":"2022-10-31T12:29:20.964623Z","iopub.status.idle":"2022-10-31T12:29:20.982507Z","shell.execute_reply.started":"2022-10-31T12:29:20.964589Z","shell.execute_reply":"2022-10-31T12:29:20.981052Z"},"trusted":true},"execution_count":null,"outputs":[]}]}