{"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":"2023-01-07T15:49:52.677791Z","iopub.execute_input":"2023-01-07T15:49:52.678382Z","iopub.status.idle":"2023-01-07T15:49:52.711368Z","shell.execute_reply.started":"2023-01-07T15:49:52.678270Z","shell.execute_reply":"2023-01-07T15:49:52.709761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport csv\nimport re \nimport string\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.feature_extraction.text import TfidfVectorizer","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:49:54.438138Z","iopub.execute_input":"2023-01-07T15:49:54.438646Z","iopub.status.idle":"2023-01-07T15:49:55.127267Z","shell.execute_reply.started":"2023-01-07T15:49:54.438609Z","shell.execute_reply":"2023-01-07T15:49:55.125834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_train.csv\")\ntest = pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:49:56.323912Z","iopub.execute_input":"2023-01-07T15:49:56.324396Z","iopub.status.idle":"2023-01-07T15:50:39.496619Z","shell.execute_reply.started":"2023-01-07T15:49:56.324361Z","shell.execute_reply":"2023-01-07T15:50:39.495249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0=train[train['rating']==0]\ntrain1=train[train['rating']==1]\ntrain2=train[train['rating']==2]\ntrain3=train[train['rating']==3]\ntrain4=train[train['rating']==4]\ntrain5=train[train['rating']==5]","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:50:39.499023Z","iopub.execute_input":"2023-01-07T15:50:39.499398Z","iopub.status.idle":"2023-01-07T15:50:39.788441Z","shell.execute_reply.started":"2023-01-07T15:50:39.499365Z","shell.execute_reply":"2023-01-07T15:50:39.787180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0.shape, train1.shape, train2.shape, train3.shape, train4.shape, train5.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:50:39.790145Z","iopub.execute_input":"2023-01-07T15:50:39.790508Z","iopub.status.idle":"2023-01-07T15:50:39.801581Z","shell.execute_reply.started":"2023-01-07T15:50:39.790477Z","shell.execute_reply":"2023-01-07T15:50:39.800319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0=train0[:int(train0.shape[0]/100)]\ntrain1=train1[:int(train1.shape[0]/100)]\ntrain2=train2[:int(train2.shape[0]/100)]\ntrain3=train3[:int(train3.shape[0]/100)]\ntrain4=train4[:int(train4.shape[0]/100)]\ntrain5=train5[:int(train5.shape[0]/100)]","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:51:29.345186Z","iopub.execute_input":"2023-01-07T15:51:29.345824Z","iopub.status.idle":"2023-01-07T15:51:29.354426Z","shell.execute_reply.started":"2023-01-07T15:51:29.345781Z","shell.execute_reply":"2023-01-07T15:51:29.353019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0.shape, train1.shape, train2.shape, train3.shape, train4.shape, train5.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:51:30.302905Z","iopub.execute_input":"2023-01-07T15:51:30.304674Z","iopub.status.idle":"2023-01-07T15:51:30.314839Z","shell.execute_reply.started":"2023-01-07T15:51:30.304603Z","shell.execute_reply":"2023-01-07T15:51:30.313101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.concat([train0,train1,train2,train3,train4,train5],axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:51:31.313260Z","iopub.execute_input":"2023-01-07T15:51:31.314693Z","iopub.status.idle":"2023-01-07T15:51:31.385617Z","shell.execute_reply.started":"2023-01-07T15:51:31.314637Z","shell.execute_reply":"2023-01-07T15:51:31.384379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:51:32.315974Z","iopub.execute_input":"2023-01-07T15:51:32.316416Z","iopub.status.idle":"2023-01-07T15:51:32.323319Z","shell.execute_reply.started":"2023-01-07T15:51:32.316385Z","shell.execute_reply":"2023-01-07T15:51:32.322139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:51:33.277533Z","iopub.execute_input":"2023-01-07T15:51:33.277975Z","iopub.status.idle":"2023-01-07T15:51:33.285733Z","shell.execute_reply.started":"2023-01-07T15:51:33.277938Z","shell.execute_reply":"2023-01-07T15:51:33.284343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\n#plotting the heatmap for correlation\nax = sns.heatmap(train.corr(), annot=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:51:34.348770Z","iopub.execute_input":"2023-01-07T15:51:34.350083Z","iopub.status.idle":"2023-01-07T15:51:34.865878Z","shell.execute_reply.started":"2023-01-07T15:51:34.350043Z","shell.execute_reply":"2023-01-07T15:51:34.864675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(['user_id', 'book_id', 'review_id', 'date_added', 'date_updated', 'n_votes','read_at', 'started_at','n_comments'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:51:37.410126Z","iopub.execute_input":"2023-01-07T15:51:37.410538Z","iopub.status.idle":"2023-01-07T15:51:37.425715Z","shell.execute_reply.started":"2023-01-07T15:51:37.410507Z","shell.execute_reply":"2023-01-07T15:51:37.424094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:51:56.553390Z","iopub.execute_input":"2023-01-07T15:51:56.553943Z","iopub.status.idle":"2023-01-07T15:51:56.575398Z","shell.execute_reply.started":"2023-01-07T15:51:56.553894Z","shell.execute_reply":"2023-01-07T15:51:56.574170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.columns\n","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:51:57.706003Z","iopub.execute_input":"2023-01-07T15:51:57.706477Z","iopub.status.idle":"2023-01-07T15:51:57.716284Z","shell.execute_reply.started":"2023-01-07T15:51:57.706442Z","shell.execute_reply":"2023-01-07T15:51:57.714561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.drop(['user_id', 'book_id', 'review_id', 'date_added','date_updated', 'read_at', 'started_at','n_comments'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:51:58.651041Z","iopub.execute_input":"2023-01-07T15:51:58.651440Z","iopub.status.idle":"2023-01-07T15:51:58.728211Z","shell.execute_reply.started":"2023-01-07T15:51:58.651408Z","shell.execute_reply":"2023-01-07T15:51:58.726400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:51:59.560303Z","iopub.execute_input":"2023-01-07T15:51:59.560783Z","iopub.status.idle":"2023-01-07T15:51:59.576961Z","shell.execute_reply.started":"2023-01-07T15:51:59.560746Z","shell.execute_reply":"2023-01-07T15:51:59.575261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Remove Hashtags\ntrain['review_text'].replace( { r\"#(\\w+)\" : '' }, inplace= True, regex = True)\n\n#Remove Mention\ntrain['review_text'].replace( { r\"@(\\w+)\" : '' }, inplace= True, regex = True)\n\n#Remove URL\ntrain['review_text'].astype(str).replace( { r\"http\\S+\" : '' }, inplace= True, regex = True)\n\ntrain['review_text']=train['review_text'].str.lower()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:52:00.425601Z","iopub.execute_input":"2023-01-07T15:52:00.426034Z","iopub.status.idle":"2023-01-07T15:52:00.525301Z","shell.execute_reply.started":"2023-01-07T15:52:00.426003Z","shell.execute_reply":"2023-01-07T15:52:00.524002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Remove Hashtags\ntest['review_text'].replace( { r\"#(\\w+)\" : '' }, inplace= True, regex = True)\n\n#Remove Mention\ntest['review_text'].replace( { r\"@(\\w+)\" : '' }, inplace= True, regex = True)\n\n#Remove URL\ntest['review_text'].astype(str).replace( { r\"http\\S+\" : '' }, inplace= True, regex = True)\n\ntest['review_text'] = test['review_text'].str.lower()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:52:01.625442Z","iopub.execute_input":"2023-01-07T15:52:01.625935Z","iopub.status.idle":"2023-01-07T15:52:06.302513Z","shell.execute_reply.started":"2023-01-07T15:52:01.625898Z","shell.execute_reply":"2023-01-07T15:52:06.301540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import stopwords with nltk.\nfrom nltk.corpus import stopwords\nstop = stopwords.words('english')","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:52:21.264753Z","iopub.execute_input":"2023-01-07T15:52:21.265249Z","iopub.status.idle":"2023-01-07T15:52:21.805742Z","shell.execute_reply.started":"2023-01-07T15:52:21.265211Z","shell.execute_reply":"2023-01-07T15:52:21.804597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['review_text'] = train['review_text'].apply(lambda x: ' '.join([word for word in x.split() if word not in (stop)]))\n\ntest['review_text'] = test['review_text'].apply(lambda x: ' '.join([word for word in x.split() if word not in (stop)]))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T15:52:23.455930Z","iopub.execute_input":"2023-01-07T15:52:23.456344Z","iopub.status.idle":"2023-01-07T15:55:53.280190Z","shell.execute_reply.started":"2023-01-07T15:52:23.456309Z","shell.execute_reply":"2023-01-07T15:55:53.279142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:01:18.556534Z","iopub.execute_input":"2023-01-07T16:01:18.557070Z","iopub.status.idle":"2023-01-07T16:01:18.570532Z","shell.execute_reply.started":"2023-01-07T16:01:18.557024Z","shell.execute_reply":"2023-01-07T16:01:18.569329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:01:19.694246Z","iopub.execute_input":"2023-01-07T16:01:19.694712Z","iopub.status.idle":"2023-01-07T16:01:19.708524Z","shell.execute_reply.started":"2023-01-07T16:01:19.694662Z","shell.execute_reply":"2023-01-07T16:01:19.706819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:01:27.719994Z","iopub.execute_input":"2023-01-07T16:01:27.720468Z","iopub.status.idle":"2023-01-07T16:01:27.728502Z","shell.execute_reply.started":"2023-01-07T16:01:27.720431Z","shell.execute_reply":"2023-01-07T16:01:27.727205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:01:28.714722Z","iopub.execute_input":"2023-01-07T16:01:28.715146Z","iopub.status.idle":"2023-01-07T16:01:28.722086Z","shell.execute_reply.started":"2023-01-07T16:01:28.715113Z","shell.execute_reply":"2023-01-07T16:01:28.721203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['rating'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:01:32.166802Z","iopub.execute_input":"2023-01-07T16:01:32.167601Z","iopub.status.idle":"2023-01-07T16:01:32.181094Z","shell.execute_reply.started":"2023-01-07T16:01:32.167550Z","shell.execute_reply":"2023-01-07T16:01:32.179430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_temp, X_test, y_temp, y_test = train_test_split(train, list(train.rating), test_size=0.2, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:02:26.285074Z","iopub.execute_input":"2023-01-07T16:02:26.285602Z","iopub.status.idle":"2023-01-07T16:02:26.298705Z","shell.execute_reply.started":"2023-01-07T16:02:26.285562Z","shell.execute_reply":"2023-01-07T16:02:26.297237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_temp['rating'] = y_temp","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:02:27.437875Z","iopub.execute_input":"2023-01-07T16:02:27.438419Z","iopub.status.idle":"2023-01-07T16:02:27.447677Z","shell.execute_reply.started":"2023-01-07T16:02:27.438378Z","shell.execute_reply":"2023-01-07T16:02:27.446398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_temp.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:02:28.774401Z","iopub.execute_input":"2023-01-07T16:02:28.774952Z","iopub.status.idle":"2023-01-07T16:02:28.786945Z","shell.execute_reply.started":"2023-01-07T16:02:28.774908Z","shell.execute_reply":"2023-01-07T16:02:28.785494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zero = X_temp[X_temp['rating'] == 0]\nzero.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:02:29.208773Z","iopub.execute_input":"2023-01-07T16:02:29.209230Z","iopub.status.idle":"2023-01-07T16:02:29.218107Z","shell.execute_reply.started":"2023-01-07T16:02:29.209196Z","shell.execute_reply":"2023-01-07T16:02:29.217107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"one = X_temp[X_temp['rating'] == 1]\none.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:02:30.475238Z","iopub.execute_input":"2023-01-07T16:02:30.476396Z","iopub.status.idle":"2023-01-07T16:02:30.486645Z","shell.execute_reply.started":"2023-01-07T16:02:30.476349Z","shell.execute_reply":"2023-01-07T16:02:30.485215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"two = X_temp[X_temp['rating'] == 2]\ntwo.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:02:30.754463Z","iopub.execute_input":"2023-01-07T16:02:30.754867Z","iopub.status.idle":"2023-01-07T16:02:30.766274Z","shell.execute_reply.started":"2023-01-07T16:02:30.754836Z","shell.execute_reply":"2023-01-07T16:02:30.764839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"three = X_temp[X_temp['rating'] == 3]\nthree.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:02:31.038454Z","iopub.execute_input":"2023-01-07T16:02:31.038933Z","iopub.status.idle":"2023-01-07T16:02:31.049870Z","shell.execute_reply.started":"2023-01-07T16:02:31.038896Z","shell.execute_reply":"2023-01-07T16:02:31.048426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"four = X_temp[X_temp['rating'] == 4]\nfour.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:02:31.391345Z","iopub.execute_input":"2023-01-07T16:02:31.391863Z","iopub.status.idle":"2023-01-07T16:02:31.403660Z","shell.execute_reply.started":"2023-01-07T16:02:31.391820Z","shell.execute_reply":"2023-01-07T16:02:31.402571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"five = X_temp[X_temp['rating'] == 5]\nfive.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:02:35.631992Z","iopub.execute_input":"2023-01-07T16:02:35.632426Z","iopub.status.idle":"2023-01-07T16:02:35.643304Z","shell.execute_reply.started":"2023-01-07T16:02:35.632393Z","shell.execute_reply":"2023-01-07T16:02:35.641858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = pd.concat([zero,one,two,three,four,five], axis = 0)\nds","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:02:58.290266Z","iopub.execute_input":"2023-01-07T16:02:58.290648Z","iopub.status.idle":"2023-01-07T16:02:58.313455Z","shell.execute_reply.started":"2023-01-07T16:02:58.290619Z","shell.execute_reply":"2023-01-07T16:02:58.311559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf = X_test\ntestdf['rating'] = y_test\ntestdf.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:03:14.900674Z","iopub.execute_input":"2023-01-07T16:03:14.901109Z","iopub.status.idle":"2023-01-07T16:03:14.909056Z","shell.execute_reply.started":"2023-01-07T16:03:14.901076Z","shell.execute_reply":"2023-01-07T16:03:14.907966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = pd.concat([ds, testdf], axis = 0)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:03:20.819444Z","iopub.execute_input":"2023-01-07T16:03:20.819876Z","iopub.status.idle":"2023-01-07T16:03:20.828592Z","shell.execute_reply.started":"2023-01-07T16:03:20.819844Z","shell.execute_reply":"2023-01-07T16:03:20.827081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds[ds['rating']==0].shape, ds[ds['rating']==1].shape, ds[ds['rating']==2].shape, ds[ds['rating']==3].shape, ds[ds['rating']==4].shape, ds[ds['rating']==5].shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:03:57.183230Z","iopub.execute_input":"2023-01-07T16:03:57.183671Z","iopub.status.idle":"2023-01-07T16:03:57.199411Z","shell.execute_reply.started":"2023-01-07T16:03:57.183640Z","shell.execute_reply":"2023-01-07T16:03:57.197949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corpus = []\nfor i in range(ds.shape[0]):\n    corpus.append(ds.iloc[i][0])","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:03:59.492463Z","iopub.execute_input":"2023-01-07T16:03:59.493644Z","iopub.status.idle":"2023-01-07T16:04:00.574998Z","shell.execute_reply.started":"2023-01-07T16:03:59.493596Z","shell.execute_reply":"2023-01-07T16:04:00.573823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer1 = TfidfVectorizer(max_features=1000)\nX1 = vectorizer1.fit_transform(ds['review_text'])\nfeature_names1 = vectorizer1.get_feature_names()\ndenselist1 = X1.todense().tolist()\ndf2_train = pd.DataFrame(denselist1, columns=feature_names1)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:04:02.307537Z","iopub.execute_input":"2023-01-07T16:04:02.307983Z","iopub.status.idle":"2023-01-07T16:04:06.066065Z","shell.execute_reply.started":"2023-01-07T16:04:02.307947Z","shell.execute_reply":"2023-01-07T16:04:06.065182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdf = df2_train\ntdf['labelxyz'] = list(ds.rating)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:04:06.365883Z","iopub.execute_input":"2023-01-07T16:04:06.366762Z","iopub.status.idle":"2023-01-07T16:04:06.379175Z","shell.execute_reply.started":"2023-01-07T16:04:06.366667Z","shell.execute_reply":"2023-01-07T16:04:06.377765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdf_zero = tdf[tdf.labelxyz == 0]\ntdf_zero.shape\n\ntdf_one = tdf[tdf.labelxyz == 1]\ntdf_one.shape\n\ntdf_two = tdf[tdf.labelxyz == 2]\ntdf_two.shape\n\ntdf_three = tdf[tdf.labelxyz == 3]\ntdf_three.shape\n\ntdf_four = tdf[tdf.labelxyz == 4]\ntdf_four.shape\n\ntdf_five = tdf[tdf.labelxyz == 5]\ntdf_five.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:04:40.628132Z","iopub.execute_input":"2023-01-07T16:04:40.629393Z","iopub.status.idle":"2023-01-07T16:04:40.710114Z","shell.execute_reply.started":"2023-01-07T16:04:40.629349Z","shell.execute_reply":"2023-01-07T16:04:40.708577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_zero = tdf_zero.sample(frac=0.9, random_state=0)\nX_test_zero = tdf_zero.drop(X_train_zero.index)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:04:46.414416Z","iopub.execute_input":"2023-01-07T16:04:46.415263Z","iopub.status.idle":"2023-01-07T16:04:46.427049Z","shell.execute_reply.started":"2023-01-07T16:04:46.415195Z","shell.execute_reply":"2023-01-07T16:04:46.425245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_one = tdf_zero.sample(frac=0.9, random_state=0)\nX_test_one = tdf_zero.drop(X_train_one.index)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:04:50.045314Z","iopub.execute_input":"2023-01-07T16:04:50.045817Z","iopub.status.idle":"2023-01-07T16:04:50.057212Z","shell.execute_reply.started":"2023-01-07T16:04:50.045779Z","shell.execute_reply":"2023-01-07T16:04:50.055792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_two = tdf_two.sample(frac=0.9, random_state=0)\nX_test_two = tdf_two.drop(X_train_two.index)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:04:51.213543Z","iopub.execute_input":"2023-01-07T16:04:51.214571Z","iopub.status.idle":"2023-01-07T16:04:51.230626Z","shell.execute_reply.started":"2023-01-07T16:04:51.214529Z","shell.execute_reply":"2023-01-07T16:04:51.229207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_three = tdf_three.sample(frac=0.9, random_state=0)\nX_test_three = tdf_three.drop(X_train_three.index)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:05:04.682633Z","iopub.execute_input":"2023-01-07T16:05:04.683070Z","iopub.status.idle":"2023-01-07T16:05:04.709609Z","shell.execute_reply.started":"2023-01-07T16:05:04.683039Z","shell.execute_reply":"2023-01-07T16:05:04.708602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_four = tdf_four.sample(frac=0.9, random_state=0)\nX_test_four = tdf_four.drop(X_train_four.index)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:05:31.457543Z","iopub.execute_input":"2023-01-07T16:05:31.457932Z","iopub.status.idle":"2023-01-07T16:05:31.488470Z","shell.execute_reply.started":"2023-01-07T16:05:31.457901Z","shell.execute_reply":"2023-01-07T16:05:31.487154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_five = tdf_five.sample(frac=0.9, random_state=0)\nX_test_five = tdf_five.drop(X_train_five.index)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:05:52.193996Z","iopub.execute_input":"2023-01-07T16:05:52.194461Z","iopub.status.idle":"2023-01-07T16:05:52.232188Z","shell.execute_reply.started":"2023-01-07T16:05:52.194429Z","shell.execute_reply":"2023-01-07T16:05:52.231236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_df = pd.concat([X_train_zero,X_train_one,X_train_two, X_train_three, X_train_four, X_train_five], axis = 0)\ntfdf_train = X_train_df.drop(['labelxyz'], axis = 1)\ny_train = list(X_train_df.labelxyz)\nX_test_df = pd.concat([X_test_zero,X_test_one,X_test_two, X_test_three, X_test_four, X_test_five], axis = 0)\ntfdf_test = X_test_df.drop(['labelxyz'], axis = 1)\ny_test = list(X_test_df.labelxyz)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:08:07.442385Z","iopub.execute_input":"2023-01-07T16:08:07.442928Z","iopub.status.idle":"2023-01-07T16:08:07.534119Z","shell.execute_reply.started":"2023-01-07T16:08:07.442887Z","shell.execute_reply":"2023-01-07T16:08:07.533202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = {'TF-IDF':[],'BOW':[]}","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:08:14.505394Z","iopub.execute_input":"2023-01-07T16:08:14.505848Z","iopub.status.idle":"2023-01-07T16:08:14.511907Z","shell.execute_reply.started":"2023-01-07T16:08:14.505813Z","shell.execute_reply":"2023-01-07T16:08:14.510939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.ensemble import GradientBoostingClassifier\n\nfrom sklearn import metrics\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom sklearn.metrics import plot_confusion_matrix\nfrom sklearn.metrics import precision_score, recall_score, f1_score\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:08:22.574899Z","iopub.execute_input":"2023-01-07T16:08:22.575282Z","iopub.status.idle":"2023-01-07T16:08:22.757288Z","shell.execute_reply.started":"2023-01-07T16:08:22.575251Z","shell.execute_reply":"2023-01-07T16:08:22.756396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Logistic Regression","metadata":{}},{"cell_type":"code","source":"len(tfdf_train), len(y_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:08:40.175428Z","iopub.execute_input":"2023-01-07T16:08:40.175827Z","iopub.status.idle":"2023-01-07T16:08:40.184342Z","shell.execute_reply.started":"2023-01-07T16:08:40.175795Z","shell.execute_reply":"2023-01-07T16:08:40.182910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tfdf_test), len(y_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:08:55.058177Z","iopub.execute_input":"2023-01-07T16:08:55.058605Z","iopub.status.idle":"2023-01-07T16:08:55.064875Z","shell.execute_reply.started":"2023-01-07T16:08:55.058572Z","shell.execute_reply":"2023-01-07T16:08:55.064106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#TF-IDF\nregressor_LR_tf = LogisticRegression(C=1.0,penalty='l2',solver='newton-cg')\nregressor_LR_tf.fit(tfdf_train,y_train)\ny_predict_LR_tf = regressor_LR_tf.predict(tfdf_test)\na=(regressor_LR_tf.score(tfdf_test,y_test))\naccuracy['TF-IDF'].append(a)\n\nprint(metrics.confusion_matrix(y_test, y_predict_LR_tf))\nprint(metrics.classification_report(y_test, y_predict_LR_tf))\nprint(metrics.accuracy_score(y_test, y_predict_LR_tf))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:11:09.724636Z","iopub.execute_input":"2023-01-07T16:11:09.725098Z","iopub.status.idle":"2023-01-07T16:11:13.990101Z","shell.execute_reply.started":"2023-01-07T16:11:09.725062Z","shell.execute_reply":"2023-01-07T16:11:13.988935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Decision Tree","metadata":{}},{"cell_type":"code","source":"#TF-IDF\nmodel_DT_tf = DecisionTreeClassifier(criterion = 'gini', max_depth=2) \nmodel_DT_tf.fit(tfdf_train,y_train)\ny_predict_DT_tf = model_DT_tf.predict(tfdf_test)\na=(model_DT_tf.score(tfdf_test,y_test))\naccuracy['TF-IDF'].append(a)\n\nprint(metrics.confusion_matrix(y_test, y_predict_DT_tf))\nprint(metrics.classification_report(y_test, y_predict_DT_tf))\nprint(metrics.accuracy_score(y_test, y_predict_DT_tf))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:11:40.530285Z","iopub.execute_input":"2023-01-07T16:11:40.530976Z","iopub.status.idle":"2023-01-07T16:11:40.795701Z","shell.execute_reply.started":"2023-01-07T16:11:40.530920Z","shell.execute_reply":"2023-01-07T16:11:40.794177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Gradient Boosting","metadata":{}},{"cell_type":"code","source":"#TF-IDF\nmodel_GB_tf = GradientBoostingClassifier(learning_rate= 0.1, max_depth=3, n_estimators= 100,subsample=1.0) \nmodel_GB_tf.fit(tfdf_train,y_train)\ny_predict_GB_tf = model_GB_tf.predict(tfdf_test)\na=(model_GB_tf.score(tfdf_test,y_test))\naccuracy['TF-IDF'].append(a)\nprint(metrics.confusion_matrix(y_test, y_predict_GB_tf))\nprint(metrics.classification_report(y_test, y_predict_GB_tf))\nprint(metrics.accuracy_score(y_test, y_predict_GB_tf))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:12:09.521843Z","iopub.execute_input":"2023-01-07T16:12:09.522378Z","iopub.status.idle":"2023-01-07T16:13:54.383044Z","shell.execute_reply.started":"2023-01-07T16:12:09.522335Z","shell.execute_reply":"2023-01-07T16:13:54.381953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Random Forest","metadata":{}},{"cell_type":"code","source":"#TF-IDF\nmodel_RF_tf = RandomForestClassifier(n_estimators= 100, max_features = 'log2')\nmodel_RF_tf.fit(tfdf_train,y_train)\ny_predict_RF_tf = model_RF_tf.predict(tfdf_test)\na=(model_RF_tf.score(tfdf_test,y_test))\naccuracy['TF-IDF'].append(a)\n\nprint(metrics.confusion_matrix(y_test, y_predict_RF_tf))\nprint(metrics.classification_report(y_test, y_predict_RF_tf))\nprint(metrics.accuracy_score(y_test, y_predict_RF_tf))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:13:54.384818Z","iopub.execute_input":"2023-01-07T16:13:54.385147Z","iopub.status.idle":"2023-01-07T16:13:58.586802Z","shell.execute_reply.started":"2023-01-07T16:13:54.385118Z","shell.execute_reply":"2023-01-07T16:13:58.585584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# K Nearest Neighbours","metadata":{}},{"cell_type":"code","source":"#TF-IDF\nmodel_KN_tf = KNeighborsClassifier(metric= 'manhattan', n_neighbors= 5, weights= 'distance') \nmodel_KN_tf.fit(tfdf_train,y_train)\ny_predict_KN_tf = model_KN_tf.predict(tfdf_test)\na=(model_KN_tf.score(tfdf_test,y_test))\naccuracy['TF-IDF'].append(a)\nprint(metrics.confusion_matrix(y_test, y_predict_KN_tf))\nprint(metrics.classification_report(y_test, y_predict_KN_tf))\nprint(metrics.accuracy_score(y_test, y_predict_KN_tf))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:13:58.588165Z","iopub.execute_input":"2023-01-07T16:13:58.588759Z","iopub.status.idle":"2023-01-07T16:15:03.486180Z","shell.execute_reply.started":"2023-01-07T16:13:58.588722Z","shell.execute_reply":"2023-01-07T16:15:03.484827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = ['LR','DT','GB','RF','KN']\ndata = {'model':model,'accuracy':accuracy['TF-IDF']}\ncompare_models = pd.DataFrame(data)\ncompare_models","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:15:55.764515Z","iopub.execute_input":"2023-01-07T16:15:55.765073Z","iopub.status.idle":"2023-01-07T16:15:55.780308Z","shell.execute_reply.started":"2023-01-07T16:15:55.765033Z","shell.execute_reply":"2023-01-07T16:15:55.778339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# RNN","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom nltk import word_tokenize\n\ndata = train['review_text'].map(word_tokenize).values\ntotal_vocabulary = set(word.lower() for tweet in data for word in tweet)  # set created from nested comprehension \nprint('There are {} unique words in the dataset.'.format(len(total_vocabulary)))\nprint('There are {} unique tweets in the dataset.'.format(len(data)))\n\n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:16:20.905157Z","iopub.execute_input":"2023-01-07T16:16:20.905542Z","iopub.status.idle":"2023-01-07T16:16:35.061051Z","shell.execute_reply.started":"2023-01-07T16:16:20.905512Z","shell.execute_reply":"2023-01-07T16:16:35.060242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.sequence import pad_sequences\nfrom keras.layers import Input, Dense, LSTM, Embedding\nfrom keras.layers import Dropout, Activation, Bidirectional, GlobalMaxPool1D\nfrom keras.models import Sequential\nfrom keras import initializers, regularizers, constraints, optimizers, layers\nfrom keras.preprocessing import text, sequence\n\n# set the emotion/sentiment as our target\ntarget = train['rating']\n\n# use one hot encoding since our target is categorical\ny = pd.get_dummies(target).values\n\n# use keras to create a Tokenizer object\ntokenizer = text.Tokenizer(num_words=20000)  # limit to the num_words most important ones\ntokenizer.fit_on_texts(list(train['review_text']))\ntokenized_texts = tokenizer.texts_to_sequences(train['review_text'])\nX = sequence.pad_sequences(tokenized_texts, maxlen=100)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:17:09.875179Z","iopub.execute_input":"2023-01-07T16:17:09.875567Z","iopub.status.idle":"2023-01-07T16:17:11.538735Z","shell.execute_reply.started":"2023-01-07T16:17:09.875525Z","shell.execute_reply":"2023-01-07T16:17:11.537530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:17:16.575280Z","iopub.execute_input":"2023-01-07T16:17:16.576610Z","iopub.status.idle":"2023-01-07T16:17:16.582991Z","shell.execute_reply.started":"2023-01-07T16:17:16.576574Z","shell.execute_reply":"2023-01-07T16:17:16.581835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:17:25.385155Z","iopub.execute_input":"2023-01-07T16:17:25.385845Z","iopub.status.idle":"2023-01-07T16:17:25.394480Z","shell.execute_reply.started":"2023-01-07T16:17:25.385792Z","shell.execute_reply":"2023-01-07T16:17:25.393155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:17:33.995382Z","iopub.execute_input":"2023-01-07T16:17:33.995890Z","iopub.status.idle":"2023-01-07T16:17:34.006095Z","shell.execute_reply.started":"2023-01-07T16:17:33.995854Z","shell.execute_reply":"2023-01-07T16:17:34.004889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:17:42.669986Z","iopub.execute_input":"2023-01-07T16:17:42.670430Z","iopub.status.idle":"2023-01-07T16:17:42.679265Z","shell.execute_reply.started":"2023-01-07T16:17:42.670395Z","shell.execute_reply":"2023-01-07T16:17:42.677948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create my NN model\nmodel = Sequential()\n\nembedding_size = 128\nmodel.add(Embedding(len(total_vocabulary), embedding_size))\nmodel.add(LSTM(25, return_sequences=True))\nmodel.add(GlobalMaxPool1D())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(50, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(50, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(6, activation='softmax')) ","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:18:13.106211Z","iopub.execute_input":"2023-01-07T16:18:13.106671Z","iopub.status.idle":"2023-01-07T16:18:13.439294Z","shell.execute_reply.started":"2023-01-07T16:18:13.106639Z","shell.execute_reply":"2023-01-07T16:18:13.438150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', \n              optimizer='adam', \n              metrics=['accuracy'])\nmodel.summary() # check the shape","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:18:13.531320Z","iopub.execute_input":"2023-01-07T16:18:13.532814Z","iopub.status.idle":"2023-01-07T16:18:13.546673Z","shell.execute_reply.started":"2023-01-07T16:18:13.532769Z","shell.execute_reply":"2023-01-07T16:18:13.545011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit(X_train, y_train, epochs=10, batch_size=32, validation_split=0.1)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:18:14.013077Z","iopub.execute_input":"2023-01-07T16:18:14.013466Z","iopub.status.idle":"2023-01-07T16:22:58.570276Z","shell.execute_reply.started":"2023-01-07T16:18:14.013436Z","shell.execute_reply":"2023-01-07T16:22:58.568761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nfrom sklearn.metrics import confusion_matrix\n\ny_pred=model.predict(X_test) ","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:22:58.573860Z","iopub.execute_input":"2023-01-07T16:22:58.575236Z","iopub.status.idle":"2023-01-07T16:23:00.452149Z","shell.execute_reply.started":"2023-01-07T16:22:58.575186Z","shell.execute_reply":"2023-01-07T16:23:00.451192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_1 =  np.argmax(y_pred, axis=1)\ny_test = np.argmax(y_test, axis=1)\nprint(y_pred_1)\ny_test","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:23:00.453567Z","iopub.execute_input":"2023-01-07T16:23:00.454035Z","iopub.status.idle":"2023-01-07T16:23:00.463598Z","shell.execute_reply.started":"2023-01-07T16:23:00.453976Z","shell.execute_reply":"2023-01-07T16:23:00.462722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_test, y_pred_1)\nprint(cm)\n\naccuracy_score(y_test, y_pred_1)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:23:00.465454Z","iopub.execute_input":"2023-01-07T16:23:00.466391Z","iopub.status.idle":"2023-01-07T16:23:00.479965Z","shell.execute_reply.started":"2023-01-07T16:23:00.466355Z","shell.execute_reply":"2023-01-07T16:23:00.478792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(hist.history.keys())","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:23:00.481811Z","iopub.execute_input":"2023-01-07T16:23:00.482667Z","iopub.status.idle":"2023-01-07T16:23:00.489158Z","shell.execute_reply.started":"2023-01-07T16:23:00.482623Z","shell.execute_reply":"2023-01-07T16:23:00.488005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualize training history\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nplt.plot(hist.history['accuracy'])\nplt.plot(hist.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()\n# summarize history for loss\nplt.plot(hist.history['loss'])\nplt.plot(hist.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T16:23:00.490970Z","iopub.execute_input":"2023-01-07T16:23:00.491408Z","iopub.status.idle":"2023-01-07T16:23:01.330586Z","shell.execute_reply.started":"2023-01-07T16:23:00.491365Z","shell.execute_reply":"2023-01-07T16:23:01.329396Z"},"trusted":true},"execution_count":null,"outputs":[]}]}