{"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":"import pandas as pd\nimport numpy as np\ndf = pd.read_csv('../input/goodreads-books-reviews-290312/goodreads_train.csv')","metadata":{"papermill":{"duration":0.159299,"end_time":"2022-05-10T20:07:02.408642","exception":false,"start_time":"2022-05-10T20:07:02.249343","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-12T17:52:02.011154Z","iopub.execute_input":"2022-05-12T17:52:02.011661Z","iopub.status.idle":"2022-05-12T17:52:18.000197Z","shell.execute_reply.started":"2022-05-12T17:52:02.011509Z","shell.execute_reply":"2022-05-12T17:52:17.999042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"papermill":{"duration":0.025251,"end_time":"2022-05-10T20:07:02.445649","exception":false,"start_time":"2022-05-10T20:07:02.420398","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-12T17:52:18.001710Z","iopub.execute_input":"2022-05-12T17:52:18.001990Z","iopub.status.idle":"2022-05-12T17:52:18.012197Z","shell.execute_reply.started":"2022-05-12T17:52:18.001959Z","shell.execute_reply":"2022-05-12T17:52:18.010970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split, StratifiedShuffleSplit\nfrom sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer\nfrom sklearn.naive_bayes import MultinomialNB\nfrom sklearn.metrics import confusion_matrix, accuracy_score\nfrom sklearn.pipeline import Pipeline\nimport matplotlib.pyplot as plt\nX = df['review_text'].astype('U')\ny = df['rating'].astype('U')\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.2, stratify = y)\n","metadata":{"papermill":{"duration":1.264484,"end_time":"2022-05-10T20:07:03.722054","exception":false,"start_time":"2022-05-10T20:07:02.45757","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-12T17:52:18.014280Z","iopub.execute_input":"2022-05-12T17:52:18.014737Z","iopub.status.idle":"2022-05-12T17:52:23.184094Z","shell.execute_reply.started":"2022-05-12T17:52:18.014687Z","shell.execute_reply":"2022-05-12T17:52:23.182521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipe = Pipeline([('tfidf_vectorizer',TfidfVectorizer(lowercase = True,\n                                                     stop_words = 'english',\n                                                    analyzer = 'word')),\n                 \n                 ('naive_bayes', MultinomialNB())])\n\nle = LabelEncoder()\ny_train = le.fit_transform(y_train)\ny_val = le.transform(y_val)\n\npipe.fit(list(X_train), list(y_train))\ny_pred = pipe.predict(X_val)\nprint(confusion_matrix(y_pred, y_val))\nprint(accuracy_score(y_pred, y_val))\nplt.imshow(confusion_matrix(y_pred, y_val))","metadata":{"papermill":{"duration":0.666049,"end_time":"2022-05-10T20:07:04.400177","exception":false,"start_time":"2022-05-10T20:07:03.734128","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-12T17:52:23.186782Z","iopub.execute_input":"2022-05-12T17:52:23.187107Z","iopub.status.idle":"2022-05-12T17:55:28.206459Z","shell.execute_reply.started":"2022-05-12T17:52:23.187070Z","shell.execute_reply":"2022-05-12T17:55:28.205346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le.classes_","metadata":{"papermill":{"duration":0.015092,"end_time":"2022-05-10T20:07:04.58096","exception":false,"start_time":"2022-05-10T20:07:04.565868","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-05-12T17:55:28.208491Z","iopub.execute_input":"2022-05-12T17:55:28.209483Z","iopub.status.idle":"2022-05-12T17:55:28.218600Z","shell.execute_reply.started":"2022-05-12T17:55:28.209421Z","shell.execute_reply":"2022-05-12T17:55:28.217477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['rating'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-05-12T17:55:28.220440Z","iopub.execute_input":"2022-05-12T17:55:28.221302Z","iopub.status.idle":"2022-05-12T17:55:28.247071Z","shell.execute_reply.started":"2022-05-12T17:55:28.221244Z","shell.execute_reply":"2022-05-12T17:55:28.245998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv('../input/goodreads-books-reviews-290312/goodreads_test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-12T17:55:28.249058Z","iopub.execute_input":"2022-05-12T17:55:28.250051Z","iopub.status.idle":"2022-05-12T17:55:38.938019Z","shell.execute_reply.started":"2022-05-12T17:55:28.249989Z","shell.execute_reply":"2022-05-12T17:55:38.936760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = df_test['review_text']\ny_pred  = pipe.predict(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-12T17:55:38.940184Z","iopub.execute_input":"2022-05-12T17:55:38.941166Z","iopub.status.idle":"2022-05-12T17:57:14.285966Z","shell.execute_reply.started":"2022-05-12T17:55:38.941103Z","shell.execute_reply":"2022-05-12T17:57:14.284813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['rating'] = y_pred.astype(float)","metadata":{"execution":{"iopub.status.busy":"2022-05-12T17:57:14.287896Z","iopub.execute_input":"2022-05-12T17:57:14.288225Z","iopub.status.idle":"2022-05-12T17:57:14.298995Z","shell.execute_reply.started":"2022-05-12T17:57:14.288188Z","shell.execute_reply":"2022-05-12T17:57:14.297445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.to_csv('submission.csv',columns = ['review_id', 'rating'],\n               header = ['review_id', 'rating'], index = False)","metadata":{"execution":{"iopub.status.busy":"2022-05-12T17:57:14.300610Z","iopub.execute_input":"2022-05-12T17:57:14.301451Z","iopub.status.idle":"2022-05-12T17:57:15.953452Z","shell.execute_reply.started":"2022-05-12T17:57:14.301399Z","shell.execute_reply":"2022-05-12T17:57:15.952408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sample = df.sample(frac = 0.3)\ndf_sample['rating'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-05-12T17:57:15.955266Z","iopub.execute_input":"2022-05-12T17:57:15.958886Z","iopub.status.idle":"2022-05-12T17:57:16.580132Z","shell.execute_reply.started":"2022-05-12T17:57:15.958818Z","shell.execute_reply":"2022-05-12T17:57:16.579225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}