{"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-05-01T12:38:12.504057Z","iopub.execute_input":"2023-05-01T12:38:12.504499Z","iopub.status.idle":"2023-05-01T12:38:12.523203Z","shell.execute_reply.started":"2023-05-01T12:38:12.504450Z","shell.execute_reply":"2023-05-01T12:38:12.521464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')\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-05-01T12:38:14.221962Z","iopub.execute_input":"2023-05-01T12:38:14.222452Z","iopub.status.idle":"2023-05-01T12:38:14.857265Z","shell.execute_reply.started":"2023-05-01T12:38:14.222410Z","shell.execute_reply":"2023-05-01T12:38:14.855891Z"},"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-05-01T12:38:16.879003Z","iopub.execute_input":"2023-05-01T12:38:16.879480Z","iopub.status.idle":"2023-05-01T12:38:38.991432Z","shell.execute_reply.started":"2023-05-01T12:38:16.879439Z","shell.execute_reply":"2023-05-01T12:38:38.989993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:38:59.169033Z","iopub.execute_input":"2023-05-01T12:38:59.169548Z","iopub.status.idle":"2023-05-01T12:38:59.194298Z","shell.execute_reply.started":"2023-05-01T12:38:59.169500Z","shell.execute_reply":"2023-05-01T12:38:59.192700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:39:00.844600Z","iopub.execute_input":"2023-05-01T12:39:00.845145Z","iopub.status.idle":"2023-05-01T12:39:00.871275Z","shell.execute_reply.started":"2023-05-01T12:39:00.845100Z","shell.execute_reply":"2023-05-01T12:39:00.869828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extract and seperate the data based on labels","metadata":{}},{"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-05-01T12:39:02.569274Z","iopub.execute_input":"2023-05-01T12:39:02.570082Z","iopub.status.idle":"2023-05-01T12:39:02.915899Z","shell.execute_reply.started":"2023-05-01T12:39:02.570007Z","shell.execute_reply":"2023-05-01T12:39:02.914719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Peeking at the distribution between each label ","metadata":{}},{"cell_type":"code","source":"train0.shape, train1.shape, train2.shape, train3.shape, train4.shape, train5.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:39:04.657081Z","iopub.execute_input":"2023-05-01T12:39:04.657617Z","iopub.status.idle":"2023-05-01T12:39:04.666653Z","shell.execute_reply.started":"2023-05-01T12:39:04.657572Z","shell.execute_reply":"2023-05-01T12:39:04.665285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reducing the data of each label by 100","metadata":{}},{"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-05-01T12:39:08.704071Z","iopub.execute_input":"2023-05-01T12:39:08.704597Z","iopub.status.idle":"2023-05-01T12:39:08.714212Z","shell.execute_reply.started":"2023-05-01T12:39:08.704551Z","shell.execute_reply":"2023-05-01T12:39:08.712666Z"},"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-05-01T12:39:11.203004Z","iopub.execute_input":"2023-05-01T12:39:11.204046Z","iopub.status.idle":"2023-05-01T12:39:11.212694Z","shell.execute_reply.started":"2023-05-01T12:39:11.203988Z","shell.execute_reply":"2023-05-01T12:39:11.211342Z"},"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-05-01T12:39:13.190527Z","iopub.execute_input":"2023-05-01T12:39:13.191415Z","iopub.status.idle":"2023-05-01T12:39:13.214087Z","shell.execute_reply.started":"2023-05-01T12:39:13.191358Z","shell.execute_reply":"2023-05-01T12:39:13.212988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:39:14.038010Z","iopub.execute_input":"2023-05-01T12:39:14.039608Z","iopub.status.idle":"2023-05-01T12:39:14.048827Z","shell.execute_reply.started":"2023-05-01T12:39:14.039534Z","shell.execute_reply":"2023-05-01T12:39:14.047314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_null = train.isnull().sum().sort_values(ascending = False)\npercent = ((train.isnull().sum()/train.isnull().count())*100).sort_values(ascending = False)\nprint(\"Total records = \", train.shape[0])\n\nmissing_data = pd.concat([total_null,percent.round(2)],axis=1,keys=['Total Missing','In Percent'])\nmissing_data.head(16)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:39:15.791630Z","iopub.execute_input":"2023-05-01T12:39:15.792137Z","iopub.status.idle":"2023-05-01T12:39:15.848469Z","shell.execute_reply.started":"2023-05-01T12:39:15.792095Z","shell.execute_reply":"2023-05-01T12:39:15.846988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:39:17.350424Z","iopub.execute_input":"2023-05-01T12:39:17.351215Z","iopub.status.idle":"2023-05-01T12:39:17.358446Z","shell.execute_reply.started":"2023-05-01T12:39:17.351168Z","shell.execute_reply":"2023-05-01T12:39:17.357238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plotting the heatmap correlation for dropping out the columns","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nax = sns.heatmap(train.corr(),annot=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:39:19.202733Z","iopub.execute_input":"2023-05-01T12:39:19.203204Z","iopub.status.idle":"2023-05-01T12:39:19.552349Z","shell.execute_reply.started":"2023-05-01T12:39:19.203168Z","shell.execute_reply":"2023-05-01T12:39:19.550510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(['user_id', 'book_id', 'review_id','date_added', 'date_updated', 'read_at', 'started_at', 'n_votes',\n       'n_comments'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:39:22.041114Z","iopub.execute_input":"2023-05-01T12:39:22.042162Z","iopub.status.idle":"2023-05-01T12:39:22.055528Z","shell.execute_reply.started":"2023-05-01T12:39:22.042104Z","shell.execute_reply":"2023-05-01T12:39:22.054213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:39:23.259650Z","iopub.execute_input":"2023-05-01T12:39:23.260821Z","iopub.status.idle":"2023-05-01T12:39:23.277589Z","shell.execute_reply.started":"2023-05-01T12:39:23.260768Z","shell.execute_reply":"2023-05-01T12:39:23.276477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.columns","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:39:24.813361Z","iopub.execute_input":"2023-05-01T12:39:24.814102Z","iopub.status.idle":"2023-05-01T12:39:24.823432Z","shell.execute_reply.started":"2023-05-01T12:39:24.814046Z","shell.execute_reply":"2023-05-01T12:39:24.821793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=test.drop(['user_id', 'book_id', 'review_id','date_added',\n       'date_updated', 'read_at', 'started_at','n_comments'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:39:26.694888Z","iopub.execute_input":"2023-05-01T12:39:26.695446Z","iopub.status.idle":"2023-05-01T12:39:26.725190Z","shell.execute_reply.started":"2023-05-01T12:39:26.695399Z","shell.execute_reply":"2023-05-01T12:39:26.723277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:39:28.189373Z","iopub.execute_input":"2023-05-01T12:39:28.189890Z","iopub.status.idle":"2023-05-01T12:39:28.207050Z","shell.execute_reply.started":"2023-05-01T12:39:28.189844Z","shell.execute_reply":"2023-05-01T12:39:28.205697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Cleaning","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"#remove hashtags\ntrain['review_text'].replace( { r\"#(\\w+)\" : '' }, inplace= True, regex = True)\n#Remove Mention\ntrain['review_text'].replace( { r\"@(\\w+)\" : '' }, inplace= True, regex = True)\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-05-01T12:39:30.864443Z","iopub.execute_input":"2023-05-01T12:39:30.864984Z","iopub.status.idle":"2023-05-01T12:39:30.966739Z","shell.execute_reply.started":"2023-05-01T12:39:30.864932Z","shell.execute_reply":"2023-05-01T12:39:30.965330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import stopwords with nltk.\nfrom nltk.corpus import stopwords\nstop = stopwords.words('english')\n","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:39:34.258630Z","iopub.execute_input":"2023-05-01T12:39:34.259149Z","iopub.status.idle":"2023-05-01T12:39:34.472407Z","shell.execute_reply.started":"2023-05-01T12:39:34.259105Z","shell.execute_reply":"2023-05-01T12:39:34.470989Z"},"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-05-01T12:39:37.469801Z","iopub.execute_input":"2023-05-01T12:39:37.470933Z","iopub.status.idle":"2023-05-01T12:43:21.140409Z","shell.execute_reply.started":"2023-05-01T12:39:37.470879Z","shell.execute_reply":"2023-05-01T12:43:21.138540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# After data cleaning","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:43:38.069950Z","iopub.execute_input":"2023-05-01T12:43:38.070441Z","iopub.status.idle":"2023-05-01T12:43:38.083305Z","shell.execute_reply.started":"2023-05-01T12:43:38.070397Z","shell.execute_reply":"2023-05-01T12:43:38.081761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:43:39.748510Z","iopub.execute_input":"2023-05-01T12:43:39.749070Z","iopub.status.idle":"2023-05-01T12:43:39.762528Z","shell.execute_reply.started":"2023-05-01T12:43:39.749020Z","shell.execute_reply":"2023-05-01T12:43:39.760497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:43:41.233446Z","iopub.execute_input":"2023-05-01T12:43:41.233964Z","iopub.status.idle":"2023-05-01T12:43:41.242456Z","shell.execute_reply.started":"2023-05-01T12:43:41.233917Z","shell.execute_reply":"2023-05-01T12:43:41.241041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:43:45.255919Z","iopub.execute_input":"2023-05-01T12:43:45.256392Z","iopub.status.idle":"2023-05-01T12:43:45.264542Z","shell.execute_reply.started":"2023-05-01T12:43:45.256352Z","shell.execute_reply":"2023-05-01T12:43:45.263253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['rating'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:43:59.639443Z","iopub.execute_input":"2023-05-01T12:43:59.640389Z","iopub.status.idle":"2023-05-01T12:43:59.651297Z","shell.execute_reply.started":"2023-05-01T12:43:59.640334Z","shell.execute_reply":"2023-05-01T12:43:59.650223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train['rating']\nx = train['review_text']","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:44:52.341850Z","iopub.execute_input":"2023-05-01T12:44:52.342384Z","iopub.status.idle":"2023-05-01T12:44:52.349994Z","shell.execute_reply.started":"2023-05-01T12:44:52.342344Z","shell.execute_reply":"2023-05-01T12:44:52.348283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:44:59.580008Z","iopub.execute_input":"2023-05-01T12:44:59.581757Z","iopub.status.idle":"2023-05-01T12:44:59.589615Z","shell.execute_reply.started":"2023-05-01T12:44:59.581666Z","shell.execute_reply":"2023-05-01T12:44:59.588638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Vectorization","metadata":{}},{"cell_type":"code","source":"# Creating a word corpus for vectorization\ncorpus = []\nfor i in range(x.shape[0]):\n    corpus.append(x.iloc[i])","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:45:46.481951Z","iopub.execute_input":"2023-05-01T12:45:46.482471Z","iopub.status.idle":"2023-05-01T12:45:46.560137Z","shell.execute_reply.started":"2023-05-01T12:45:46.482428Z","shell.execute_reply":"2023-05-01T12:45:46.558765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer1 = TfidfVectorizer(max_features=1000)\nX1 = vectorizer1.fit_transform(x)\nfeature_names1 = vectorizer1.get_feature_names()\ndenselist1 = X1.todense().tolist()\ntrain = pd.DataFrame(denselist1, columns=feature_names1)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:46:01.633552Z","iopub.execute_input":"2023-05-01T12:46:01.634207Z","iopub.status.idle":"2023-05-01T12:46:05.967212Z","shell.execute_reply.started":"2023-05-01T12:46:01.634151Z","shell.execute_reply":"2023-05-01T12:46:05.966177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# splitting the training and testing part from the data\nX_temp, X_test, y_temp, y_test = train_test_split(train, y, test_size=0.2, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:46:18.980446Z","iopub.execute_input":"2023-05-01T12:46:18.980954Z","iopub.status.idle":"2023-05-01T12:46:19.041271Z","shell.execute_reply.started":"2023-05-01T12:46:18.980910Z","shell.execute_reply":"2023-05-01T12:46:19.040176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_temp.shape, X_test.shape, y_temp.shape, y_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:46:27.508288Z","iopub.execute_input":"2023-05-01T12:46:27.508804Z","iopub.status.idle":"2023-05-01T12:46:27.517179Z","shell.execute_reply.started":"2023-05-01T12:46:27.508762Z","shell.execute_reply":"2023-05-01T12:46:27.515855Z"},"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-05-01T12:50:54.575334Z","iopub.execute_input":"2023-05-01T12:50:54.576042Z","iopub.status.idle":"2023-05-01T12:50:54.591432Z","shell.execute_reply.started":"2023-05-01T12:50:54.575976Z","shell.execute_reply":"2023-05-01T12:50:54.590377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = {'TF-IDF':[]}","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:50:55.724553Z","iopub.execute_input":"2023-05-01T12:50:55.725131Z","iopub.status.idle":"2023-05-01T12:50:55.731046Z","shell.execute_reply.started":"2023-05-01T12:50:55.725087Z","shell.execute_reply":"2023-05-01T12:50:55.729738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Logistic Regression","metadata":{}},{"cell_type":"code","source":"#TF-IDF\nregressor_LR_tf = LogisticRegression(C=1.0,penalty='l2',solver='newton-cg')\nregressor_LR_tf.fit(X_temp, y_temp)\ny_predict_LR_tf = regressor_LR_tf.predict(X_test)\na=(regressor_LR_tf.score(X_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-05-01T12:50:57.267050Z","iopub.execute_input":"2023-05-01T12:50:57.267558Z","iopub.status.idle":"2023-05-01T12:51:02.315447Z","shell.execute_reply.started":"2023-05-01T12:50:57.267520Z","shell.execute_reply":"2023-05-01T12:51:02.313709Z"},"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(X_temp, y_temp)\ny_predict_DT_tf = model_DT_tf.predict(X_test)\na=(model_DT_tf.score(X_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-05-01T12:51:20.317724Z","iopub.execute_input":"2023-05-01T12:51:20.319134Z","iopub.status.idle":"2023-05-01T12:51:20.558693Z","shell.execute_reply.started":"2023-05-01T12:51:20.319069Z","shell.execute_reply":"2023-05-01T12:51:20.557375Z"},"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(X_temp, y_temp)\ny_predict_GB_tf = model_GB_tf.predict(X_test)\na=(model_GB_tf.score(X_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-05-01T12:51:52.078189Z","iopub.execute_input":"2023-05-01T12:51:52.078731Z","iopub.status.idle":"2023-05-01T12:53:52.016208Z","shell.execute_reply.started":"2023-05-01T12:51:52.078685Z","shell.execute_reply":"2023-05-01T12:53:52.014603Z"},"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(X_temp, y_temp)\ny_predict_RF_tf = model_RF_tf.predict(X_test)\na=(model_RF_tf.score(X_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-05-01T12:55:15.310937Z","iopub.execute_input":"2023-05-01T12:55:15.311490Z","iopub.status.idle":"2023-05-01T12:55:20.161354Z","shell.execute_reply.started":"2023-05-01T12:55:15.311442Z","shell.execute_reply":"2023-05-01T12:55:20.160185Z"},"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(X_temp, y_temp)\ny_predict_KN_tf = model_KN_tf.predict(X_test)\na=(model_KN_tf.score(X_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-05-01T12:56:02.922274Z","iopub.execute_input":"2023-05-01T12:56:02.923770Z","iopub.status.idle":"2023-05-01T12:59:41.066080Z","shell.execute_reply.started":"2023-05-01T12:56:02.923702Z","shell.execute_reply":"2023-05-01T12:59:41.064442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation","metadata":{}},{"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-05-01T13:02:41.324465Z","iopub.execute_input":"2023-05-01T13:02:41.324992Z","iopub.status.idle":"2023-05-01T13:02:41.338832Z","shell.execute_reply.started":"2023-05-01T13:02:41.324949Z","shell.execute_reply":"2023-05-01T13:02:41.337790Z"},"trusted":true},"execution_count":null,"outputs":[]}]}