{"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":"markdown","source":"# **Data Preprocessing**","metadata":{"id":"9db56c5e","papermill":{"duration":0.011742,"end_time":"2023-01-01T14:10:16.608590","exception":false,"start_time":"2023-01-01T14:10:16.596848","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# Basic Imports\nimport pandas as pd\nimport numpy as np\n\n# Ignore Warnings\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"id":"f2852af1","papermill":{"duration":0.029876,"end_time":"2023-01-01T14:10:16.653242","exception":false,"start_time":"2023-01-01T14:10:16.623366","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-14T07:06:14.815439Z","iopub.execute_input":"2023-01-14T07:06:14.816066Z","iopub.status.idle":"2023-01-14T07:06:14.847048Z","shell.execute_reply.started":"2023-01-14T07:06:14.815962Z","shell.execute_reply":"2023-01-14T07:06:14.846246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load and view the training data\ntrain_df = pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_train.csv\")\ntrain_df.sample(5, random_state = 42)","metadata":{"id":"ec372ad2","papermill":{"duration":35.456926,"end_time":"2023-01-01T14:10:52.121126","exception":false,"start_time":"2023-01-01T14:10:16.664200","status":"completed"},"tags":[],"outputId":"a01e0bd5-75ef-4379-cbc3-462bf456d678","execution":{"iopub.status.busy":"2023-01-14T07:06:14.849110Z","iopub.execute_input":"2023-01-14T07:06:14.849759Z","iopub.status.idle":"2023-01-14T07:06:41.207965Z","shell.execute_reply.started":"2023-01-14T07:06:14.849725Z","shell.execute_reply":"2023-01-14T07:06:41.206887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load and view the testing data\ntest_df = pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_test.csv\")\ntest_df.sample(5, random_state = 42)","metadata":{"id":"b7fbaf56","papermill":{"duration":20.00826,"end_time":"2023-01-01T14:11:12.139335","exception":false,"start_time":"2023-01-01T14:10:52.131075","status":"completed"},"tags":[],"outputId":"3074ea4a-228a-4ee5-bccf-c16dad25ede7","execution":{"iopub.status.busy":"2023-01-14T07:06:41.209549Z","iopub.execute_input":"2023-01-14T07:06:41.209989Z","iopub.status.idle":"2023-01-14T07:06:54.953641Z","shell.execute_reply.started":"2023-01-14T07:06:41.209948Z","shell.execute_reply":"2023-01-14T07:06:54.952730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View the shape of data\ntrain_df.shape, test_df.shape","metadata":{"id":"6a9b482d","papermill":{"duration":0.021796,"end_time":"2023-01-01T14:11:12.171754","exception":false,"start_time":"2023-01-01T14:11:12.149958","status":"completed"},"tags":[],"outputId":"249278bc-9a7e-4038-f9f8-eee57f5c3bb0","execution":{"iopub.status.busy":"2023-01-14T07:06:54.956374Z","iopub.execute_input":"2023-01-14T07:06:54.956748Z","iopub.status.idle":"2023-01-14T07:06:54.963596Z","shell.execute_reply.started":"2023-01-14T07:06:54.956712Z","shell.execute_reply":"2023-01-14T07:06:54.962409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Count the unique users in train and test data\ntrain_df[\"user_id\"].nunique(), test_df[\"user_id\"].nunique()","metadata":{"id":"512eeee8","papermill":{"duration":0.195745,"end_time":"2023-01-01T14:11:12.377921","exception":false,"start_time":"2023-01-01T14:11:12.182176","status":"completed"},"tags":[],"outputId":"4fb5b37c-8bc1-4e5e-9236-4b7b0f47fa25","execution":{"iopub.status.busy":"2023-01-14T07:06:54.965326Z","iopub.execute_input":"2023-01-14T07:06:54.965801Z","iopub.status.idle":"2023-01-14T07:06:55.117753Z","shell.execute_reply.started":"2023-01-14T07:06:54.965765Z","shell.execute_reply":"2023-01-14T07:06:55.116778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Finding common Users from train and test set**","metadata":{"papermill":{"duration":0.010212,"end_time":"2023-01-01T14:11:12.398681","exception":false,"start_time":"2023-01-01T14:11:12.388469","status":"completed"},"tags":[],"id":"e2c35f74"}},{"cell_type":"code","source":"# Check for common users in train and test data\nnp.intersect1d(train_df[\"user_id\"].unique(), test_df[\"user_id\"].unique())","metadata":{"id":"e710150f","papermill":{"duration":0.211717,"end_time":"2023-01-01T14:11:12.621069","exception":false,"start_time":"2023-01-01T14:11:12.409352","status":"completed"},"tags":[],"outputId":"60110789-18ac-4360-bed3-27347022b111","execution":{"iopub.status.busy":"2023-01-14T07:06:55.119391Z","iopub.execute_input":"2023-01-14T07:06:55.119730Z","iopub.status.idle":"2023-01-14T07:06:55.278756Z","shell.execute_reply.started":"2023-01-14T07:06:55.119697Z","shell.execute_reply":"2023-01-14T07:06:55.277845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Finding unique books in train and test set**","metadata":{"papermill":{"duration":0.010629,"end_time":"2023-01-01T14:11:12.642544","exception":false,"start_time":"2023-01-01T14:11:12.631915","status":"completed"},"tags":[],"id":"323ce1e4"}},{"cell_type":"code","source":"# Count the unique books in train and test data\ntrain_df[\"book_id\"].nunique(), test_df[\"book_id\"].nunique()","metadata":{"id":"b8059aae","papermill":{"duration":0.062415,"end_time":"2023-01-01T14:11:12.715611","exception":false,"start_time":"2023-01-01T14:11:12.653196","status":"completed"},"tags":[],"outputId":"dc97c464-9b40-449f-fa64-3caf084705a7","execution":{"iopub.status.busy":"2023-01-14T07:06:55.280145Z","iopub.execute_input":"2023-01-14T07:06:55.280742Z","iopub.status.idle":"2023-01-14T07:06:55.317339Z","shell.execute_reply.started":"2023-01-14T07:06:55.280701Z","shell.execute_reply":"2023-01-14T07:06:55.316285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Finding common books from train and test set**","metadata":{"papermill":{"duration":0.010419,"end_time":"2023-01-01T14:11:12.736729","exception":false,"start_time":"2023-01-01T14:11:12.726310","status":"completed"},"tags":[],"id":"3b7aab9a"}},{"cell_type":"code","source":"# Check for common books in train and test data\nnp.intersect1d(train_df[\"book_id\"].unique(), test_df[\"book_id\"].unique()).size","metadata":{"id":"f5a477b0","papermill":{"duration":0.067849,"end_time":"2023-01-01T14:11:12.815155","exception":false,"start_time":"2023-01-01T14:11:12.747306","status":"completed"},"tags":[],"outputId":"ceda8b75-0569-4a8c-fcb7-f7dedf23e74b","execution":{"iopub.status.busy":"2023-01-14T07:06:55.318870Z","iopub.execute_input":"2023-01-14T07:06:55.319505Z","iopub.status.idle":"2023-01-14T07:06:55.360103Z","shell.execute_reply.started":"2023-01-14T07:06:55.319470Z","shell.execute_reply":"2023-01-14T07:06:55.358965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Dropping Collumns**","metadata":{"id":"vtjF66akYhVb"}},{"cell_type":"code","source":"# Dropping the columns of no use\ntrain_df.drop(columns = [\"book_id\", \"review_id\", \"date_added\", \"date_updated\", \"read_at\", \"started_at\", \"user_id\", \"n_votes\", \"n_comments\"], inplace = True)\ntest_df.drop(columns = [\"book_id\", \"review_id\", \"date_added\", \"date_updated\", \"read_at\", \"started_at\", \"user_id\", \"n_votes\", \"n_comments\"], inplace = True)","metadata":{"id":"dc43e264","papermill":{"duration":0.247243,"end_time":"2023-01-01T14:11:13.073162","exception":false,"start_time":"2023-01-01T14:11:12.825919","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-14T07:06:55.361713Z","iopub.execute_input":"2023-01-14T07:06:55.362809Z","iopub.status.idle":"2023-01-14T07:06:55.532086Z","shell.execute_reply.started":"2023-01-14T07:06:55.362771Z","shell.execute_reply":"2023-01-14T07:06:55.530960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View the new train_df\ntrain_df.sample(5, random_state = 42)","metadata":{"id":"d137b702","papermill":{"duration":0.069441,"end_time":"2023-01-01T14:11:13.153625","exception":false,"start_time":"2023-01-01T14:11:13.084184","status":"completed"},"tags":[],"outputId":"3d50caa6-aacc-455f-d0a4-1f0ef60507c4","execution":{"iopub.status.busy":"2023-01-14T07:06:55.537262Z","iopub.execute_input":"2023-01-14T07:06:55.539463Z","iopub.status.idle":"2023-01-14T07:06:55.574721Z","shell.execute_reply.started":"2023-01-14T07:06:55.539431Z","shell.execute_reply":"2023-01-14T07:06:55.573795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View the new test_df\ntest_df.sample(5, random_state = 42)","metadata":{"id":"b59d549b","papermill":{"duration":0.043176,"end_time":"2023-01-01T14:11:13.207704","exception":false,"start_time":"2023-01-01T14:11:13.164528","status":"completed"},"tags":[],"outputId":"f991e890-f893-4364-bde7-248d2d7b57a9","execution":{"iopub.status.busy":"2023-01-14T07:06:55.576038Z","iopub.execute_input":"2023-01-14T07:06:55.576408Z","iopub.status.idle":"2023-01-14T07:06:55.600234Z","shell.execute_reply.started":"2023-01-14T07:06:55.576373Z","shell.execute_reply":"2023-01-14T07:06:55.599032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Check for Null Values**","metadata":{"id":"2Tuv0vwXYkJ-"}},{"cell_type":"code","source":"# Check forl null values in our training data\ntrain_df.isnull().sum()","metadata":{"id":"c0ab01c6","papermill":{"duration":0.193646,"end_time":"2023-01-01T14:11:13.412573","exception":false,"start_time":"2023-01-01T14:11:13.218927","status":"completed"},"tags":[],"outputId":"930bed82-f3c2-4e88-fc1f-0a58c2446ebd","execution":{"iopub.status.busy":"2023-01-14T07:06:55.601993Z","iopub.execute_input":"2023-01-14T07:06:55.602362Z","iopub.status.idle":"2023-01-14T07:06:55.723913Z","shell.execute_reply.started":"2023-01-14T07:06:55.602330Z","shell.execute_reply":"2023-01-14T07:06:55.722931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check forl null values in our testing data\ntest_df.isnull().sum()","metadata":{"id":"06a7e682","papermill":{"duration":0.111003,"end_time":"2023-01-01T14:11:13.534913","exception":false,"start_time":"2023-01-01T14:11:13.423910","status":"completed"},"tags":[],"outputId":"b2155cc1-4598-43ed-ba02-25e05abc0e9a","execution":{"iopub.status.busy":"2023-01-14T07:06:55.724970Z","iopub.execute_input":"2023-01-14T07:06:55.726350Z","iopub.status.idle":"2023-01-14T07:06:55.793887Z","shell.execute_reply.started":"2023-01-14T07:06:55.726310Z","shell.execute_reply":"2023-01-14T07:06:55.792879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View train_df\ntrain_df.sample(5, random_state = 42)","metadata":{"id":"7d32ad1b","papermill":{"duration":0.067825,"end_time":"2023-01-01T14:11:13.614566","exception":false,"start_time":"2023-01-01T14:11:13.546741","status":"completed"},"tags":[],"outputId":"47d7ecb6-f2b8-47e3-e0b8-a996e06c29a0","execution":{"iopub.status.busy":"2023-01-14T07:06:55.795300Z","iopub.execute_input":"2023-01-14T07:06:55.796439Z","iopub.status.idle":"2023-01-14T07:06:55.828517Z","shell.execute_reply.started":"2023-01-14T07:06:55.796405Z","shell.execute_reply":"2023-01-14T07:06:55.827495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View test_df\ntest_df.sample(5, random_state = 42)","metadata":{"id":"64de450b","papermill":{"duration":0.053731,"end_time":"2023-01-01T14:11:13.680752","exception":false,"start_time":"2023-01-01T14:11:13.627021","status":"completed"},"tags":[],"outputId":"7fe86706-f866-4ab2-b0b6-279f00efa739","execution":{"iopub.status.busy":"2023-01-14T07:06:55.830015Z","iopub.execute_input":"2023-01-14T07:06:55.830448Z","iopub.status.idle":"2023-01-14T07:06:55.851117Z","shell.execute_reply.started":"2023-01-14T07:06:55.830412Z","shell.execute_reply":"2023-01-14T07:06:55.849991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Cleaning the Review Text**","metadata":{"id":"d00bc2a8","papermill":{"duration":0.01186,"end_time":"2023-01-01T14:11:13.713355","exception":false,"start_time":"2023-01-01T14:11:13.701495","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"**Lowercase**","metadata":{"papermill":{"duration":0.011449,"end_time":"2023-01-01T14:11:13.736840","exception":false,"start_time":"2023-01-01T14:11:13.725391","status":"completed"},"tags":[],"id":"078c4c3b"}},{"cell_type":"code","source":"# Lowercase the review text\ntrain_df[\"review_text\"] = train_df[\"review_text\"].apply(lambda x: str(x).lower())\ntest_df[\"review_text\"] = test_df[\"review_text\"].apply(lambda x: str(x).lower())","metadata":{"id":"5d70e395","papermill":{"duration":3.139704,"end_time":"2023-01-01T14:11:16.888503","exception":false,"start_time":"2023-01-01T14:11:13.748799","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-14T07:06:55.852607Z","iopub.execute_input":"2023-01-14T07:06:55.853027Z","iopub.status.idle":"2023-01-14T07:06:58.296137Z","shell.execute_reply.started":"2023-01-14T07:06:55.852993Z","shell.execute_reply":"2023-01-14T07:06:58.295114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Line Breaks and Extra Spcaces**","metadata":{"papermill":{"duration":0.01167,"end_time":"2023-01-01T14:11:16.912791","exception":false,"start_time":"2023-01-01T14:11:16.901121","status":"completed"},"tags":[],"id":"0becb141"}},{"cell_type":"code","source":"# Remove the line breaks and extra spaces\ntrain_df[\"review_text\"] = train_df[\"review_text\"].apply(lambda x: \" \".join(x.split()))\ntest_df[\"review_text\"] = test_df[\"review_text\"].apply(lambda x: \" \".join(x.split()))","metadata":{"id":"33964bea","papermill":{"duration":20.664863,"end_time":"2023-01-01T14:11:37.589607","exception":false,"start_time":"2023-01-01T14:11:16.924744","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-14T07:06:58.297845Z","iopub.execute_input":"2023-01-14T07:06:58.298267Z","iopub.status.idle":"2023-01-14T07:07:15.889081Z","shell.execute_reply.started":"2023-01-14T07:06:58.298229Z","shell.execute_reply":"2023-01-14T07:07:15.888055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Links and URL's**","metadata":{"papermill":{"duration":0.011617,"end_time":"2023-01-01T14:11:37.613224","exception":false,"start_time":"2023-01-01T14:11:37.601607","status":"completed"},"tags":[],"id":"9360f697"}},{"cell_type":"code","source":"# re for matching and replacing patterns in string\nimport re\n\n# Removing https links from the text\ntrain_df[\"review_text\"] = train_df[\"review_text\"].apply(\n    lambda x: re.sub(\n        r'(https?:\\/\\/)(\\s)*(www\\.)?(\\s)*((\\w|\\s)+\\.)*([\\w\\-\\s]+\\/)*([\\w\\-]+)((\\?)?[\\w\\s]*=\\s*[\\w\\%&]*)*', '',\n        x, flags=re.MULTILINE))\n\ntest_df[\"review_text\"] = test_df[\"review_text\"].apply(\n    lambda x: re.sub(\n        r'(https?:\\/\\/)(\\s)*(www\\.)?(\\s)*((\\w|\\s)+\\.)*([\\w\\-\\s]+\\/)*([\\w\\-]+)((\\?)?[\\w\\s]*=\\s*[\\w\\%&]*)*', '',\n        x, flags=re.MULTILINE))","metadata":{"id":"14d60886","papermill":{"duration":4.733365,"end_time":"2023-01-01T14:11:42.358395","exception":false,"start_time":"2023-01-01T14:11:37.625030","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-14T07:07:15.890623Z","iopub.execute_input":"2023-01-14T07:07:15.891004Z","iopub.status.idle":"2023-01-14T07:07:19.756690Z","shell.execute_reply.started":"2023-01-14T07:07:15.890966Z","shell.execute_reply":"2023-01-14T07:07:19.755651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Special Characters**","metadata":{"papermill":{"duration":0.011286,"end_time":"2023-01-01T14:11:42.381647","exception":false,"start_time":"2023-01-01T14:11:42.370361","status":"completed"},"tags":[],"id":"d414050d"}},{"cell_type":"code","source":"# Remove special charaters from the review text\ntrain_df[\"review_text\"] = train_df[\"review_text\"].apply(lambda x: re.sub('\\W+',' ', x))\ntest_df[\"review_text\"] = test_df[\"review_text\"].apply(lambda x: re.sub('\\W+',' ', x))","metadata":{"id":"4a8f920d","papermill":{"duration":100.938818,"end_time":"2023-01-01T14:13:23.331954","exception":false,"start_time":"2023-01-01T14:11:42.393136","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-14T07:07:19.759645Z","iopub.execute_input":"2023-01-14T07:07:19.760066Z","iopub.status.idle":"2023-01-14T07:08:52.395502Z","shell.execute_reply.started":"2023-01-14T07:07:19.760028Z","shell.execute_reply":"2023-01-14T07:08:52.394497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Removing Stopwords**","metadata":{"id":"jhrxe4cNYcNg"}},{"cell_type":"code","source":"# Import nltk and download stopwords\nimport nltk\nnltk.download('stopwords')\n\nfrom nltk.corpus import stopwords\n\n# Load the stop words\nstop_words = list(stopwords.words('english'))\n\n# View the count of stop_words\nlen(stop_words)","metadata":{"id":"d3d2501d","papermill":{"duration":1.806235,"end_time":"2023-01-01T14:13:25.150647","exception":false,"start_time":"2023-01-01T14:13:23.344412","status":"completed"},"tags":[],"outputId":"72596609-7ad5-46d4-d98d-fcda27b9775c","execution":{"iopub.status.busy":"2023-01-14T07:08:52.397864Z","iopub.execute_input":"2023-01-14T07:08:52.398275Z","iopub.status.idle":"2023-01-14T07:08:53.573992Z","shell.execute_reply.started":"2023-01-14T07:08:52.398239Z","shell.execute_reply":"2023-01-14T07:08:53.572788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remove stop_words from reviews\ntrain_df[\"review_text\"] = train_df[\"review_text\"].apply(lambda x: ' '.join([word for word in x.split() if word not in stop_words]))\ntest_df[\"review_text\"] = test_df[\"review_text\"].apply(lambda x: ' '.join([word for word in x.split() if word not in stop_words]))","metadata":{"id":"93c9c0b7","papermill":{"duration":577.223989,"end_time":"2023-01-01T14:23:02.387493","exception":false,"start_time":"2023-01-01T14:13:25.163504","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-14T07:08:53.575384Z","iopub.execute_input":"2023-01-14T07:08:53.576398Z","iopub.status.idle":"2023-01-14T07:17:29.305230Z","shell.execute_reply.started":"2023-01-14T07:08:53.576328Z","shell.execute_reply":"2023-01-14T07:17:29.304095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Remove Repeated Words**","metadata":{}},{"cell_type":"code","source":"# Remove repeated words from reviews\ntrain_df[\"review_text\"] = train_df[\"review_text\"].apply(lambda x: ' '.join(set(x.split())))\ntest_df[\"review_text\"] = test_df[\"review_text\"].apply(lambda x: ' '.join(set(x.split())))","metadata":{"execution":{"iopub.status.busy":"2023-01-14T07:17:29.307588Z","iopub.execute_input":"2023-01-14T07:17:29.307995Z","iopub.status.idle":"2023-01-14T07:17:50.763783Z","shell.execute_reply.started":"2023-01-14T07:17:29.307958Z","shell.execute_reply":"2023-01-14T07:17:50.762542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Remove Small Words**","metadata":{"id":"tvtQduJgZMhM"}},{"cell_type":"code","source":"# Remove small words from reviews\ntrain_df[\"review_text\"] = train_df[\"review_text\"].apply(lambda x: ' '.join([word for word in x.split() if len(word) >= 5]))\ntest_df[\"review_text\"] = test_df[\"review_text\"].apply(lambda x: ' '.join([word for word in x.split() if len(word) >= 5]))","metadata":{"id":"dH3Jt8pJZRIp","execution":{"iopub.status.busy":"2023-01-14T07:17:50.766154Z","iopub.execute_input":"2023-01-14T07:17:50.766551Z","iopub.status.idle":"2023-01-14T07:18:10.562075Z","shell.execute_reply.started":"2023-01-14T07:17:50.766510Z","shell.execute_reply":"2023-01-14T07:18:10.561095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View train_df\ntrain_df.sample(5, random_state = 42)","metadata":{"papermill":{"duration":0.076328,"end_time":"2023-01-01T14:23:02.477299","exception":false,"start_time":"2023-01-01T14:23:02.400971","status":"completed"},"tags":[],"id":"5cf1c22b","outputId":"b591e016-f625-4cbd-805a-75c82bd17460","execution":{"iopub.status.busy":"2023-01-14T07:18:10.566772Z","iopub.execute_input":"2023-01-14T07:18:10.567083Z","iopub.status.idle":"2023-01-14T07:18:10.600962Z","shell.execute_reply.started":"2023-01-14T07:18:10.567056Z","shell.execute_reply":"2023-01-14T07:18:10.599834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View test_df\ntest_df.sample(5, random_state = 42)","metadata":{"papermill":{"duration":0.044473,"end_time":"2023-01-01T14:23:02.534058","exception":false,"start_time":"2023-01-01T14:23:02.489585","status":"completed"},"tags":[],"id":"158353c8","outputId":"ab60025b-9f07-4335-9037-d934cfcefc11","execution":{"iopub.status.busy":"2023-01-14T07:18:10.602709Z","iopub.execute_input":"2023-01-14T07:18:10.603068Z","iopub.status.idle":"2023-01-14T07:18:10.626176Z","shell.execute_reply.started":"2023-01-14T07:18:10.603031Z","shell.execute_reply":"2023-01-14T07:18:10.625380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Visualization**","metadata":{"id":"32be23f0","papermill":{"duration":0.012014,"end_time":"2023-01-01T14:23:02.558547","exception":false,"start_time":"2023-01-01T14:23:02.546533","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# Wordcloud for text visualization - most used words throughout all reviews\nfrom wordcloud import WordCloud\n\n# Matplotlib for plotting\nimport matplotlib.pyplot as plt\n\ndef show_wordcloud(data, title = None):\n    # Initialize the wordcloud\n    wordcloud = WordCloud(\n        colormap            = \"Spectral\",\n        scale               = 3,\n        random_state        = 1\n    ).generate(str(data))\n\n    # Customize the wordcloud plot\n    fig = plt.figure(1, figsize = (16, 16))\n    plt.axis('off')\n    if title: \n        fig.suptitle(title, fontsize = 20)\n        fig.subplots_adjust(top = 2.3)\n\n    # Plot and display the wordcloud\n    plt.imshow(wordcloud)\n    plt.show()","metadata":{"id":"0ed35de3","papermill":{"duration":0.076216,"end_time":"2023-01-01T14:23:02.646840","exception":false,"start_time":"2023-01-01T14:23:02.570624","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-01-14T07:18:10.627813Z","iopub.execute_input":"2023-01-14T07:18:10.628476Z","iopub.status.idle":"2023-01-14T07:18:10.681281Z","shell.execute_reply.started":"2023-01-14T07:18:10.628418Z","shell.execute_reply":"2023-01-14T07:18:10.680408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train data word cloud\nshow_wordcloud(train_df[\"review_text\"], title = \"Train Data Wordcloud\")","metadata":{"id":"8a8f99e0","papermill":{"duration":0.712638,"end_time":"2023-01-01T14:23:03.371818","exception":false,"start_time":"2023-01-01T14:23:02.659180","status":"completed"},"tags":[],"outputId":"0fdaf7cb-5e27-4aa7-a98c-adbdf81f88e8","execution":{"iopub.status.busy":"2023-01-14T07:18:10.682565Z","iopub.execute_input":"2023-01-14T07:18:10.682911Z","iopub.status.idle":"2023-01-14T07:18:11.279496Z","shell.execute_reply.started":"2023-01-14T07:18:10.682866Z","shell.execute_reply":"2023-01-14T07:18:11.278490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test data word cloud\nshow_wordcloud(test_df[\"review_text\"], title = \"Test Data Wordcloud\")","metadata":{"id":"629f86c2","papermill":{"duration":0.688937,"end_time":"2023-01-01T14:23:04.076712","exception":false,"start_time":"2023-01-01T14:23:03.387775","status":"completed"},"tags":[],"outputId":"69d99863-b5d3-4513-f5a6-be47372c5f02","execution":{"iopub.status.busy":"2023-01-14T07:18:11.285412Z","iopub.execute_input":"2023-01-14T07:18:11.286092Z","iopub.status.idle":"2023-01-14T07:18:11.817260Z","shell.execute_reply.started":"2023-01-14T07:18:11.286056Z","shell.execute_reply":"2023-01-14T07:18:11.816288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Preparation**","metadata":{"id":"vDEVwGBraCUy"}},{"cell_type":"code","source":"# Seperate the reveiws and labels from training data\nfeatures = train_df[\"review_text\"].values\nlabels = train_df[\"rating\"].values\n\nsub_features = test_df[\"review_text\"].values","metadata":{"id":"fItszREIaEiS","execution":{"iopub.status.busy":"2023-01-14T07:18:11.818730Z","iopub.execute_input":"2023-01-14T07:18:11.819559Z","iopub.status.idle":"2023-01-14T07:18:11.825797Z","shell.execute_reply.started":"2023-01-14T07:18:11.819520Z","shell.execute_reply":"2023-01-14T07:18:11.824549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View Labels\nlabels","metadata":{"id":"3Yw3CdCuwvKm","outputId":"b7dd76bb-c04e-4b89-8507-3f6e724661b8","execution":{"iopub.status.busy":"2023-01-14T07:18:11.827745Z","iopub.execute_input":"2023-01-14T07:18:11.828216Z","iopub.status.idle":"2023-01-14T07:18:11.840396Z","shell.execute_reply.started":"2023-01-14T07:18:11.828180Z","shell.execute_reply":"2023-01-14T07:18:11.839165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tensorflow imports for text encoding\nfrom tensorflow.keras.preprocessing.text import Tokenizer\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences\n\n# Global Vars\nMAX_FEATURES = 20_000\nMAX_LEN = 200\n\n# Initialize the tokenizer\ntokenizer = Tokenizer(num_words = MAX_FEATURES)\n\n# Train the tokenizer\ntokenizer.fit_on_texts(features)\n\n# Get word index from the tokenizer\nword_index = tokenizer.word_index\n\n# Get the encoded text - Train Text\nencoded_features = tokenizer.texts_to_sequences(features)\nencoded_padded_features = pad_sequences(encoded_features, maxlen = MAX_LEN)\n\n# Get the encoded submission text\nencoded_sub_features = tokenizer.texts_to_sequences(sub_features)\nencoded_padded_sub_features = pad_sequences(encoded_sub_features, maxlen = MAX_LEN)","metadata":{"id":"RvOqPRXoi2Ta","execution":{"iopub.status.busy":"2023-01-14T07:18:11.842299Z","iopub.execute_input":"2023-01-14T07:18:11.842696Z","iopub.status.idle":"2023-01-14T07:19:56.620652Z","shell.execute_reply.started":"2023-01-14T07:18:11.842663Z","shell.execute_reply":"2023-01-14T07:19:56.619704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import the train test split\nfrom sklearn.model_selection import train_test_split\n\n# Initialize the train test split\nX_train, X_test, y_train, y_test = train_test_split(encoded_padded_features, labels, test_size = 0.20, random_state = 42)","metadata":{"id":"SxloHb6xj-8o","execution":{"iopub.status.busy":"2023-01-14T07:19:56.622560Z","iopub.execute_input":"2023-01-14T07:19:56.623338Z","iopub.status.idle":"2023-01-14T07:19:56.931901Z","shell.execute_reply.started":"2023-01-14T07:19:56.623300Z","shell.execute_reply":"2023-01-14T07:19:56.930906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# OneHotEncode the target\nfrom sklearn.preprocessing import OneHotEncoder\n\n# Initialize the encoder\nenc = OneHotEncoder(handle_unknown = 'ignore', sparse = False)\n\n# Encode the target\ny_train_encoded = enc.fit_transform(y_train.reshape(-1, 1))\ny_test_encoded = enc.transform(y_test.reshape(-1, 1))","metadata":{"id":"Rtzg0Elat1Kt","execution":{"iopub.status.busy":"2023-01-14T07:19:56.933225Z","iopub.execute_input":"2023-01-14T07:19:56.934148Z","iopub.status.idle":"2023-01-14T07:19:57.012511Z","shell.execute_reply.started":"2023-01-14T07:19:56.934109Z","shell.execute_reply":"2023-01-14T07:19:57.011486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model**","metadata":{"id":"5TxNOmWupnXH"}},{"cell_type":"code","source":"# Model Imports\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras import layers, optimizers, callbacks\n\n# Build the Model\nmodel = Sequential(\n    [\n        layers.Embedding(MAX_FEATURES, MAX_LEN, input_length = MAX_LEN),\n     \n        layers.Conv1D(64, 4, activation = 'relu', padding = 'same'),\n        layers.Conv1D(64, 2, activation = 'relu', padding = 'same'),\n        layers.MaxPooling1D(pool_size = 2),\n        layers.Bidirectional(layers.GRU(32, return_sequences = True)),\n        layers.Dropout(0.2),\n     \n        layers.Conv1D(128, 8, activation = 'relu', padding = 'same'),\n        layers.Conv1D(128, 4, activation = 'relu', padding = 'same'),\n        layers.MaxPooling1D(pool_size = 2),\n        layers.Bidirectional(layers.GRU(32, return_sequences = True)),\n        layers.Dropout(0.2),\n     \n        layers.Conv1D(128, 4, activation = 'relu', padding = 'same'),\n        layers.Conv1D(128, 2, activation = 'relu', padding = 'same'),\n        layers.MaxPooling1D(pool_size = 2),\n        layers.Bidirectional(layers.GRU(32, return_sequences = True)),\n        layers.Dropout(0.2),\n     \n        layers.GlobalMaxPooling1D(),\n\n        layers.Dense(256, activation = \"relu\"),\n        layers.Dense(6, activation='sigmoid')\n    ]\n)","metadata":{"id":"JTIJBqKuppDc","execution":{"iopub.status.busy":"2023-01-14T07:19:57.014020Z","iopub.execute_input":"2023-01-14T07:19:57.015055Z","iopub.status.idle":"2023-01-14T07:20:01.859005Z","shell.execute_reply.started":"2023-01-14T07:19:57.015018Z","shell.execute_reply":"2023-01-14T07:20:01.857981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View the model summary\nmodel.summary()","metadata":{"id":"IfhZIIODsQUU","outputId":"d5aed5aa-c33d-480d-fbbe-07e251236b26","execution":{"iopub.status.busy":"2023-01-14T07:20:01.860617Z","iopub.execute_input":"2023-01-14T07:20:01.861075Z","iopub.status.idle":"2023-01-14T07:20:01.871128Z","shell.execute_reply.started":"2023-01-14T07:20:01.861038Z","shell.execute_reply":"2023-01-14T07:20:01.870034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize the Callbacks\nearly_stop = callbacks.EarlyStopping(patience = 5, restore_best_weights = True, verbose = 1)\nreduce_lr = callbacks.ReduceLROnPlateau(factor = 0.1, patience = 2, mode = 'min', verbose = 1)\n\n# Initialize optimizer for training our model\noptimizer = optimizers.Adam(learning_rate = 1e-4)\n\n# Compile the model and specify loss function, optimizer and metrics values to the model\nmodel.compile(loss = \"categorical_crossentropy\", optimizer = optimizer, metrics = [\"accuracy\"])","metadata":{"id":"7ZCcVWF6sxEs","execution":{"iopub.status.busy":"2023-01-14T07:20:01.872805Z","iopub.execute_input":"2023-01-14T07:20:01.873420Z","iopub.status.idle":"2023-01-14T07:20:01.890247Z","shell.execute_reply.started":"2023-01-14T07:20:01.873381Z","shell.execute_reply":"2023-01-14T07:20:01.889119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Start training the model\nmodel_training_history = model.fit(\n    x = X_train, y = y_train_encoded, epochs = 10, batch_size = 32,\n    shuffle = True, validation_split = 0.2, callbacks = [early_stop, reduce_lr])","metadata":{"id":"c66YmWLVsdib","outputId":"7f1b6650-9450-4dc4-9c31-29f2ee4ae25e","execution":{"iopub.status.busy":"2023-01-14T07:20:01.891746Z","iopub.execute_input":"2023-01-14T07:20:01.892121Z","iopub.status.idle":"2023-01-14T08:14:37.014582Z","shell.execute_reply.started":"2023-01-14T07:20:01.892067Z","shell.execute_reply":"2023-01-14T08:14:37.013616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model\nmodel.evaluate(X_test, y_test_encoded)","metadata":{"id":"kSvxoUub9pSL","outputId":"acb506eb-4525-4e70-da0b-ab453db7ca67","execution":{"iopub.status.busy":"2023-01-14T08:14:37.015964Z","iopub.execute_input":"2023-01-14T08:14:37.016354Z","iopub.status.idle":"2023-01-14T08:16:24.738687Z","shell.execute_reply.started":"2023-01-14T08:14:37.016316Z","shell.execute_reply":"2023-01-14T08:16:24.737784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Predictions**","metadata":{"id":"Y-bRLVFj-AXo"}},{"cell_type":"code","source":"# Get the prediction on the submission features\ny_pred = model.predict(encoded_padded_sub_features)","metadata":{"id":"_woSZzJF9vHu","outputId":"76b3048c-7fda-4486-82d9-90a7652cd441","execution":{"iopub.status.busy":"2023-01-14T08:16:24.740277Z","iopub.execute_input":"2023-01-14T08:16:24.740626Z","iopub.status.idle":"2023-01-14T08:18:48.351082Z","shell.execute_reply.started":"2023-01-14T08:16:24.740579Z","shell.execute_reply":"2023-01-14T08:18:48.350037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Inverse encode the predictions\ny_pred = enc.inverse_transform(y_pred).reshape((-1))\ny_pred","metadata":{"id":"J_c7ceWH-Isk","outputId":"19a29ec5-239c-4a9f-c6b1-a55ce63a1e5d","execution":{"iopub.status.busy":"2023-01-14T08:18:48.352765Z","iopub.execute_input":"2023-01-14T08:18:48.353985Z","iopub.status.idle":"2023-01-14T08:18:48.385056Z","shell.execute_reply.started":"2023-01-14T08:18:48.353947Z","shell.execute_reply":"2023-01-14T08:18:48.383988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import the submission file\nsub_file = pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_sample_submission.csv\")\n\n# View the submission file\nsub_file","metadata":{"id":"CAl-ygMI-qNH","outputId":"d1a75595-6c35-4ac2-e13b-11a4b9082374","execution":{"iopub.status.busy":"2023-01-14T08:19:36.066329Z","iopub.execute_input":"2023-01-14T08:19:36.066699Z","iopub.status.idle":"2023-01-14T08:19:37.024820Z","shell.execute_reply.started":"2023-01-14T08:19:36.066668Z","shell.execute_reply":"2023-01-14T08:19:37.023838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add our prediction to sub_file\nsub_file[\"rating\"] = y_pred\n\n# Submit the submission file\nsub_file.to_csv('submission.csv', index = False)","metadata":{"id":"t4G34PVjANwX","execution":{"iopub.status.busy":"2023-01-14T08:19:39.434624Z","iopub.execute_input":"2023-01-14T08:19:39.435003Z","iopub.status.idle":"2023-01-14T08:19:40.351002Z","shell.execute_reply.started":"2023-01-14T08:19:39.434970Z","shell.execute_reply":"2023-01-14T08:19:40.349970Z"},"trusted":true},"execution_count":null,"outputs":[]}]}