{"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":"2022-12-28T07:25:30.739882Z","iopub.execute_input":"2022-12-28T07:25:30.740753Z","iopub.status.idle":"2022-12-28T07:25:30.783877Z","shell.execute_reply.started":"2022-12-28T07:25:30.740662Z","shell.execute_reply":"2022-12-28T07:25:30.782867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport nltk\nnltk.download('stopwords')\nfrom nltk.corpus import stopwords\nfrom nltk.tokenize import word_tokenize\nfrom nltk.stem import WordNetLemmatizer\nimport re, string, unicodedata\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-12-28T07:25:34.053702Z","iopub.execute_input":"2022-12-28T07:25:34.054084Z","iopub.status.idle":"2022-12-28T07:25:35.399916Z","shell.execute_reply.started":"2022-12-28T07:25:34.054042Z","shell.execute_reply":"2022-12-28T07:25:35.398926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataset","metadata":{}},{"cell_type":"code","source":"train_book=pd.read_csv('/kaggle/input/goodreads-books-reviews-290312/goodreads_train.csv')\nprint(train_book.shape)\ntrain_book.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-21T12:40:24.386141Z","iopub.execute_input":"2022-12-21T12:40:24.386486Z","iopub.status.idle":"2022-12-21T12:40:54.153078Z","shell.execute_reply.started":"2022-12-21T12:40:24.386450Z","shell.execute_reply":"2022-12-21T12:40:54.151972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_book=pd.read_csv('/kaggle/input/goodreads-books-reviews-290312/goodreads_test.csv')\nprint(test_book.shape)\ntest_book.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-21T12:40:54.155457Z","iopub.execute_input":"2022-12-21T12:40:54.156118Z","iopub.status.idle":"2022-12-21T12:41:11.564171Z","shell.execute_reply.started":"2022-12-21T12:40:54.156079Z","shell.execute_reply":"2022-12-21T12:41:11.563176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(train_book[\"rating\"])\nplt.title(\"Distribution of ratings - Class Imbalanced\")","metadata":{"execution":{"iopub.status.busy":"2022-12-21T12:41:11.565865Z","iopub.execute_input":"2022-12-21T12:41:11.566563Z","iopub.status.idle":"2022-12-21T12:41:11.902494Z","shell.execute_reply.started":"2022-12-21T12:41:11.566523Z","shell.execute_reply":"2022-12-21T12:41:11.901564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# From previous intitution we can say that, these columns are not really necessary\nto_drop = [\"user_id\", \"book_id\", \"review_id\", \"read_at\", \"started_at\", \"date_added\", \"date_updated\", \"n_votes\", \"n_comments\"]\ntrain = train_book.drop(to_drop, axis= 1)\ntest = test_book.drop(to_drop, axis= 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:51:49.255891Z","iopub.execute_input":"2022-12-16T17:51:49.256261Z","iopub.status.idle":"2022-12-16T17:51:49.298604Z","shell.execute_reply.started":"2022-12-16T17:51:49.256231Z","shell.execute_reply":"2022-12-16T17:51:49.297605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:51:49.300161Z","iopub.execute_input":"2022-12-16T17:51:49.300649Z","iopub.status.idle":"2022-12-16T17:51:49.318285Z","shell.execute_reply.started":"2022-12-16T17:51:49.30061Z","shell.execute_reply":"2022-12-16T17:51:49.316657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:51:49.32066Z","iopub.execute_input":"2022-12-16T17:51:49.321481Z","iopub.status.idle":"2022-12-16T17:51:49.334297Z","shell.execute_reply.started":"2022-12-16T17:51:49.321441Z","shell.execute_reply":"2022-12-16T17:51:49.332921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train[\"review_text\"]\ny_train = train[\"rating\"]\nX_test = test[\"review_text\"]","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:51:49.336064Z","iopub.execute_input":"2022-12-16T17:51:49.336645Z","iopub.status.idle":"2022-12-16T17:51:49.342617Z","shell.execute_reply.started":"2022-12-16T17:51:49.336604Z","shell.execute_reply":"2022-12-16T17:51:49.341625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Class Imbalance","metadata":{}},{"cell_type":"code","source":"# This library will be used for handing class imbalanced data\nimport imblearn\nfrom imblearn.over_sampling import RandomOverSampler, SMOTE\nfrom imblearn.under_sampling import RandomUnderSampler\nfrom collections import Counter\nimblearn.__version__\n","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:51:49.347514Z","iopub.execute_input":"2022-12-16T17:51:49.347813Z","iopub.status.idle":"2022-12-16T17:51:49.762729Z","shell.execute_reply.started":"2022-12-16T17:51:49.347788Z","shell.execute_reply":"2022-12-16T17:51:49.761687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Counter(y_train))","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:51:49.764251Z","iopub.execute_input":"2022-12-16T17:51:49.764846Z","iopub.status.idle":"2022-12-16T17:51:49.875353Z","shell.execute_reply.started":"2022-12-16T17:51:49.764803Z","shell.execute_reply":"2022-12-16T17:51:49.874174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We solve calss imbalance for only Train split\nundersample = RandomUnderSampler(random_state = 69)\n# oversample = SMOTE() # SMOTE only works for numeric data, not str\nX_under, y_under = undersample.fit_resample(pd.DataFrame(X_train), y_train)\nprint(Counter(y_under))\nsns.countplot(y_under)\nplt.title(\"Distribution of ratings - Class Balanced\")","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:51:49.877223Z","iopub.execute_input":"2022-12-16T17:51:49.877698Z","iopub.status.idle":"2022-12-16T17:51:50.426823Z","shell.execute_reply.started":"2022-12-16T17:51:49.877654Z","shell.execute_reply":"2022-12-16T17:51:50.425694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_under = X_under[\"review_text\"]\nprint(X_under.shape)\nX_under","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:51:50.428347Z","iopub.execute_input":"2022-12-16T17:51:50.429289Z","iopub.status.idle":"2022-12-16T17:51:50.440257Z","shell.execute_reply.started":"2022-12-16T17:51:50.429248Z","shell.execute_reply":"2022-12-16T17:51:50.439004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Text Pre-processing","metadata":{}},{"cell_type":"code","source":"# Library for text processing\nimport re\nimport string\nimport nltk\nimport nltk.corpus\nnltk.download('stopwords')\nfrom nltk.corpus import stopwords\nstop_words = stopwords.words('english')","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:51:50.441943Z","iopub.execute_input":"2022-12-16T17:51:50.442752Z","iopub.status.idle":"2022-12-16T17:51:50.456067Z","shell.execute_reply.started":"2022-12-16T17:51:50.442702Z","shell.execute_reply":"2022-12-16T17:51:50.455028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stop_words.extend(['book', 'books', 'series', 'star', 'stars', 'story', 'characters', 'character', 'read'])\ndef process_txt(txt):\n    txt = txt.lower()\n    # Remove links\n    txt = re.sub(r'http\\S+' , \"\", txt)\n    txt = re.sub(r'www\\S+' , \"\", txt)\n    # Remove special squence\n    txt = re.sub(\"spoiler alert\", \"\", txt)\n    txt = re.sub(r'(\\(view spoiler\\).*?\\(hide spoiler\\))' , \"\", txt)\n    # Remove quotes\n    txt = re.sub(r'\"([^\"\\\\]|\\\\.)*\"' , \"\", txt)\n    # Remove punctuations & numbers\n    txt = re.sub(r'[^a-zA-Z ]' , \" \", txt)\n    # Remove stopwords\n    txt = ' '.join([word for word in txt.split() if word not in stop_words])\n    return txt","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:51:50.457583Z","iopub.execute_input":"2022-12-16T17:51:50.458419Z","iopub.status.idle":"2022-12-16T17:51:50.465917Z","shell.execute_reply.started":"2022-12-16T17:51:50.458381Z","shell.execute_reply":"2022-12-16T17:51:50.465209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nprocessed_txt = X_under.apply(process_txt)\nprocessed_txt","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:51:50.467503Z","iopub.execute_input":"2022-12-16T17:51:50.468151Z","iopub.status.idle":"2022-12-16T17:52:53.350309Z","shell.execute_reply.started":"2022-12-16T17:51:50.468116Z","shell.execute_reply":"2022-12-16T17:52:53.349198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_under = processed_txt","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:52:53.351986Z","iopub.execute_input":"2022-12-16T17:52:53.352371Z","iopub.status.idle":"2022-12-16T17:52:53.356792Z","shell.execute_reply.started":"2022-12-16T17:52:53.352335Z","shell.execute_reply":"2022-12-16T17:52:53.35587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nprocessed_txt = X_test.apply(process_txt)\nX_test = processed_txt\nX_test","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:52:53.358435Z","iopub.execute_input":"2022-12-16T17:52:53.359091Z","iopub.status.idle":"2022-12-16T17:56:00.554899Z","shell.execute_reply.started":"2022-12-16T17:52:53.359035Z","shell.execute_reply":"2022-12-16T17:56:00.553852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Lemmatize","metadata":{}},{"cell_type":"code","source":"nltk.download('omw-1.4')\nfrom nltk.stem import WordNetLemmatizer\nwordnet_lemmatizer = WordNetLemmatizer()","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:56:00.556363Z","iopub.execute_input":"2022-12-16T17:56:00.557307Z","iopub.status.idle":"2022-12-16T17:56:00.896067Z","shell.execute_reply.started":"2022-12-16T17:56:00.557264Z","shell.execute_reply":"2022-12-16T17:56:00.894951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nX_under = X_under.apply( lambda x : ' '.join(wordnet_lemmatizer.lemmatize(word, pos = 'a') for word in x.split()))\nX_under","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:56:00.897865Z","iopub.execute_input":"2022-12-16T17:56:00.8983Z","iopub.status.idle":"2022-12-16T17:56:48.563485Z","shell.execute_reply.started":"2022-12-16T17:56:00.898259Z","shell.execute_reply":"2022-12-16T17:56:48.562352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nX_test = X_test.apply( lambda x : ' '.join(wordnet_lemmatizer.lemmatize(word, pos = 'a') for word in x.split()))\nX_test","metadata":{"execution":{"iopub.status.busy":"2022-12-16T17:56:48.567222Z","iopub.execute_input":"2022-12-16T17:56:48.567547Z","iopub.status.idle":"2022-12-16T17:59:15.720406Z","shell.execute_reply.started":"2022-12-16T17:56:48.567514Z","shell.execute_reply":"2022-12-16T17:59:15.719319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Call Pre-processed Data","metadata":{}},{"cell_type":"code","source":"X_under = pd.read_csv(\"/kaggle/input/goodreads-processed-data-undersampled/goodreads_processed_undersampled_train.csv\")\nX_under[\"review_text\"] = X_under[\"review_text\"].astype(str)\ny_under = X_under[\"rating\"]\nX_under = X_under[\"review_text\"]","metadata":{"execution":{"iopub.status.busy":"2022-12-28T07:25:55.439083Z","iopub.execute_input":"2022-12-28T07:25:55.439681Z","iopub.status.idle":"2022-12-28T07:25:56.559909Z","shell.execute_reply.started":"2022-12-28T07:25:55.439636Z","shell.execute_reply":"2022-12-28T07:25:56.558856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_under","metadata":{"execution":{"iopub.status.busy":"2022-12-28T07:25:56.565290Z","iopub.execute_input":"2022-12-28T07:25:56.567594Z","iopub.status.idle":"2022-12-28T07:25:56.583180Z","shell.execute_reply.started":"2022-12-28T07:25:56.567555Z","shell.execute_reply":"2022-12-28T07:25:56.582131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_under = X_under.apply(lambda x: x.split())\nX_under","metadata":{"execution":{"iopub.status.busy":"2022-12-28T07:25:51.577718Z","iopub.execute_input":"2022-12-28T07:25:51.578061Z","iopub.status.idle":"2022-12-28T07:25:53.743856Z","shell.execute_reply.started":"2022-12-28T07:25:51.578027Z","shell.execute_reply":"2022-12-28T07:25:53.742810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_under","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:50:11.784996Z","iopub.execute_input":"2022-12-27T06:50:11.785696Z","iopub.status.idle":"2022-12-27T06:50:11.794932Z","shell.execute_reply.started":"2022-12-27T06:50:11.785653Z","shell.execute_reply":"2022-12-27T06:50:11.793651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = pd.read_csv(\"/kaggle/input/goodreads-processed-data-undersampled/goodreds_processed_test_only_text.csv\")\nX_test = X_test[\"review_text\"].astype(str)\nX_test","metadata":{"execution":{"iopub.status.busy":"2022-12-21T16:39:12.531087Z","iopub.execute_input":"2022-12-21T16:39:12.531704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = X_test.apply(lambda x: x.split())\nX_test","metadata":{"execution":{"iopub.status.busy":"2022-12-21T12:55:21.884255Z","iopub.execute_input":"2022-12-21T12:55:21.884921Z","iopub.status.idle":"2022-12-21T12:55:28.257673Z","shell.execute_reply.started":"2022-12-21T12:55:21.884879Z","shell.execute_reply":"2022-12-21T12:55:28.256580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(X_under, y_under, test_size=0.4, random_state=42)\n\nprint(X_train.shape)\nprint(y_train.shape)\nprint(X_val.shape)\nprint(y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T07:26:01.037193Z","iopub.execute_input":"2022-12-28T07:26:01.037620Z","iopub.status.idle":"2022-12-28T07:26:01.070257Z","shell.execute_reply.started":"2022-12-28T07:26:01.037581Z","shell.execute_reply":"2022-12-28T07:26:01.069172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(y_val)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T07:26:01.704781Z","iopub.execute_input":"2022-12-28T07:26:01.705123Z","iopub.status.idle":"2022-12-28T07:26:01.942336Z","shell.execute_reply.started":"2022-12-28T07:26:01.705092Z","shell.execute_reply":"2022-12-28T07:26:01.941410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-12-28T07:26:02.164109Z","iopub.execute_input":"2022-12-28T07:26:02.164992Z","iopub.status.idle":"2022-12-28T07:26:02.378116Z","shell.execute_reply.started":"2022-12-28T07:26:02.164958Z","shell.execute_reply":"2022-12-28T07:26:02.377229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Word 2 Vec Model","metadata":{}},{"cell_type":"code","source":"import gensim\nw2v_model = gensim.models.Word2Vec(pd.concat([X_train , X_val]), vector_size=50, window=5, min_count=1, sg=1)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:04:49.432841Z","iopub.execute_input":"2022-12-27T06:04:49.433283Z","iopub.status.idle":"2022-12-27T06:11:11.038485Z","shell.execute_reply.started":"2022-12-27T06:04:49.433242Z","shell.execute_reply":"2022-12-27T06:11:11.037525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"words = set(w2v_model.wv.index_to_key )\n\nX_train_vect = np.array([np.array([w2v_model.wv[i] for i in ls if i in words]) for ls in X_train])\nX_val_vect = np.array([np.array([w2v_model.wv[i] for i in ls if i in words]) for ls in X_val])","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:11:11.040386Z","iopub.execute_input":"2022-12-27T06:11:11.040751Z","iopub.status.idle":"2022-12-27T06:11:38.430520Z","shell.execute_reply.started":"2022-12-27T06:11:11.040714Z","shell.execute_reply":"2022-12-27T06:11:38.429335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_vect_avg = []\nfor v in X_train_vect:\n    if v.size:\n        X_train_vect_avg.append(v.mean(axis=0))\n    else:\n        X_train_vect_avg.append(np.zeros(50, dtype=float))\n        \nX_val_vect_avg = []\nfor v in X_val_vect:\n    if v.size:\n        X_val_vect_avg.append(v.mean(axis=0))\n    else:\n        X_val_vect_avg.append(np.zeros(50, dtype=float))","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:11:38.431949Z","iopub.execute_input":"2022-12-27T06:11:38.432606Z","iopub.status.idle":"2022-12-27T06:11:40.410144Z","shell.execute_reply.started":"2022-12-27T06:11:38.432567Z","shell.execute_reply":"2022-12-27T06:11:40.409192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(X_train_vect_avg))\nX_train_vect_avg[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:11:40.412585Z","iopub.execute_input":"2022-12-27T06:11:40.412972Z","iopub.status.idle":"2022-12-27T06:11:40.420799Z","shell.execute_reply.started":"2022-12-27T06:11:40.412934Z","shell.execute_reply":"2022-12-27T06:11:40.419673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(X_val_vect_avg))\nX_val_vect_avg[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:11:40.422428Z","iopub.execute_input":"2022-12-27T06:11:40.422824Z","iopub.status.idle":"2022-12-27T06:11:40.434210Z","shell.execute_reply.started":"2022-12-27T06:11:40.422788Z","shell.execute_reply":"2022-12-27T06:11:40.433123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Random Forest","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix, f1_score, accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-12-27T06:12:42.556235Z","iopub.execute_input":"2022-12-27T06:12:42.556624Z","iopub.status.idle":"2022-12-27T06:12:42.562364Z","shell.execute_reply.started":"2022-12-27T06:12:42.556589Z","shell.execute_reply":"2022-12-27T06:12:42.561254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import classification_report, confusion_matrix, f1_score, accuracy_score\n\nrf = RandomForestClassifier()\nrf_model = rf.fit(X_train_vect_avg, y_train.values.ravel())","metadata":{"execution":{"iopub.status.busy":"2022-12-21T13:32:54.467385Z","iopub.execute_input":"2022-12-21T13:32:54.467760Z","iopub.status.idle":"2022-12-21T13:35:09.065998Z","shell.execute_reply.started":"2022-12-21T13:32:54.467726Z","shell.execute_reply":"2022-12-21T13:35:09.064941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_pred = rf_model.predict(X_val_vect_avg)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T13:35:09.068109Z","iopub.execute_input":"2022-12-21T13:35:09.068486Z","iopub.status.idle":"2022-12-21T13:35:11.413921Z","shell.execute_reply.started":"2022-12-21T13:35:09.068450Z","shell.execute_reply":"2022-12-21T13:35:11.412938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val,rf_pred))","metadata":{"execution":{"iopub.status.busy":"2022-12-21T13:35:11.415206Z","iopub.execute_input":"2022-12-21T13:35:11.416055Z","iopub.status.idle":"2022-12-21T13:35:11.526639Z","shell.execute_reply.started":"2022-12-21T13:35:11.416010Z","shell.execute_reply":"2022-12-21T13:35:11.525409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_cn_mat = confusion_matrix(y_val,rf_pred)\nrf_cn_mat = pd.DataFrame(rf_cn_mat, range(6), range(6))\nrf_cn_mat","metadata":{"execution":{"iopub.status.busy":"2022-12-21T13:35:11.529132Z","iopub.execute_input":"2022-12-21T13:35:11.529688Z","iopub.status.idle":"2022-12-21T13:35:11.552422Z","shell.execute_reply.started":"2022-12-21T13:35:11.529649Z","shell.execute_reply":"2022-12-21T13:35:11.551249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10, 10))\nsns.heatmap(rf_cn_mat, annot=True, fmt='d', cmap='Blues')\nplt.title(\"Confusion Matrix for Random Forest - Validation\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-21T13:35:11.553935Z","iopub.execute_input":"2022-12-21T13:35:11.554432Z","iopub.status.idle":"2022-12-21T13:35:11.952689Z","shell.execute_reply.started":"2022-12-21T13:35:11.554397Z","shell.execute_reply":"2022-12-21T13:35:11.951553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf_pred = rf_model.predict(X_train_vect_avg)\nprint(classification_report(y_train,rf_pred))\nrf_cn_mat = confusion_matrix(y_train,rf_pred)\nrf_cn_mat = pd.DataFrame(rf_cn_mat, range(6), range(6))\nplt.figure(figsize = (10, 10))\nsns.heatmap(rf_cn_mat, annot=True, fmt='d', cmap='Blues')\nplt.title(\"Confusion Matrix for Random Forest - Train\")","metadata":{"execution":{"iopub.status.busy":"2022-12-21T13:35:31.641496Z","iopub.execute_input":"2022-12-21T13:35:31.642186Z","iopub.status.idle":"2022-12-21T13:35:35.802634Z","shell.execute_reply.started":"2022-12-21T13:35:31.642148Z","shell.execute_reply":"2022-12-21T13:35:35.801582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import optuna\n\ndef rf_performance(rf_1, x = X_val_vect_avg, y = y_val):\n    pred_RF_1 = rf_1.predict(x)\n    return round(accuracy_score(y,pred_RF_1),6)\n\ndef objective(trial):\n    rf_n_estimators = trial.suggest_int('rf_n_estimators',1,100)\n    rf_max_depth = trial.suggest_int('rf_max_depth',2,32)\n    rf_min_samples_split =  trial.suggest_int('rf_min_samples_split',2,20)\n    rf_min_samples_leaf = trial.suggest_int('rf_min_samples_leaf',2,20)\n    rf_random_state = trial.suggest_int('rf_random_state',1,1000)\n    rf_1 = RandomForestClassifier(n_estimators = rf_n_estimators, max_depth = rf_max_depth, random_state = rf_random_state,\n                            min_samples_split = rf_min_samples_split, min_samples_leaf = rf_min_samples_leaf)\n    rf_1.fit(X_train_vect_avg,y_train)\n    return rf_performance(rf_1)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T14:06:22.622170Z","iopub.execute_input":"2022-12-21T14:06:22.622526Z","iopub.status.idle":"2022-12-21T14:06:22.631186Z","shell.execute_reply.started":"2022-12-21T14:06:22.622494Z","shell.execute_reply":"2022-12-21T14:06:22.630160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study = optuna.create_study(direction = 'maximize', sampler = optuna.samplers.RandomSampler())\nstudy.optimize(objective, n_trials = 100)\ntrial = study.best_trial\nprint(trial.params)\nprint(study.best_trial)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T14:13:05.802030Z","iopub.execute_input":"2022-12-21T14:13:05.802430Z","iopub.status.idle":"2022-12-21T15:29:03.371275Z","shell.execute_reply.started":"2022-12-21T14:13:05.802397Z","shell.execute_reply":"2022-12-21T15:29:03.370060Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_rf_1 = RandomForestClassifier(n_estimators = trial.params['rf_n_estimators'], max_depth = trial.params['rf_max_depth'],\n                            min_samples_split = trial.params['rf_min_samples_split'],random_state = trial.params['rf_random_state'],\n                            min_samples_leaf = trial.params['rf_min_samples_leaf'])\nbest_rf_1.fit(X_train_vect_avg,y_train)    ","metadata":{"execution":{"iopub.status.busy":"2022-12-21T15:46:44.160295Z","iopub.execute_input":"2022-12-21T15:46:44.160689Z","iopub.status.idle":"2022-12-21T15:48:28.893354Z","shell.execute_reply.started":"2022-12-21T15:46:44.160652Z","shell.execute_reply":"2022-12-21T15:48:28.892361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_pred_rf_1 = best_rf_1.predict(X_val_vect_avg)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T15:48:28.895383Z","iopub.execute_input":"2022-12-21T15:48:28.896858Z","iopub.status.idle":"2022-12-21T15:48:30.293006Z","shell.execute_reply.started":"2022-12-21T15:48:28.896819Z","shell.execute_reply":"2022-12-21T15:48:30.291979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val, best_pred_rf_1))","metadata":{"execution":{"iopub.status.busy":"2022-12-21T15:48:30.294797Z","iopub.execute_input":"2022-12-21T15:48:30.295197Z","iopub.status.idle":"2022-12-21T15:48:30.411786Z","shell.execute_reply.started":"2022-12-21T15:48:30.295157Z","shell.execute_reply":"2022-12-21T15:48:30.410654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Logistic Regression","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression \n\nlog_model = LogisticRegression(max_iter = 1000)\nlog_model.fit(X_train_vect_avg, y_train.values.ravel())","metadata":{"execution":{"iopub.status.busy":"2022-12-21T17:13:15.786657Z","iopub.execute_input":"2022-12-21T17:13:15.787545Z","iopub.status.idle":"2022-12-21T17:13:52.672937Z","shell.execute_reply.started":"2022-12-21T17:13:15.787510Z","shell.execute_reply":"2022-12-21T17:13:52.671671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_pred = log_model.predict(X_val_vect_avg)\nlog_pred","metadata":{"execution":{"iopub.status.busy":"2022-12-21T17:13:52.679223Z","iopub.execute_input":"2022-12-21T17:13:52.680181Z","iopub.status.idle":"2022-12-21T17:13:52.817217Z","shell.execute_reply.started":"2022-12-21T17:13:52.680144Z","shell.execute_reply":"2022-12-21T17:13:52.816096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val,log_pred))","metadata":{"execution":{"iopub.status.busy":"2022-12-21T17:13:52.818609Z","iopub.execute_input":"2022-12-21T17:13:52.819556Z","iopub.status.idle":"2022-12-21T17:13:52.985961Z","shell.execute_reply.started":"2022-12-21T17:13:52.819518Z","shell.execute_reply":"2022-12-21T17:13:52.984795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_cn_mat = confusion_matrix(y_val,log_pred)\nlog_cn_mat = pd.DataFrame(log_cn_mat, range(6), range(6))\nlog_cn_mat","metadata":{"execution":{"iopub.status.busy":"2022-12-21T17:13:52.988811Z","iopub.execute_input":"2022-12-21T17:13:52.989179Z","iopub.status.idle":"2022-12-21T17:13:53.028758Z","shell.execute_reply.started":"2022-12-21T17:13:52.989140Z","shell.execute_reply":"2022-12-21T17:13:53.027705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10, 10))\nsns.heatmap(log_cn_mat, annot=True, fmt='d', cmap='Blues')\nplt.title(\"Confusion Matrix for Logistic Regression - Validation\")","metadata":{"execution":{"iopub.status.busy":"2022-12-21T17:13:53.030087Z","iopub.execute_input":"2022-12-21T17:13:53.030503Z","iopub.status.idle":"2022-12-21T17:13:53.454712Z","shell.execute_reply.started":"2022-12-21T17:13:53.030469Z","shell.execute_reply":"2022-12-21T17:13:53.453696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_pred = log_model.predict(X_train_vect_avg)\nprint(classification_report(y_train,log_pred))\nlog_cn_mat = confusion_matrix(y_train,log_pred)\nlog_cn_mat = pd.DataFrame(log_cn_mat, range(6), range(6))\nplt.figure(figsize = (10, 10))\nsns.heatmap(log_cn_mat, annot=True, fmt='d', cmap='Blues')\nplt.title(\"Confusion Matrix for Logistic Regression - Training\")","metadata":{"execution":{"iopub.status.busy":"2022-12-21T17:13:53.456232Z","iopub.execute_input":"2022-12-21T17:13:53.456604Z","iopub.status.idle":"2022-12-21T17:13:54.173250Z","shell.execute_reply.started":"2022-12-21T17:13:53.456569Z","shell.execute_reply":"2022-12-21T17:13:54.172363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Trials","metadata":{"execution":{"iopub.status.busy":"2022-12-21T17:13:54.174726Z","iopub.execute_input":"2022-12-21T17:13:54.175074Z","iopub.status.idle":"2022-12-21T17:13:54.179351Z","shell.execute_reply.started":"2022-12-21T17:13:54.175039Z","shell.execute_reply":"2022-12-21T17:13:54.178310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import optuna\n\ndef log_performance(log, x = X_val_vect_avg, y = y_val):\n    pred_log = log.predict(x)\n    return round(accuracy_score(y,pred_log),6)\n\ndef objective(trial):\n    penalty = trial.suggest_categorical('penalty',['l2','l1'])\n    if penalty == 'l1':\n        solver = 'saga'\n    else:\n        solver = 'lbfgs'\n    log_random_state = trial.suggest_int('log_random_state',1,1000)\n    log_regularization = trial.suggest_uniform('log_regularization',0.01, 10)\n    log_max_iter = 10000\n    log = LogisticRegression(penalty = penalty,C = log_regularization,solver = solver,max_iter = log_max_iter,random_state = log_random_state)\n    log.fit(X_train_vect_avg,y_train.values.ravel())\n    return log_performance(log)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T17:13:54.180682Z","iopub.execute_input":"2022-12-21T17:13:54.181135Z","iopub.status.idle":"2022-12-21T17:13:54.502153Z","shell.execute_reply.started":"2022-12-21T17:13:54.181098Z","shell.execute_reply":"2022-12-21T17:13:54.501137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study = optuna.create_study(direction = 'maximize')\nstudy.optimize(objective, n_trials = 100)\ntrial = study.best_trial\nprint(trial.params)\nprint(study.best_trial)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T17:13:54.503650Z","iopub.execute_input":"2022-12-21T17:13:54.504065Z","iopub.status.idle":"2022-12-21T18:01:33.761394Z","shell.execute_reply.started":"2022-12-21T17:13:54.504026Z","shell.execute_reply":"2022-12-21T18:01:33.758612Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_log = LogisticRegression(penalty = 'l2',C = 0.2161844476911182,solver = 'saga', max_iter = 10000, random_state = 27)\nbest_log.fit(X_train_vect_avg,y_train.values.ravel())","metadata":{"execution":{"iopub.status.busy":"2022-12-21T18:02:44.696895Z","iopub.execute_input":"2022-12-21T18:02:44.697959Z","iopub.status.idle":"2022-12-21T18:02:52.816572Z","shell.execute_reply.started":"2022-12-21T18:02:44.697923Z","shell.execute_reply":"2022-12-21T18:02:52.815568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_pred_log = best_log.predict(X_val_vect_avg)\nprint(classification_report(y_val, best_pred_log))","metadata":{"execution":{"iopub.status.busy":"2022-12-21T18:03:14.046323Z","iopub.execute_input":"2022-12-21T18:03:14.046670Z","iopub.status.idle":"2022-12-21T18:03:14.283078Z","shell.execute_reply.started":"2022-12-21T18:03:14.046640Z","shell.execute_reply":"2022-12-21T18:03:14.282046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## SVM","metadata":{}},{"cell_type":"code","source":"from sklearn import svm\nfrom sklearn.metrics import classification_report, confusion_matrix, f1_score, accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-12-22T06:45:45.420416Z","iopub.execute_input":"2022-12-22T06:45:45.420830Z","iopub.status.idle":"2022-12-22T06:45:45.425988Z","shell.execute_reply.started":"2022-12-22T06:45:45.420796Z","shell.execute_reply":"2022-12-22T06:45:45.425064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rbf = svm.SVC(kernel='rbf', gamma=0.5, C=0.1).fit(X_train_vect_avg, y_train)\nrbf_pred = rbf.predict(X_val_vect_avg)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T06:45:48.942853Z","iopub.execute_input":"2022-12-22T06:45:48.943241Z","iopub.status.idle":"2022-12-22T07:15:02.029585Z","shell.execute_reply.started":"2022-12-22T06:45:48.943203Z","shell.execute_reply":"2022-12-22T07:15:02.028508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val,rbf_pred))\nrbf_cn_mat = confusion_matrix(y_val,rbf_pred)\nrbf_cn_mat = pd.DataFrame(rbf_cn_mat, range(6), range(6))\nplt.figure(figsize = (10, 10))\nsns.heatmap(rbf_cn_mat, annot=True, fmt='d', cmap='Blues')\nplt.title(\"Confusion Matrix for SVM - Validation\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T07:15:02.031799Z","iopub.execute_input":"2022-12-22T07:15:02.032250Z","iopub.status.idle":"2022-12-22T07:15:02.622385Z","shell.execute_reply.started":"2022-12-22T07:15:02.032203Z","shell.execute_reply":"2022-12-22T07:15:02.621252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## RNN","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom tensorflow.keras.optimizers import Adam , Adagrad, SGD, RMSprop\nfrom keras.layers import Dense, Dropout, BatchNormalization, SimpleRNN, Activation, LSTM, Bidirectional\nfrom tensorflow.keras.models import Sequential\nfrom keras.wrappers.scikit_learn import KerasClassifier\n\nfrom sklearn.metrics import classification_report, confusion_matrix, f1_score, accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-12-22T12:28:24.302109Z","iopub.execute_input":"2022-12-22T12:28:24.302794Z","iopub.status.idle":"2022-12-22T12:28:29.683427Z","shell.execute_reply.started":"2022-12-22T12:28:24.302760Z","shell.execute_reply":"2022-12-22T12:28:29.682276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(X_train_vect_avg))\nX_train_vect_avg[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-12-22T12:28:29.688168Z","iopub.execute_input":"2022-12-22T12:28:29.691194Z","iopub.status.idle":"2022-12-22T12:28:29.707186Z","shell.execute_reply.started":"2022-12-22T12:28:29.691154Z","shell.execute_reply":"2022-12-22T12:28:29.706043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.stack(X_train_vect_avg, axis= 0)\nprint(X_train.shape)\nX_val = np.stack(X_val_vect_avg, axis= 0)\nprint(X_val.shape)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T12:28:29.708977Z","iopub.execute_input":"2022-12-22T12:28:29.709695Z","iopub.status.idle":"2022-12-22T12:28:29.921724Z","shell.execute_reply.started":"2022-12-22T12:28:29.709659Z","shell.execute_reply":"2022-12-22T12:28:29.920676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_reshaped =np.array(X_train).reshape(X_train.shape[0], 1, X_train.shape[1])\nX_val_reshaped = np.array(X_val).reshape(X_val.shape[0], 1, X_val.shape[1])\n\n\nprint(X_train_reshaped.shape)\nprint(X_val_reshaped.shape)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T12:28:29.923053Z","iopub.execute_input":"2022-12-22T12:28:29.923992Z","iopub.status.idle":"2022-12-22T12:28:29.944673Z","shell.execute_reply.started":"2022-12-22T12:28:29.923933Z","shell.execute_reply":"2022-12-22T12:28:29.943614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vanilla_rnn():\n    model = Sequential()\n    model.add(SimpleRNN(25, input_shape = (1, 50), return_sequences = False))\n    model.add(Dense(6))\n    model.add(Activation('softmax'))\n    \n    adam = Adam(lr = 0.001)\n    model.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['accuracy'])\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2022-12-22T08:38:39.788942Z","iopub.execute_input":"2022-12-22T08:38:39.789303Z","iopub.status.idle":"2022-12-22T08:38:39.795755Z","shell.execute_reply.started":"2022-12-22T08:38:39.789264Z","shell.execute_reply":"2022-12-22T08:38:39.794615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = KerasClassifier(build_fn = vanilla_rnn, epochs = 200, batch_size = 128, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T08:38:44.158098Z","iopub.execute_input":"2022-12-22T08:38:44.158761Z","iopub.status.idle":"2022-12-22T08:38:44.163551Z","shell.execute_reply.started":"2022-12-22T08:38:44.158724Z","shell.execute_reply":"2022-12-22T08:38:44.162575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.backend.clear_session()\n\ncallback = EarlyStopping(monitor = \"accuracy\", patience = 20, restore_best_weights=True) \n\nrnn_history = model.fit(X_train_reshaped, y_train, callbacks = [callback])","metadata":{"execution":{"iopub.status.busy":"2022-12-22T08:43:21.499971Z","iopub.execute_input":"2022-12-22T08:43:21.500359Z","iopub.status.idle":"2022-12-22T08:53:17.788473Z","shell.execute_reply.started":"2022-12-22T08:43:21.500319Z","shell.execute_reply":"2022-12-22T08:53:17.787554Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rnn_history.history.keys()","metadata":{"execution":{"iopub.status.busy":"2022-12-22T08:54:55.698643Z","iopub.execute_input":"2022-12-22T08:54:55.699239Z","iopub.status.idle":"2022-12-22T08:54:55.712541Z","shell.execute_reply.started":"2022-12-22T08:54:55.699193Z","shell.execute_reply":"2022-12-22T08:54:55.711412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(rnn_history.history[\"loss\"])\nplt.title(\"Loss of RNN training\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T08:55:45.516628Z","iopub.execute_input":"2022-12-22T08:55:45.516998Z","iopub.status.idle":"2022-12-22T08:55:45.821501Z","shell.execute_reply.started":"2022-12-22T08:55:45.516968Z","shell.execute_reply":"2022-12-22T08:55:45.820290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(rnn_history.history[\"accuracy\"])\nplt.title(\"Accuracy of RNN training\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T08:56:08.675106Z","iopub.execute_input":"2022-12-22T08:56:08.675707Z","iopub.status.idle":"2022-12-22T08:56:08.898078Z","shell.execute_reply.started":"2022-12-22T08:56:08.675670Z","shell.execute_reply":"2022-12-22T08:56:08.897103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_val_reshaped)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T08:56:52.647729Z","iopub.execute_input":"2022-12-22T08:56:52.648461Z","iopub.status.idle":"2022-12-22T08:56:55.359579Z","shell.execute_reply.started":"2022-12-22T08:56:52.648425Z","shell.execute_reply":"2022-12-22T08:56:55.358576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(accuracy_score(y_pred, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-12-22T08:58:27.245200Z","iopub.execute_input":"2022-12-22T08:58:27.246187Z","iopub.status.idle":"2022-12-22T08:58:27.257478Z","shell.execute_reply.started":"2022-12-22T08:58:27.246152Z","shell.execute_reply":"2022-12-22T08:58:27.256324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val,y_pred))\nrnn_cn_mat = confusion_matrix(y_val,y_pred)\nrnn_cn_mat = pd.DataFrame(rnn_cn_mat, range(6), range(6))\nplt.figure(figsize = (10, 10))\nsns.heatmap(rnn_cn_mat, annot=True, fmt='d', cmap='Blues')\nplt.title(\"Confusion Matrix for RNN - Validation\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T09:00:04.098820Z","iopub.execute_input":"2022-12-22T09:00:04.099211Z","iopub.status.idle":"2022-12-22T09:00:04.652151Z","shell.execute_reply.started":"2022-12-22T09:00:04.099180Z","shell.execute_reply":"2022-12-22T09:00:04.651213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## LSTM","metadata":{}},{"cell_type":"code","source":"def lstm():\n    model = Sequential()\n    model.add(LSTM(25, input_shape = (1, 50), return_sequences = False))\n    model.add(Dense(6))\n    model.add(Activation('softmax'))\n    \n    adam = Adam(lr = 0.001)\n    model.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['accuracy'])\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2022-12-22T09:00:35.440208Z","iopub.execute_input":"2022-12-22T09:00:35.440948Z","iopub.status.idle":"2022-12-22T09:00:35.447163Z","shell.execute_reply.started":"2022-12-22T09:00:35.440912Z","shell.execute_reply":"2022-12-22T09:00:35.446116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = KerasClassifier(build_fn = lstm, epochs = 200, batch_size = 128, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T09:00:38.092884Z","iopub.execute_input":"2022-12-22T09:00:38.093661Z","iopub.status.idle":"2022-12-22T09:00:38.098877Z","shell.execute_reply.started":"2022-12-22T09:00:38.093622Z","shell.execute_reply":"2022-12-22T09:00:38.097720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.backend.clear_session()\n\ncallback = EarlyStopping(monitor = \"accuracy\", patience = 20, restore_best_weights=True) \n\nlstm_history = model.fit(X_train_reshaped, y_train, callbacks = [callback])","metadata":{"execution":{"iopub.status.busy":"2022-12-22T09:00:44.062005Z","iopub.execute_input":"2022-12-22T09:00:44.062385Z","iopub.status.idle":"2022-12-22T09:10:11.230062Z","shell.execute_reply.started":"2022-12-22T09:00:44.062346Z","shell.execute_reply":"2022-12-22T09:10:11.229105Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(lstm_history.history[\"loss\"])\nplt.title(\"Loss of LSTM training\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T09:11:13.834794Z","iopub.execute_input":"2022-12-22T09:11:13.835446Z","iopub.status.idle":"2022-12-22T09:11:14.050046Z","shell.execute_reply.started":"2022-12-22T09:11:13.835400Z","shell.execute_reply":"2022-12-22T09:11:14.049113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(lstm_history.history[\"accuracy\"])\nplt.title(\"Accuracy of LSTM training\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T09:11:14.051419Z","iopub.execute_input":"2022-12-22T09:11:14.052426Z","iopub.status.idle":"2022-12-22T09:11:14.266737Z","shell.execute_reply.started":"2022-12-22T09:11:14.052387Z","shell.execute_reply":"2022-12-22T09:11:14.265760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_val_reshaped)\n\nprint(accuracy_score(y_pred, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-12-22T09:11:07.883775Z","iopub.execute_input":"2022-12-22T09:11:07.884451Z","iopub.status.idle":"2022-12-22T09:11:13.298875Z","shell.execute_reply.started":"2022-12-22T09:11:07.884413Z","shell.execute_reply":"2022-12-22T09:11:13.297750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val,y_pred))\nlstm_cn_mat = confusion_matrix(y_val,y_pred)\nlstm_cn_mat = pd.DataFrame(lstm_cn_mat, range(6), range(6))\nplt.figure(figsize = (10, 10))\nsns.heatmap(lstm_cn_mat, annot=True, fmt='d', cmap='Blues')\nplt.title(\"Confusion Matrix for LSTM - Validation\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T09:11:13.300594Z","iopub.execute_input":"2022-12-22T09:11:13.300947Z","iopub.status.idle":"2022-12-22T09:11:13.833142Z","shell.execute_reply.started":"2022-12-22T09:11:13.300911Z","shell.execute_reply":"2022-12-22T09:11:13.832150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Bi-directional","metadata":{}},{"cell_type":"code","source":"def bi_lstm():\n    model = Sequential()\n    model.add(Bidirectional(LSTM(25, input_shape = (1, 50), return_sequences = False)))\n    model.add(Dense(6))\n    model.add(Activation('softmax'))\n    \n    adam = Adam(lr = 0.001)\n    model.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['accuracy'])\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2022-12-22T12:29:21.991866Z","iopub.execute_input":"2022-12-22T12:29:21.992568Z","iopub.status.idle":"2022-12-22T12:29:21.998324Z","shell.execute_reply.started":"2022-12-22T12:29:21.992534Z","shell.execute_reply":"2022-12-22T12:29:21.997335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = KerasClassifier(build_fn = bi_lstm, epochs = 200, batch_size = 128, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T12:29:24.497775Z","iopub.execute_input":"2022-12-22T12:29:24.498145Z","iopub.status.idle":"2022-12-22T12:29:24.503524Z","shell.execute_reply.started":"2022-12-22T12:29:24.498112Z","shell.execute_reply":"2022-12-22T12:29:24.502546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.backend.clear_session()\n\ncallback = EarlyStopping(monitor = \"accuracy\", patience = 20, restore_best_weights=True) \n\nbi_lstm_history = model.fit(X_train_reshaped, y_train, callbacks = [callback])","metadata":{"execution":{"iopub.status.busy":"2022-12-22T12:29:30.931435Z","iopub.execute_input":"2022-12-22T12:29:30.931807Z","iopub.status.idle":"2022-12-22T12:42:14.958066Z","shell.execute_reply.started":"2022-12-22T12:29:30.931775Z","shell.execute_reply":"2022-12-22T12:42:14.957210Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(ncols = 2, figsize = (10 , 5))\n\naxes[0].plot(bi_lstm_history.history[\"loss\"])\naxes[0].set_title(\"Loss of training\")\n\naxes[1].plot(bi_lstm_history.history[\"accuracy\"])\naxes[1].set_title(\"Accuracy of training\")\n\nfig.suptitle(\"Bi-Directional LSTM\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T12:43:07.505546Z","iopub.execute_input":"2022-12-22T12:43:07.505899Z","iopub.status.idle":"2022-12-22T12:43:07.859886Z","shell.execute_reply.started":"2022-12-22T12:43:07.505869Z","shell.execute_reply":"2022-12-22T12:43:07.858895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_val_reshaped)\n\nprint(accuracy_score(y_pred, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-12-22T12:43:24.014544Z","iopub.execute_input":"2022-12-22T12:43:24.014906Z","iopub.status.idle":"2022-12-22T12:43:28.142018Z","shell.execute_reply.started":"2022-12-22T12:43:24.014874Z","shell.execute_reply":"2022-12-22T12:43:28.140901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val,y_pred))\nbi_lstm_cn_mat = confusion_matrix(y_val,y_pred)\nbi_lstm_cn_mat = pd.DataFrame(bi_lstm_cn_mat, range(6), range(6))\nplt.figure(figsize = (10, 10))\nsns.heatmap(bi_lstm_cn_mat, annot=True, fmt='d', cmap='Blues')\nplt.title(\"Confusion Matrix for Bi-directional LSTM - Validation\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T12:43:33.710868Z","iopub.execute_input":"2022-12-22T12:43:33.711253Z","iopub.status.idle":"2022-12-22T12:43:34.210921Z","shell.execute_reply.started":"2022-12-22T12:43:33.711221Z","shell.execute_reply":"2022-12-22T12:43:34.209886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Stacked RNN","metadata":{}},{"cell_type":"code","source":"def stacked_rnn():\n    model = Sequential()\n    model.add(SimpleRNN(50, input_shape = (1, 50), return_sequences = True))\n    model.add(SimpleRNN(30, return_sequences = True))\n    model.add(SimpleRNN(20, return_sequences = False))\n    model.add(Dense(10))\n    model.add(Dense(6))\n    model.add(Activation('softmax'))\n    \n    adam = Adam(lr = 0.001)\n    model.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['accuracy'])\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2022-12-22T12:46:33.563884Z","iopub.execute_input":"2022-12-22T12:46:33.564594Z","iopub.status.idle":"2022-12-22T12:46:33.571283Z","shell.execute_reply.started":"2022-12-22T12:46:33.564558Z","shell.execute_reply":"2022-12-22T12:46:33.570196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = KerasClassifier(build_fn = stacked_rnn, epochs = 200, batch_size = 128, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T12:46:57.256767Z","iopub.execute_input":"2022-12-22T12:46:57.257150Z","iopub.status.idle":"2022-12-22T12:46:57.262612Z","shell.execute_reply.started":"2022-12-22T12:46:57.257116Z","shell.execute_reply":"2022-12-22T12:46:57.261529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.backend.clear_session()\n\ncallback = EarlyStopping(monitor = \"accuracy\", patience = 20, restore_best_weights=True) \n\nstack_rnn_history = model.fit(X_train_reshaped, y_train, callbacks = [callback])","metadata":{"execution":{"iopub.status.busy":"2022-12-22T12:47:27.693983Z","iopub.execute_input":"2022-12-22T12:47:27.694930Z","iopub.status.idle":"2022-12-22T13:05:59.178896Z","shell.execute_reply.started":"2022-12-22T12:47:27.694894Z","shell.execute_reply":"2022-12-22T13:05:59.178010Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(ncols = 2, figsize = (10 , 5))\n\naxes[0].plot(stack_rnn_history.history[\"loss\"])\naxes[0].set_title(\"Loss of training\")\n\naxes[1].plot(stack_rnn_history.history[\"accuracy\"])\naxes[1].set_title(\"Accuracy of training\")\n\nfig.suptitle(\"Stacked RNN\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:05:59.182484Z","iopub.execute_input":"2022-12-22T13:05:59.182760Z","iopub.status.idle":"2022-12-22T13:05:59.486806Z","shell.execute_reply.started":"2022-12-22T13:05:59.182734Z","shell.execute_reply":"2022-12-22T13:05:59.485949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_val_reshaped)\n\nprint(accuracy_score(y_pred, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:05:59.487924Z","iopub.execute_input":"2022-12-22T13:05:59.489184Z","iopub.status.idle":"2022-12-22T13:06:04.876663Z","shell.execute_reply.started":"2022-12-22T13:05:59.489147Z","shell.execute_reply":"2022-12-22T13:06:04.875658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val,y_pred))\nstack_rnn_cn_mat = confusion_matrix(y_val,y_pred)\nstack_rnn_cn_mat = pd.DataFrame(stack_rnn_cn_mat, range(6), range(6))\nplt.figure(figsize = (10, 10))\nsns.heatmap(stack_rnn_cn_mat, annot=True, fmt='d', cmap='Blues')\nplt.title(\"Confusion Matrix for Stacked RNN - Validation\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T13:06:04.878827Z","iopub.execute_input":"2022-12-22T13:06:04.879218Z","iopub.status.idle":"2022-12-22T13:06:05.367890Z","shell.execute_reply.started":"2022-12-22T13:06:04.879164Z","shell.execute_reply":"2022-12-22T13:06:05.366820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## BERT","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras.initializers import TruncatedNormal\nfrom tensorflow.keras.losses import CategoricalCrossentropy\nfrom tensorflow.keras.metrics import CategoricalAccuracy\nfrom tensorflow.keras.layers import Input, Dense\n\nfrom tensorflow.keras.utils import to_categorical\n\nimport transformers\nfrom transformers import AutoTokenizer,TFBertModel","metadata":{"execution":{"iopub.status.busy":"2022-12-27T13:48:12.046865Z","iopub.execute_input":"2022-12-27T13:48:12.048386Z","iopub.status.idle":"2022-12-27T13:48:12.055470Z","shell.execute_reply.started":"2022-12-27T13:48:12.048338Z","shell.execute_reply":"2022-12-27T13:48:12.054181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = to_categorical(y_train)\ny_val = to_categorical(y_val)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T13:48:14.575065Z","iopub.execute_input":"2022-12-27T13:48:14.576041Z","iopub.status.idle":"2022-12-27T13:48:14.593973Z","shell.execute_reply.started":"2022-12-27T13:48:14.575862Z","shell.execute_reply":"2022-12-27T13:48:14.592774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train, y_val","metadata":{"execution":{"iopub.status.busy":"2022-12-27T13:48:15.100775Z","iopub.execute_input":"2022-12-27T13:48:15.101141Z","iopub.status.idle":"2022-12-27T13:48:15.111467Z","shell.execute_reply.started":"2022-12-27T13:48:15.101110Z","shell.execute_reply":"2022-12-27T13:48:15.110305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenizer = AutoTokenizer.from_pretrained(\"bert-base-cased\")\nbert = TFBertModel.from_pretrained(\"bert-base-cased\")","metadata":{"execution":{"iopub.status.busy":"2022-12-27T13:48:17.859047Z","iopub.execute_input":"2022-12-27T13:48:17.859417Z","iopub.status.idle":"2022-12-27T13:48:44.321168Z","shell.execute_reply.started":"2022-12-27T13:48:17.859385Z","shell.execute_reply":"2022-12-27T13:48:44.320217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = tokenizer(\n    text= X_train.to_list(),\n    add_special_tokens=True,\n    max_length=70,\n    truncation=True,\n    padding=True, \n    return_tensors='tf',\n    return_token_type_ids = False,\n    return_attention_mask = True,\n    verbose = True)\n\nX_val = tokenizer(\n    text=X_val.tolist(),\n    add_special_tokens=True,\n    max_length=70,\n    truncation=True,\n    padding=True, \n    return_tensors='tf',\n    return_token_type_ids = False,\n    return_attention_mask = True,\n    verbose = True)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T13:48:44.323277Z","iopub.execute_input":"2022-12-27T13:48:44.325577Z","iopub.status.idle":"2022-12-27T13:49:29.339743Z","shell.execute_reply.started":"2022-12-27T13:48:44.325546Z","shell.execute_reply":"2022-12-27T13:49:29.338707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.keys()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T13:49:29.341307Z","iopub.execute_input":"2022-12-27T13:49:29.341681Z","iopub.status.idle":"2022-12-27T13:49:29.348326Z","shell.execute_reply.started":"2022-12-27T13:49:29.341645Z","shell.execute_reply":"2022-12-27T13:49:29.347064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_ids = X_train[\"input_ids\"]\nattention_mask = X_train[\"attention_mask\"]","metadata":{"execution":{"iopub.status.busy":"2022-12-27T13:49:29.350545Z","iopub.execute_input":"2022-12-27T13:49:29.351134Z","iopub.status.idle":"2022-12-27T13:49:29.370540Z","shell.execute_reply.started":"2022-12-27T13:49:29.351099Z","shell.execute_reply":"2022-12-27T13:49:29.369490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The last dense layer doesnot have any Softmax or logits at the end, So argmax might be needed to be applied later\n\nmax_len = 70\ninput_ids = Input(shape=(max_len,), dtype=tf.int32, name=\"input_ids\")\ninput_mask = Input(shape=(max_len,), dtype=tf.int32, name=\"attention_mask\")\nembeddings = bert(input_ids,attention_mask = input_mask)[0] \nout = tf.keras.layers.GlobalMaxPool1D()(embeddings)\nout = Dense(512, activation='relu')(out)\nout = Dense(256, activation='relu')(out)\nout = Dense(128, activation='relu')(out)\nout = Dense(64, activation='relu')(out)\nout = Dense(32,activation = 'relu')(out)\nout = Dense(16, activation='relu')(out)\ny = Dense(6)(out)\nmodel = tf.keras.Model(inputs=[input_ids, input_mask], outputs=y)\nmodel.layers[2].trainable = True","metadata":{"execution":{"iopub.status.busy":"2022-12-27T13:49:33.492625Z","iopub.execute_input":"2022-12-27T13:49:33.493310Z","iopub.status.idle":"2022-12-27T13:49:40.505536Z","shell.execute_reply.started":"2022-12-27T13:49:33.493275Z","shell.execute_reply":"2022-12-27T13:49:40.504492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = Adam( learning_rate = 0.00005, epsilon=1e-08, decay=0.01, clipnorm=1.0)\n# Set loss and metrics\nloss = CategoricalCrossentropy(from_logits = True)\nmetric = CategoricalAccuracy('balanced_accuracy'),\n# Compile the model\nmodel.compile( optimizer = optimizer, loss = loss, metrics = metric)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T13:50:09.171078Z","iopub.execute_input":"2022-12-27T13:50:09.171696Z","iopub.status.idle":"2022-12-27T13:50:09.193713Z","shell.execute_reply.started":"2022-12-27T13:50:09.171660Z","shell.execute_reply":"2022-12-27T13:50:09.192776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.backend.clear_session()\n\ntrain_history = model.fit(\n    x ={'input_ids':X_train['input_ids'],'attention_mask':X_train['attention_mask']} ,\n    y = y_train,\n    validation_data = (\n    {'input_ids':X_val['input_ids'],'attention_mask':X_val['attention_mask']}, y_val ),\n  epochs = 60,\n    batch_size = 64\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T13:50:24.655185Z","iopub.execute_input":"2022-12-27T13:50:24.655551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p saved_model\nmodel.save('saved_model/my_model')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_history.history.keys()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(ncols = 2, nrows = 2, figsize = (10 , 5))\n\naxes[0][0].plot(train_history.history[\"loss\"])\naxes[0][0].set_title(\"Loss of training\")\n\naxes[0][1].plot(train_history.history[\"balanced_accuracy\"])\naxes[0][1].set_title(\"Accuracy of training\")\n\naxes[1][0].plot(train_history.history[\"val_loss\"])\naxes[1][0].set_title(\"Loss of validation\")\n\naxes[1][1].plot(train_history.history[\"val_balanced_accuracy\"])\naxes[1][1].set_title(\"Accuracy of validation\")\n\nfig.suptitle(\"Pre-trained BERT\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_raw = model.predict({'input_ids':X_val['input_ids'],'attention_mask':X_val['attention_mask']})\npredicted_raw[0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix, f1_score, accuracy_score\n\ny_pred = np.argmax(predicted_raw, axis = 1)\nprint(accuracy_score(y_val, y_pred))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val,y_pred))\nbert_cn_mat = confusion_matrix(y_val,y_pred)\nbert_cn_mat = pd.DataFrame(bert_cn_mat, range(6), range(6))\nplt.figure(figsize = (10, 10))\nsns.heatmap(bert_cn_mat, annot=True, fmt='d', cmap='Blues')\nplt.title(\"Confusion Matrix for BERT - Validation\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}