{"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-04-16T04:58:45.183592Z","iopub.execute_input":"2023-04-16T04:58:45.184209Z","iopub.status.idle":"2023-04-16T04:58:45.196054Z","shell.execute_reply.started":"2023-04-16T04:58:45.184152Z","shell.execute_reply":"2023-04-16T04:58:45.19435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/goodreads-books-reviews-290312/goodreads_train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:58:45.198802Z","iopub.execute_input":"2023-04-16T04:58:45.199443Z","iopub.status.idle":"2023-04-16T04:58:58.405497Z","shell.execute_reply.started":"2023-04-16T04:58:45.199389Z","shell.execute_reply":"2023-04-16T04:58:58.403494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:58:58.408218Z","iopub.execute_input":"2023-04-16T04:58:58.409593Z","iopub.status.idle":"2023-04-16T04:58:58.429094Z","shell.execute_reply.started":"2023-04-16T04:58:58.409532Z","shell.execute_reply":"2023-04-16T04:58:58.427754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract and separate the data based on their labels\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# 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)]","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:58:58.430576Z","iopub.execute_input":"2023-04-16T04:58:58.431035Z","iopub.status.idle":"2023-04-16T04:58:58.734595Z","shell.execute_reply.started":"2023-04-16T04:58:58.431Z","shell.execute_reply":"2023-04-16T04:58:58.733342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Analyzing the size of each label after reduction\ntrain0.shape, train1.shape, train2.shape, train3.shape, train4.shape, train5.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:58:58.738343Z","iopub.execute_input":"2023-04-16T04:58:58.738831Z","iopub.status.idle":"2023-04-16T04:58:58.74785Z","shell.execute_reply.started":"2023-04-16T04:58:58.738781Z","shell.execute_reply":"2023-04-16T04:58:58.746296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Combining and shufflingthe data \ntrain=pd.concat([train0,train1,train2,train3,train4,train5],axis=0)\ntrain = train.sample(frac = 1)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:58:58.749605Z","iopub.execute_input":"2023-04-16T04:58:58.750439Z","iopub.status.idle":"2023-04-16T04:58:58.779464Z","shell.execute_reply.started":"2023-04-16T04:58:58.750387Z","shell.execute_reply":"2023-04-16T04:58:58.778353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:58:58.781007Z","iopub.execute_input":"2023-04-16T04:58:58.782139Z","iopub.status.idle":"2023-04-16T04:58:58.799733Z","shell.execute_reply.started":"2023-04-16T04:58:58.782095Z","shell.execute_reply":"2023-04-16T04:58:58.79826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(df.columns[[0,1,2,5,6,7,8,9,10]],axis=1)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:58:58.801179Z","iopub.execute_input":"2023-04-16T04:58:58.802095Z","iopub.status.idle":"2023-04-16T04:58:58.815347Z","shell.execute_reply.started":"2023-04-16T04:58:58.802031Z","shell.execute_reply":"2023-04-16T04:58:58.813987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Remove Hashtags\ntrain['review_text'].replace( { r\"#(\\w+)\" : '' }, inplace= True, regex = True)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:58:58.816568Z","iopub.execute_input":"2023-04-16T04:58:58.816934Z","iopub.status.idle":"2023-04-16T04:58:58.851098Z","shell.execute_reply.started":"2023-04-16T04:58:58.8169Z","shell.execute_reply":"2023-04-16T04:58:58.849727Z"},"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-04-16T04:58:58.853147Z","iopub.execute_input":"2023-04-16T04:58:58.853522Z","iopub.status.idle":"2023-04-16T04:58:58.892334Z","shell.execute_reply.started":"2023-04-16T04:58:58.853487Z","shell.execute_reply":"2023-04-16T04:58:58.890923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#coverting text to lower case\ntrain['review_text']=train['review_text'].str.lower()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T04:58:58.897839Z","iopub.execute_input":"2023-04-16T04:58:58.898323Z","iopub.status.idle":"2023-04-16T04:58:58.923249Z","shell.execute_reply.started":"2023-04-16T04:58:58.898281Z","shell.execute_reply":"2023-04-16T04:58:58.92145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import stopwords with nltk.\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-04-16T05:01:35.576213Z","iopub.execute_input":"2023-04-16T05:01:35.576718Z","iopub.status.idle":"2023-04-16T05:01:38.552089Z","shell.execute_reply.started":"2023-04-16T05:01:35.57668Z","shell.execute_reply":"2023-04-16T05:01:38.550435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.feature_extraction.text import TfidfVectorizer\ny = train['rating']\nx = train['review_text']\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()\ndenselist1 = X1.todense().tolist()\ntrain = pd.DataFrame(denselist1, columns=feature_names1)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:09:18.637762Z","iopub.execute_input":"2023-04-16T05:09:18.638362Z","iopub.status.idle":"2023-04-16T05:09:23.414251Z","shell.execute_reply.started":"2023-04-16T05:09:18.638303Z","shell.execute_reply":"2023-04-16T05:09:23.412805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn import tree\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.metrics import accuracy_score\nfrom sklearn import preprocessing\nimport warnings\nwarnings.filterwarnings('ignore')\nfrom sklearn.tree import DecisionTreeClassifier","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:17:13.626139Z","iopub.execute_input":"2023-04-16T05:17:13.627512Z","iopub.status.idle":"2023-04-16T05:17:13.637377Z","shell.execute_reply.started":"2023-04-16T05:17:13.627433Z","shell.execute_reply":"2023-04-16T05:17:13.635458Z"},"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)\n\n# For creating a table of the accuracies in the end\naccuracy = {'TF-IDF':[]}\n\n# Logistic Regression\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\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# 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','RF','KN']\ndata = {'model':model,'accuracy':accuracy['TF-IDF']}\ncompare_models = pd.DataFrame(data)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:17:18.790154Z","iopub.execute_input":"2023-04-16T05:17:18.790605Z","iopub.status.idle":"2023-04-16T05:19:47.013595Z","shell.execute_reply.started":"2023-04-16T05:17:18.790567Z","shell.execute_reply":"2023-04-16T05:19:47.012351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"compare_models","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:21:23.30194Z","iopub.execute_input":"2023-04-16T05:21:23.302549Z","iopub.status.idle":"2023-04-16T05:21:23.320537Z","shell.execute_reply.started":"2023-04-16T05:21:23.302494Z","shell.execute_reply":"2023-04-16T05:21:23.318916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = ['LR','DT','RF','KN']\nacc=[0.4727,0.36166,0.46,0.2588]\nplt.figure(figsize=(10,8))\ngraph = plt.bar(model,acc)\nplt.xlabel('Accuracy')\nplt.ylabel('Models')    \ngraph[0].set_color('green')\ngraph[1].set_color('yellow')\ngraph[2].set_color('red')\ngraph[3].set_color('blue')","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:30:05.495497Z","iopub.execute_input":"2023-04-16T05:30:05.495987Z","iopub.status.idle":"2023-04-16T05:30:05.735871Z","shell.execute_reply.started":"2023-04-16T05:30:05.495945Z","shell.execute_reply":"2023-04-16T05:30:05.734311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}