{"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-27T11:45:10.120743Z","iopub.execute_input":"2023-05-27T11:45:10.121138Z","iopub.status.idle":"2023-05-27T11:45:10.129617Z","shell.execute_reply.started":"2023-05-27T11:45:10.121108Z","shell.execute_reply":"2023-05-27T11:45:10.128852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading the data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_train.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:45:10.132385Z","iopub.execute_input":"2023-05-27T11:45:10.133528Z","iopub.status.idle":"2023-05-27T11:45:21.55637Z","shell.execute_reply.started":"2023-05-27T11:45:10.133484Z","shell.execute_reply":"2023-05-27T11:45:21.555087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## classification based on rating","metadata":{}},{"cell_type":"code","source":"# Extract and separate the data based on their labels\n\ntrain0=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]\n\n# Peeking at the distribution between each label\ntrain0.shape, train1.shape, train2.shape, train3.shape, train4.shape, train5.shape\n\n# Reducing size of each label by 1/100\ntrain0=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)]\n\n# Analyzing the size of each label after reduction\ntrain0.shape, train1.shape, train2.shape, train3.shape, train4.shape, train5.shape\n\n# Combining the data \ntrain=pd.concat([train0,train1,train2,train3,train4,train5],axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:45:21.558884Z","iopub.execute_input":"2023-05-27T11:45:21.559293Z","iopub.status.idle":"2023-05-27T11:45:21.813745Z","shell.execute_reply.started":"2023-05-27T11:45:21.559263Z","shell.execute_reply":"2023-05-27T11:45:21.812749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data reduction and Cleaning","metadata":{}},{"cell_type":"code","source":"#Remove Hashtags\ntrain['review_text'].replace( { r\"#(\\w+)\" : '' }, inplace= True, regex = True)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:45:21.815137Z","iopub.execute_input":"2023-05-27T11:45:21.815434Z","iopub.status.idle":"2023-05-27T11:45:21.840687Z","shell.execute_reply.started":"2023-05-27T11:45:21.815407Z","shell.execute_reply":"2023-05-27T11:45:21.840035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Remove Mention\ntrain['review_text'].replace( { r\"@(\\w+)\" : '' }, inplace= True, regex = True)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:45:21.842857Z","iopub.execute_input":"2023-05-27T11:45:21.844339Z","iopub.status.idle":"2023-05-27T11:45:21.868164Z","shell.execute_reply.started":"2023-05-27T11:45:21.844308Z","shell.execute_reply":"2023-05-27T11:45:21.867161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Remove URL\ntrain['review_text'].astype(str).replace( { r\"http\\S+\" : '' }, inplace= True, regex = True)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:45:21.869821Z","iopub.execute_input":"2023-05-27T11:45:21.871268Z","iopub.status.idle":"2023-05-27T11:45:21.897139Z","shell.execute_reply.started":"2023-05-27T11:45:21.871228Z","shell.execute_reply":"2023-05-27T11:45:21.895843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['review_text']=train['review_text'].str.lower()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:45:21.898238Z","iopub.execute_input":"2023-05-27T11:45:21.898609Z","iopub.status.idle":"2023-05-27T11:45:21.918461Z","shell.execute_reply.started":"2023-05-27T11:45:21.898586Z","shell.execute_reply":"2023-05-27T11:45:21.917618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n","metadata":{"execution":{"iopub.status.busy":"2023-05-20T01:24:01.213383Z","iopub.execute_input":"2023-05-20T01:24:01.213714Z","iopub.status.idle":"2023-05-20T01:24:01.247902Z","shell.execute_reply.started":"2023-05-20T01:24:01.213687Z","shell.execute_reply":"2023-05-20T01:24:01.246768Z"}}},{"cell_type":"code","source":"import nltk\n\n\nfrom nltk.corpus import stopwords\nstop = stopwords.words('english')\n\ntrain['review_text'] = train['review_text'].apply(lambda x: ' '.join([word for word in x.split() if word not in (stop)]))","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:45:21.919919Z","iopub.execute_input":"2023-05-27T11:45:21.92027Z","iopub.status.idle":"2023-05-27T11:45:24.599745Z","shell.execute_reply.started":"2023-05-27T11:45:21.920248Z","shell.execute_reply":"2023-05-27T11:45:24.598417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:45:24.600964Z","iopub.execute_input":"2023-05-27T11:45:24.601282Z","iopub.status.idle":"2023-05-27T11:45:24.617398Z","shell.execute_reply.started":"2023-05-27T11:45:24.601259Z","shell.execute_reply":"2023-05-27T11:45:24.616322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train['rating']\nx = train['review_text']\nfrom sklearn.feature_extraction.text import TfidfVectorizer\n\n# Creating a word corpus for vectorization\ncorpus = []\nfor i in range(x.shape[0]):\n    corpus.append(x.iloc[i])\n    \nvectorizer1 = TfidfVectorizer(max_features=1000)\nX1 = vectorizer1.fit_transform(x)\nfeature_names1 = vectorizer1.get_feature_names_out()\ndenselist1 = X1.todense().tolist()\ntrain = pd.DataFrame(denselist1, columns=feature_names1)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:45:24.618829Z","iopub.execute_input":"2023-05-27T11:45:24.61931Z","iopub.status.idle":"2023-05-27T11:45:26.654921Z","shell.execute_reply.started":"2023-05-27T11:45:24.61928Z","shell.execute_reply":"2023-05-27T11:45:26.654032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:45:26.657616Z","iopub.execute_input":"2023-05-27T11:45:26.658179Z","iopub.status.idle":"2023-05-27T11:45:26.691762Z","shell.execute_reply.started":"2023-05-27T11:45:26.658148Z","shell.execute_reply":"2023-05-27T11:45:26.690905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import GradientBoostingClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\n\n\n# 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)\n\n# For creating a table of the accuracies in the end\naccuracy = {'TF-IDF':[]}\n\n# Logistic Regression\n\n# Decision Tree\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\n# Gradient Boosting\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)\n\n# Random Forest\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\n# K-Neighbors Classifier\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)\n\n# Evaluation\nmodel = ['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-27T12:14:24.125346Z","iopub.execute_input":"2023-05-27T12:14:24.125758Z"},"trusted":true},"execution_count":null,"outputs":[]}]}