{"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-16T05:30:44.949401Z","iopub.execute_input":"2023-04-16T05:30:44.949975Z","iopub.status.idle":"2023-04-16T05:30:44.96081Z","shell.execute_reply.started":"2023-04-16T05:30:44.949934Z","shell.execute_reply":"2023-04-16T05:30:44.959321Z"},"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-04-16T05:30:44.963561Z","iopub.execute_input":"2023-04-16T05:30:44.964088Z","iopub.status.idle":"2023-04-16T05:30:44.98015Z","shell.execute_reply.started":"2023-04-16T05:30:44.964019Z","shell.execute_reply":"2023-04-16T05:30:44.978906Z"},"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-04-16T05:30:44.98212Z","iopub.execute_input":"2023-04-16T05:30:44.982648Z","iopub.status.idle":"2023-04-16T05:30:59.389851Z","shell.execute_reply.started":"2023-04-16T05:30:44.982593Z","shell.execute_reply":"2023-04-16T05:30:59.38725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:30:59.391027Z","iopub.status.idle":"2023-04-16T05:30:59.391515Z","shell.execute_reply.started":"2023-04-16T05:30:59.391294Z","shell.execute_reply":"2023-04-16T05:30:59.391316Z"},"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-04-16T05:30:59.394039Z","iopub.status.idle":"2023-04-16T05:30:59.394547Z","shell.execute_reply.started":"2023-04-16T05:30:59.394301Z","shell.execute_reply":"2023-04-16T05:30:59.394324Z"},"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-04-16T05:30:59.39658Z","iopub.status.idle":"2023-04-16T05:30:59.397067Z","shell.execute_reply.started":"2023-04-16T05:30:59.396818Z","shell.execute_reply":"2023-04-16T05:30:59.39684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.columns\n","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:30:59.398809Z","iopub.status.idle":"2023-04-16T05:30:59.399288Z","shell.execute_reply.started":"2023-04-16T05:30:59.399068Z","shell.execute_reply":"2023-04-16T05:30:59.399094Z"},"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-04-16T05:30:59.400962Z","iopub.status.idle":"2023-04-16T05:30:59.401425Z","shell.execute_reply.started":"2023-04-16T05:30:59.401218Z","shell.execute_reply":"2023-04-16T05:30:59.40124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:30:59.402452Z","iopub.status.idle":"2023-04-16T05:30:59.402886Z","shell.execute_reply.started":"2023-04-16T05:30:59.40267Z","shell.execute_reply":"2023-04-16T05:30:59.40269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntest['reviewgooreads'].replace( { r\"#(\\w+)\" : '' }, inplace= True, regex = True)\n\n\ntest['reviewgoodreads'].replace( { r\"@(\\w+)\" : '' }, inplace= True, regex = True)\n\ntest['reviewgoodreads'].astype(str).replace( { r\"http\\S+\" : '' }, inplace= True, regex = True)\n\ntest['reviewgoodreads'] = test['goodreads'].str.lower()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:30:59.404938Z","iopub.status.idle":"2023-04-16T05:30:59.405352Z","shell.execute_reply.started":"2023-04-16T05:30:59.405158Z","shell.execute_reply":"2023-04-16T05:30:59.405177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom nltk.corpus import stopwords\nstop = stopwords.words('english')","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:30:59.407023Z","iopub.status.idle":"2023-04-16T05:30:59.408102Z","shell.execute_reply.started":"2023-04-16T05:30:59.407813Z","shell.execute_reply":"2023-04-16T05:30:59.407852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:30:59.409425Z","iopub.status.idle":"2023-04-16T05:30:59.410432Z","shell.execute_reply.started":"2023-04-16T05:30:59.410196Z","shell.execute_reply":"2023-04-16T05:30:59.410218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer1 = TfidfVectorizer(max_features=10)\nX1 = vectorizer1.fit_transform(ds['reviewgooreads'])\nfeature_names1 = vectorizer1.get_feature_names()\ndenselist1 = X.tolist()\ndf2_train = pd.DataFrame(denselist, columns=feature_names1)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:30:59.411668Z","iopub.status.idle":"2023-04-16T05:30:59.412442Z","shell.execute_reply.started":"2023-04-16T05:30:59.4122Z","shell.execute_reply":"2023-04-16T05:30:59.412224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_df = pd.concat([x.one, 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-04-16T05:30:59.4151Z","iopub.status.idle":"2023-04-16T05:30:59.415558Z","shell.execute_reply.started":"2023-04-16T05:30:59.415328Z","shell.execute_reply":"2023-04-16T05:30:59.415348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = {'TF-IDF':[],'BOW':[]}","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:30:59.417403Z","iopub.status.idle":"2023-04-16T05:30:59.41781Z","shell.execute_reply.started":"2023-04-16T05:30:59.417615Z","shell.execute_reply":"2023-04-16T05:30:59.417634Z"},"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-04-16T05:30:59.419333Z","iopub.status.idle":"2023-04-16T05:30:59.419815Z","shell.execute_reply.started":"2023-04-16T05:30:59.419603Z","shell.execute_reply":"2023-04-16T05:30:59.419625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\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-04-16T05:30:59.421402Z","iopub.status.idle":"2023-04-16T05:30:59.422234Z","shell.execute_reply.started":"2023-04-16T05:30:59.421966Z","shell.execute_reply":"2023-04-16T05:30:59.421989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\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-04-16T05:30:59.423596Z","iopub.status.idle":"2023-04-16T05:30:59.424724Z","shell.execute_reply.started":"2023-04-16T05:30:59.424467Z","shell.execute_reply":"2023-04-16T05:30:59.424499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\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-04-16T05:30:59.427532Z","iopub.status.idle":"2023-04-16T05:30:59.428875Z","shell.execute_reply.started":"2023-04-16T05:30:59.428622Z","shell.execute_reply":"2023-04-16T05:30:59.428648Z"},"trusted":true},"execution_count":null,"outputs":[]}]}