{"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-29T05:42:37.163441Z","iopub.execute_input":"2023-01-29T05:42:37.164388Z","iopub.status.idle":"2023-01-29T05:42:37.195653Z","shell.execute_reply.started":"2023-01-29T05:42:37.164270Z","shell.execute_reply":"2023-01-29T05:42:37.194458Z"},"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-29T05:42:37.197813Z","iopub.execute_input":"2023-01-29T05:42:37.198261Z","iopub.status.idle":"2023-01-29T05:42:38.298903Z","shell.execute_reply.started":"2023-01-29T05:42:37.198222Z","shell.execute_reply":"2023-01-29T05:42:38.297447Z"},"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-29T05:42:41.847884Z","iopub.execute_input":"2023-01-29T05:42:41.849032Z","iopub.status.idle":"2023-01-29T05:43:22.511071Z","shell.execute_reply.started":"2023-01-29T05:42:41.848996Z","shell.execute_reply":"2023-01-29T05:43:22.510107Z"},"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-29T05:43:22.512866Z","iopub.execute_input":"2023-01-29T05:43:22.513177Z","iopub.status.idle":"2023-01-29T05:43:22.733866Z","shell.execute_reply.started":"2023-01-29T05:43:22.513150Z","shell.execute_reply":"2023-01-29T05:43:22.732689Z"},"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-29T05:43:22.735435Z","iopub.execute_input":"2023-01-29T05:43:22.736044Z","iopub.status.idle":"2023-01-29T05:43:22.745792Z","shell.execute_reply.started":"2023-01-29T05:43:22.736008Z","shell.execute_reply":"2023-01-29T05:43:22.744689Z"},"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-29T05:43:55.964194Z","iopub.execute_input":"2023-01-29T05:43:55.964717Z","iopub.status.idle":"2023-01-29T05:43:55.974129Z","shell.execute_reply.started":"2023-01-29T05:43:55.964686Z","shell.execute_reply":"2023-01-29T05:43:55.973059Z"},"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-29T05:44:03.944656Z","iopub.execute_input":"2023-01-29T05:44:03.945168Z","iopub.status.idle":"2023-01-29T05:44:03.955026Z","shell.execute_reply.started":"2023-01-29T05:44:03.945118Z","shell.execute_reply":"2023-01-29T05:44:03.953769Z"},"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-29T05:44:11.827257Z","iopub.execute_input":"2023-01-29T05:44:11.827714Z","iopub.status.idle":"2023-01-29T05:44:11.898304Z","shell.execute_reply.started":"2023-01-29T05:44:11.827678Z","shell.execute_reply":"2023-01-29T05:44:11.896650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:44:17.014890Z","iopub.execute_input":"2023-01-29T05:44:17.015429Z","iopub.status.idle":"2023-01-29T05:44:17.022944Z","shell.execute_reply.started":"2023-01-29T05:44:17.015387Z","shell.execute_reply":"2023-01-29T05:44:17.021946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:44:23.022322Z","iopub.execute_input":"2023-01-29T05:44:23.022870Z","iopub.status.idle":"2023-01-29T05:44:23.031625Z","shell.execute_reply.started":"2023-01-29T05:44:23.022822Z","shell.execute_reply":"2023-01-29T05:44:23.030330Z"},"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-29T05:44:28.364703Z","iopub.execute_input":"2023-01-29T05:44:28.365089Z","iopub.status.idle":"2023-01-29T05:44:28.786772Z","shell.execute_reply.started":"2023-01-29T05:44:28.365055Z","shell.execute_reply":"2023-01-29T05:44:28.786016Z"},"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-29T05:44:38.145876Z","iopub.execute_input":"2023-01-29T05:44:38.146285Z","iopub.status.idle":"2023-01-29T05:44:38.161535Z","shell.execute_reply.started":"2023-01-29T05:44:38.146250Z","shell.execute_reply":"2023-01-29T05:44:38.160048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:44:44.621911Z","iopub.execute_input":"2023-01-29T05:44:44.622334Z","iopub.status.idle":"2023-01-29T05:44:44.644648Z","shell.execute_reply.started":"2023-01-29T05:44:44.622307Z","shell.execute_reply":"2023-01-29T05:44:44.642885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating Bag of Words","metadata":{}},{"cell_type":"code","source":"dff = train.drop(['rating'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:48:06.826698Z","iopub.execute_input":"2023-01-29T05:48:06.827258Z","iopub.status.idle":"2023-01-29T05:48:06.835941Z","shell.execute_reply.started":"2023-01-29T05:48:06.827212Z","shell.execute_reply":"2023-01-29T05:48:06.833881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_temp, X_test, y_temp, y_test = train_test_split(dff, list(train.rating), test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:48:36.526635Z","iopub.execute_input":"2023-01-29T05:48:36.527111Z","iopub.status.idle":"2023-01-29T05:48:36.538464Z","shell.execute_reply.started":"2023-01-29T05:48:36.527075Z","shell.execute_reply":"2023-01-29T05:48:36.537034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape, X_temp.shape, len(y_test), len(y_temp)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:48:49.320906Z","iopub.execute_input":"2023-01-29T05:48:49.321340Z","iopub.status.idle":"2023-01-29T05:48:49.331608Z","shell.execute_reply.started":"2023-01-29T05:48:49.321307Z","shell.execute_reply":"2023-01-29T05:48:49.329977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_temp['label'] = y_temp","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:48:56.553885Z","iopub.execute_input":"2023-01-29T05:48:56.554262Z","iopub.status.idle":"2023-01-29T05:48:56.563003Z","shell.execute_reply.started":"2023-01-29T05:48:56.554236Z","shell.execute_reply":"2023-01-29T05:48:56.561436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_temp.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:49:06.482504Z","iopub.execute_input":"2023-01-29T05:49:06.482842Z","iopub.status.idle":"2023-01-29T05:49:06.496228Z","shell.execute_reply.started":"2023-01-29T05:49:06.482812Z","shell.execute_reply":"2023-01-29T05:49:06.495054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zero = X_temp[X_temp['label'] == 0]\nzero.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:49:36.433238Z","iopub.execute_input":"2023-01-29T05:49:36.433652Z","iopub.status.idle":"2023-01-29T05:49:36.444316Z","shell.execute_reply.started":"2023-01-29T05:49:36.433615Z","shell.execute_reply":"2023-01-29T05:49:36.442703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"one = X_temp[X_temp['label'] == 1]\none.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:49:45.201553Z","iopub.execute_input":"2023-01-29T05:49:45.202045Z","iopub.status.idle":"2023-01-29T05:49:45.209791Z","shell.execute_reply.started":"2023-01-29T05:49:45.202008Z","shell.execute_reply":"2023-01-29T05:49:45.209139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"two = X_temp[X_temp['label'] == 2]\ntwo.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:49:56.300084Z","iopub.execute_input":"2023-01-29T05:49:56.300476Z","iopub.status.idle":"2023-01-29T05:49:56.311819Z","shell.execute_reply.started":"2023-01-29T05:49:56.300443Z","shell.execute_reply":"2023-01-29T05:49:56.310001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"three = X_temp[X_temp['label'] == 3]\nthree.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:50:07.411493Z","iopub.execute_input":"2023-01-29T05:50:07.412004Z","iopub.status.idle":"2023-01-29T05:50:07.422992Z","shell.execute_reply.started":"2023-01-29T05:50:07.411953Z","shell.execute_reply":"2023-01-29T05:50:07.421473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"four = X_temp[X_temp['label'] == 4]\nfour.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:50:25.457311Z","iopub.execute_input":"2023-01-29T05:50:25.457706Z","iopub.status.idle":"2023-01-29T05:50:25.466028Z","shell.execute_reply.started":"2023-01-29T05:50:25.457674Z","shell.execute_reply":"2023-01-29T05:50:25.465129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"five = X_temp[X_temp['label'] == 5]\nfive.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:50:47.174109Z","iopub.execute_input":"2023-01-29T05:50:47.174545Z","iopub.status.idle":"2023-01-29T05:50:47.184332Z","shell.execute_reply.started":"2023-01-29T05:50:47.174515Z","shell.execute_reply":"2023-01-29T05:50:47.183426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = pd.concat([zero,one, two,three, four, five], axis = 0)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:51:01.793395Z","iopub.execute_input":"2023-01-29T05:51:01.793849Z","iopub.status.idle":"2023-01-29T05:51:01.806316Z","shell.execute_reply.started":"2023-01-29T05:51:01.793820Z","shell.execute_reply":"2023-01-29T05:51:01.804739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:51:09.127941Z","iopub.execute_input":"2023-01-29T05:51:09.128308Z","iopub.status.idle":"2023-01-29T05:51:09.140596Z","shell.execute_reply.started":"2023-01-29T05:51:09.128280Z","shell.execute_reply":"2023-01-29T05:51:09.139346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf = X_test\ntestdf['label'] = y_test\ntestdf.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:51:20.981504Z","iopub.execute_input":"2023-01-29T05:51:20.983264Z","iopub.status.idle":"2023-01-29T05:51:20.991172Z","shell.execute_reply.started":"2023-01-29T05:51:20.983196Z","shell.execute_reply":"2023-01-29T05:51:20.990009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(testdf[testdf['label']==0]),len(testdf[testdf['label']==1]), len(testdf[testdf['label']==2]), len(testdf[testdf['label']==3]), len(testdf[testdf['label']==4]), len(testdf[testdf['label']==5])","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:51:57.107124Z","iopub.execute_input":"2023-01-29T05:51:57.107557Z","iopub.status.idle":"2023-01-29T05:51:57.122603Z","shell.execute_reply.started":"2023-01-29T05:51:57.107525Z","shell.execute_reply":"2023-01-29T05:51:57.121074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = pd.concat([ds, testdf], axis = 0)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:52:10.975092Z","iopub.execute_input":"2023-01-29T05:52:10.975640Z","iopub.status.idle":"2023-01-29T05:52:10.983841Z","shell.execute_reply.started":"2023-01-29T05:52:10.975603Z","shell.execute_reply":"2023-01-29T05:52:10.982608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds[ds['label']==0].shape, ds[ds['label']==1].shape, ds[ds['label']==2].shape, ds[ds['label']==3].shape, ds[ds['label']==4].shape, ds[ds['label']==5].shape","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:52:46.948031Z","iopub.execute_input":"2023-01-29T05:52:46.948401Z","iopub.status.idle":"2023-01-29T05:52:46.964338Z","shell.execute_reply.started":"2023-01-29T05:52:46.948371Z","shell.execute_reply":"2023-01-29T05:52:46.962451Z"},"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-29T05:53:06.072135Z","iopub.execute_input":"2023-01-29T05:53:06.072625Z","iopub.status.idle":"2023-01-29T05:53:07.054725Z","shell.execute_reply.started":"2023-01-29T05:53:06.072594Z","shell.execute_reply":"2023-01-29T05:53:07.053681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer_train = CountVectorizer(max_features=1000)\nvocabulary_train = vectorizer_train.fit_transform(corpus)\nbow = pd.DataFrame(vocabulary_train.toarray(), columns = vectorizer_train.get_feature_names())","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:53:17.525576Z","iopub.execute_input":"2023-01-29T05:53:17.526263Z","iopub.status.idle":"2023-01-29T05:53:18.980566Z","shell.execute_reply.started":"2023-01-29T05:53:17.526228Z","shell.execute_reply":"2023-01-29T05:53:18.979047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bow['labelxyz'] = list(ds.label)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:53:24.219409Z","iopub.execute_input":"2023-01-29T05:53:24.219867Z","iopub.status.idle":"2023-01-29T05:53:24.232372Z","shell.execute_reply.started":"2023-01-29T05:53:24.219829Z","shell.execute_reply":"2023-01-29T05:53:24.230647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bow_zero = bow[bow.labelxyz == 0]\nbow_one = bow[bow.labelxyz == 1]\nbow_two = bow[bow.labelxyz == 2]\nbow_three = bow[bow.labelxyz == 3]\nbow_four = bow[bow.labelxyz == 4]\nbow_five = bow[bow.labelxyz == 5]\nX_train_zero = bow_zero.sample(frac=0.8, random_state=0)\nX_test_zero = bow_zero.drop(X_train_zero.index)\nX_train_one = bow_one.sample(frac=0.8, random_state=0)\nX_test_one = bow_one.drop(X_train_one.index)\nX_train_two = bow_two.sample(frac=0.8, random_state=0)\nX_test_two = bow_two.drop(X_train_two.index)\nX_train_three = bow_three.sample(frac=0.8, random_state=0)\nX_test_three = bow_three.drop(X_train_three.index)\nX_train_four = bow_four.sample(frac=0.8, random_state=0)\nX_test_four = bow_four.drop(X_train_four.index)\nX_train_five = bow_five.sample(frac=0.8, random_state=0)\nX_test_five = bow_five.drop(X_train_five.index)\nX_train_df = pd.concat([X_train_zero, X_train_one, X_train_two, X_train_three, X_train_four, X_train_five], axis = 0)\nbow_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)\nbow_test = X_test_df.drop(['labelxyz'], axis = 1)\ny_test = list(X_test_df.labelxyz)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:55:12.455530Z","iopub.execute_input":"2023-01-29T05:55:12.455950Z","iopub.status.idle":"2023-01-29T05:55:12.606897Z","shell.execute_reply.started":"2023-01-29T05:55:12.455915Z","shell.execute_reply":"2023-01-29T05:55:12.605431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building ML models","metadata":{}},{"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-29T05:55:46.643771Z","iopub.execute_input":"2023-01-29T05:55:46.644181Z","iopub.status.idle":"2023-01-29T05:55:46.895648Z","shell.execute_reply.started":"2023-01-29T05:55:46.644150Z","shell.execute_reply":"2023-01-29T05:55:46.894016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = ['LR','RF','DT','KN','GB']\naccuracy = {'BOW':[]}","metadata":{"execution":{"iopub.status.busy":"2023-01-29T05:55:54.012157Z","iopub.execute_input":"2023-01-29T05:55:54.012597Z","iopub.status.idle":"2023-01-29T05:55:54.017601Z","shell.execute_reply.started":"2023-01-29T05:55:54.012562Z","shell.execute_reply":"2023-01-29T05:55:54.016695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Logistic Regression","metadata":{}},{"cell_type":"code","source":"#BOW\nregressor_LR_bow = LogisticRegression(C= 1.0, penalty='l2', solver= 'liblinear')\nregressor_LR_bow.fit(bow_train,y_train)\ny_predict_LR_bow = regressor_LR_bow.predict(bow_test)\na=(regressor_LR_bow.score(bow_test,y_test))\naccuracy['BOW'].append(a)\nprint(metrics.confusion_matrix(y_test, y_predict_LR_bow))\nprint(metrics.classification_report(y_test, y_predict_LR_bow))\nprint(metrics.accuracy_score(y_test, y_predict_LR_bow))","metadata":{"execution":{"iopub.status.busy":"2023-01-29T06:00:19.678610Z","iopub.execute_input":"2023-01-29T06:00:19.679085Z","iopub.status.idle":"2023-01-29T06:00:24.779856Z","shell.execute_reply.started":"2023-01-29T06:00:19.679054Z","shell.execute_reply":"2023-01-29T06:00:24.779025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Random Forest","metadata":{}},{"cell_type":"code","source":"#BOW\nmodel_RF_bow = RandomForestClassifier(max_features='sqrt', n_estimators= 1000)\nmodel_RF_bow.fit(bow_train,y_train)\ny_predict_RF_bow = model_RF_bow.predict(bow_test)\na=(model_RF_bow.score(bow_test,y_test))\naccuracy['BOW'].append(a)\nprint(metrics.confusion_matrix(y_test, y_predict_RF_bow))\nprint(metrics.classification_report(y_test, y_predict_RF_bow))\nprint(metrics.accuracy_score(y_test, y_predict_RF_bow))","metadata":{"execution":{"iopub.status.busy":"2023-01-29T06:00:46.580454Z","iopub.execute_input":"2023-01-29T06:00:46.580868Z","iopub.status.idle":"2023-01-29T06:01:34.831708Z","shell.execute_reply.started":"2023-01-29T06:00:46.580835Z","shell.execute_reply":"2023-01-29T06:01:34.829723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Decision Tree","metadata":{}},{"cell_type":"code","source":"model_DT_bow = DecisionTreeClassifier(criterion= 'gini', max_depth= 2) \nmodel_DT_bow.fit(bow_train,y_train)\ny_predict_DT_bow = model_DT_bow.predict(bow_test)\na=(model_DT_bow.score(bow_test,y_test))\naccuracy['BOW'].append(a)\nprint(metrics.confusion_matrix(y_test, y_predict_DT_bow))\nprint(metrics.classification_report(y_test, y_predict_DT_bow))\nprint(metrics.accuracy_score(y_test, y_predict_DT_bow))","metadata":{"execution":{"iopub.status.busy":"2023-01-29T06:01:35.008605Z","iopub.execute_input":"2023-01-29T06:01:35.009920Z","iopub.status.idle":"2023-01-29T06:01:35.155389Z","shell.execute_reply.started":"2023-01-29T06:01:35.009871Z","shell.execute_reply":"2023-01-29T06:01:35.153991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## K neighbors","metadata":{}},{"cell_type":"code","source":"#BOW\nmodel_KN_bow = KNeighborsClassifier(metric= 'euclidean', n_neighbors= 19, weights= 'distance') \nmodel_KN_bow.fit(bow_train,y_train)\ny_predict_KN_bow = model_KN_bow.predict(bow_test)\na=(model_KN_bow.score(bow_test,y_test))\naccuracy['BOW'].append(a)\nprint(metrics.confusion_matrix(y_test, y_predict_KN_bow))\nprint(metrics.classification_report(y_test, y_predict_KN_bow))\nprint(metrics.accuracy_score(y_test, y_predict_KN_bow))","metadata":{"execution":{"iopub.status.busy":"2023-01-29T06:02:00.784452Z","iopub.execute_input":"2023-01-29T06:02:00.785087Z","iopub.status.idle":"2023-01-29T06:02:02.125822Z","shell.execute_reply.started":"2023-01-29T06:02:00.785037Z","shell.execute_reply":"2023-01-29T06:02:02.124417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Gradient Boosting","metadata":{}},{"cell_type":"code","source":"#BOW\nmodel_GB_bow = GradientBoostingClassifier(learning_rate= 0.1, max_depth= 3, n_estimators= 100, subsample= 1.0)\nmodel_GB_bow.fit(bow_train,y_train)\ny_predict_GB_bow = model_GB_bow.predict(bow_test)\na=(model_GB_bow.score(bow_test,y_test))\naccuracy['BOW'].append(a)\nprint(metrics.confusion_matrix(y_test, y_predict_GB_bow))\nprint(metrics.classification_report(y_test, y_predict_GB_bow))\nprint(metrics.accuracy_score(y_test, y_predict_GB_bow))","metadata":{"execution":{"iopub.status.busy":"2023-01-29T06:02:18.480910Z","iopub.execute_input":"2023-01-29T06:02:18.481327Z","iopub.status.idle":"2023-01-29T06:03:29.829222Z","shell.execute_reply.started":"2023-01-29T06:02:18.481292Z","shell.execute_reply":"2023-01-29T06:03:29.828161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Evaluation","metadata":{}},{"cell_type":"code","source":"data_bow = {'model':model,'accuracy':accuracy['BOW']}\ncompare_models_bow = pd.DataFrame(data_bow)\ncompare_models_bow","metadata":{"execution":{"iopub.status.busy":"2023-01-29T06:03:29.831152Z","iopub.execute_input":"2023-01-29T06:03:29.832180Z","iopub.status.idle":"2023-01-29T06:03:29.845561Z","shell.execute_reply.started":"2023-01-29T06:03:29.832143Z","shell.execute_reply":"2023-01-29T06:03:29.843885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install matplotlib==3.4.2","metadata":{"execution":{"iopub.status.busy":"2023-01-29T06:03:29.847344Z","iopub.execute_input":"2023-01-29T06:03:29.848733Z","iopub.status.idle":"2023-01-29T06:04:00.253021Z","shell.execute_reply.started":"2023-01-29T06:03:29.848682Z","shell.execute_reply":"2023-01-29T06:04:00.251858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x, y = compare_models_bow['model'],compare_models_bow['accuracy']\nind = np.arange(len(y))\nfig, ax = plt.subplots(figsize=(8,5))\nax.bar(ind, y)\nax.set_xticks(ind)\nax.set_xticklabels(x)\nax.bar_label(ax.containers[0], horizontalalignment='left')\nplt.show()\nfig.savefig('Comparision of Models, BoW - After Second Wave.pdf', format='pdf')","metadata":{"execution":{"iopub.status.busy":"2023-01-29T06:04:00.255817Z","iopub.execute_input":"2023-01-29T06:04:00.256242Z","iopub.status.idle":"2023-01-29T06:04:00.474898Z","shell.execute_reply.started":"2023-01-29T06:04:00.256204Z","shell.execute_reply":"2023-01-29T06:04:00.473449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}