{"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":"from sklearn.model_selection import train_test_split\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.naive_bayes import MultinomialNB\nfrom sklearn.metrics import hamming_loss\nfrom sklearn.metrics import accuracy_score\nfrom wordcloud import WordCloud\nfrom keras.preprocessing.sequence import pad_sequences\nfrom keras.preprocessing.text import Tokenizer\nfrom keras.utils.vis_utils import plot_model\n\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport re\nimport nltk\nimport string\nimport tensorflow as tf\n\nsns.set_theme(style=\"darkgrid\")\nnltk.download('omw-1.4')","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:56:10.671263Z","iopub.execute_input":"2022-08-07T00:56:10.672472Z","iopub.status.idle":"2022-08-07T00:56:19.938206Z","shell.execute_reply.started":"2022-08-07T00:56:10.672362Z","shell.execute_reply":"2022-08-07T00:56:19.936968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"# Loading the data.\ndata  =  pd.read_csv('../input/jigsaw-toxic-comment-classification-challenge/train.csv.zip')","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:56:19.940930Z","iopub.execute_input":"2022-08-07T00:56:19.941767Z","iopub.status.idle":"2022-08-07T00:56:21.836348Z","shell.execute_reply.started":"2022-08-07T00:56:19.941720Z","shell.execute_reply":"2022-08-07T00:56:21.835302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Look at the rop few rows of the data.\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:56:21.839088Z","iopub.execute_input":"2022-08-07T00:56:21.839600Z","iopub.status.idle":"2022-08-07T00:56:21.860495Z","shell.execute_reply.started":"2022-08-07T00:56:21.839555Z","shell.execute_reply":"2022-08-07T00:56:21.859207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check if there are any missing values.\ndata.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:56:21.863992Z","iopub.execute_input":"2022-08-07T00:56:21.865359Z","iopub.status.idle":"2022-08-07T00:56:21.905652Z","shell.execute_reply.started":"2022-08-07T00:56:21.865300Z","shell.execute_reply":"2022-08-07T00:56:21.904110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Storing all possible classes/labels.\nlabels = [\"toxic\", \"severe_toxic\", \"obscene\", \"threat\", \"insult\", \"identity_hate\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:56:21.907856Z","iopub.execute_input":"2022-08-07T00:56:21.909159Z","iopub.status.idle":"2022-08-07T00:56:21.914725Z","shell.execute_reply.started":"2022-08-07T00:56:21.909108Z","shell.execute_reply":"2022-08-07T00:56:21.913756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Computing percentage of each label in the data.\nN = len(data)\nlabel_pct_map = dict()\nfor label in labels:\n    label_pct_map[label] = data[label].sum() / N","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:56:21.916390Z","iopub.execute_input":"2022-08-07T00:56:21.916790Z","iopub.status.idle":"2022-08-07T00:56:21.936014Z","shell.execute_reply.started":"2022-08-07T00:56:21.916738Z","shell.execute_reply":"2022-08-07T00:56:21.934830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting percentage of each class present in the dataset.\nplt.figure(figsize=(15,10))\nax = sns.barplot(x=list(label_pct_map.keys()), y=list(label_pct_map.values()))\nax.set(title=\"Distribution of different classes in the dataset\",\n       xlabel=\"Toxicity Class\",\n       ylabel=\"Percentage\")","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:56:21.938076Z","iopub.execute_input":"2022-08-07T00:56:21.938903Z","iopub.status.idle":"2022-08-07T00:56:22.191958Z","shell.execute_reply.started":"2022-08-07T00:56:21.938845Z","shell.execute_reply":"2022-08-07T00:56:22.190517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting distribution of comment length.\ndata['comment_length'] = data.apply(lambda r: len(r.comment_text.split()), axis=1)\nplt.figure(figsize=(15,10))\nax = sns.histplot(data, x=\"comment_length\")\nax.set(title=\"Distribution of length of comments is right-skewed\", xlabel=\"Length of Comment\")","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:56:22.193736Z","iopub.execute_input":"2022-08-07T00:56:22.194197Z","iopub.status.idle":"2022-08-07T00:56:26.636229Z","shell.execute_reply.started":"2022-08-07T00:56:22.194145Z","shell.execute_reply":"2022-08-07T00:56:26.634995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### As can be seen above, the distribution of comment length is right-skewed.","metadata":{}},{"cell_type":"code","source":"# Visualize the variation of comment length across various classes.\ndata[\"label\"] = \"\"\ntbls = []\nfor label in labels:\n    df = data[data[label] == 1].copy()\n    df[\"label\"] = label\n    df.drop('comment_text', inplace=True, axis=1)\n    df.drop('id', inplace=True, axis=1)\n    tbls.append(df)\ndf = pd.concat(tbls, axis=0)\ndf.reset_index(drop=True, inplace=True)\n\ndata.drop('comment_length', inplace=True, axis=1)\ndata.drop('label', inplace=True, axis=1)\n\nplt.figure(figsize=(15,10))\nax = sns.boxplot(x=\"label\", y=\"comment_length\", palette=\"Set2\", data=df[df['comment_length'] < 100])\nax.set(title=\"Variation of comment length across toxic comments\", xlabel=\"Label\", ylabel=\"Comment Length\")\n\ndel df","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:56:26.637880Z","iopub.execute_input":"2022-08-07T00:56:26.639153Z","iopub.status.idle":"2022-08-07T00:56:27.008204Z","shell.execute_reply.started":"2022-08-07T00:56:26.639089Z","shell.execute_reply":"2022-08-07T00:56:27.006750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### The severe toxic comments seem to be relatively shorter compared to others. Other than that, there is no specific variation in length of comments based on toxicity.","metadata":{"execution":{"iopub.status.busy":"2022-08-01T23:51:08.911408Z","iopub.execute_input":"2022-08-01T23:51:08.911796Z","iopub.status.idle":"2022-08-01T23:51:09.263133Z","shell.execute_reply.started":"2022-08-01T23:51:08.911763Z","shell.execute_reply":"2022-08-01T23:51:09.261719Z"}}},{"cell_type":"code","source":"def clean_comment(comment):\n    \"\"\"\n    Clean comments in the dataset.\n    \n    :param comment: comment string that is present in the input file.\n    :type  comment: str\n    :return: string that has been cleaned using Wordnet Lemmatizer, etc. \n    :rtype: str\n    \"\"\"\n    \n    comment = comment.lower()  # convert to lowercase\n    comment = re.sub(r'^https?:\\/\\/.*[\\s]*', '', comment)  # remove links\n    words = nltk.tokenize.word_tokenize(comment)\n    lemmatizer = nltk.wordnet.WordNetLemmatizer()\n    stop_words = set(nltk.corpus.stopwords.words('english'))\n    final_tokens = []\n    for w in words:\n        w = \"\".join([\"\" if c in string.punctuation else c for c in w])  # remove punctutation\n        if w != \"\" and w not in stop_words:  # process only non stopwords\n            final_tokens.append(lemmatizer.lemmatize(w))  # using WordNet for lemmatizing\n    cleaned_comment = \" \".join(final_tokens)\n    return cleaned_comment","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:56:27.013387Z","iopub.execute_input":"2022-08-07T00:56:27.013794Z","iopub.status.idle":"2022-08-07T00:56:27.022769Z","shell.execute_reply.started":"2022-08-07T00:56:27.013760Z","shell.execute_reply":"2022-08-07T00:56:27.021428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Clean all the comments in the dataset.\ndata['comment'] = data.apply(lambda r: clean_comment(r.comment_text), axis=1)\ndata.drop('comment_text', inplace=True, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:56:27.024377Z","iopub.execute_input":"2022-08-07T00:56:27.024852Z","iopub.status.idle":"2022-08-07T00:59:47.335564Z","shell.execute_reply.started":"2022-08-07T00:56:27.024818Z","shell.execute_reply":"2022-08-07T00:59:47.334205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Removing any empty comments.\ndata = data[data['comment'] != \"\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:59:47.337673Z","iopub.execute_input":"2022-08-07T00:59:47.338178Z","iopub.status.idle":"2022-08-07T00:59:47.384316Z","shell.execute_reply.started":"2022-08-07T00:59:47.338125Z","shell.execute_reply":"2022-08-07T00:59:47.383248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting word cloud of all comments in the dataset.\nwordcloud = WordCloud(stopwords=nltk.corpus.stopwords.words('english'), \n                      max_words=50, background_color=\"white\").generate(str(data.comment))\nplt.figure(figsize=(11,7))\nplt.imshow(wordcloud)\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:59:47.385785Z","iopub.execute_input":"2022-08-07T00:59:47.386174Z","iopub.status.idle":"2022-08-07T00:59:47.716800Z","shell.execute_reply.started":"2022-08-07T00:59:47.386138Z","shell.execute_reply":"2022-08-07T00:59:47.715433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting word cloud of all toxic comments in the dataset.\ntoxic_text = str(data[(data['identity_hate']==1) | \n                      (data['insult']==1) | \n                      (data['obscene']==1) | \n                      (data['severe_toxic']==1) | \n                      (data['threat']==1) | \n                      (data['toxic']==1)].comment)\nwordcloud = WordCloud(stopwords=nltk.corpus.stopwords.words('english'), \n                      max_words=50, background_color=\"black\").generate(toxic_text)\nplt.figure(figsize=(11,7))\nplt.imshow(wordcloud)\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:59:47.718475Z","iopub.execute_input":"2022-08-07T00:59:47.719801Z","iopub.status.idle":"2022-08-07T00:59:48.034927Z","shell.execute_reply.started":"2022-08-07T00:59:47.719750Z","shell.execute_reply":"2022-08-07T00:59:48.034148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* #### As we can see, the toxic comments have a lot of abusive words present.\n* #### Interesting Observation: The word edit/edits is present in both comments and toxic comments. This word has no sentiment as such attached to it. Could this be specific to the dataset and its comments?","metadata":{}},{"cell_type":"code","source":"# Plotting the correlation matrix between various labels.\nplt.figure(figsize=(11, 7))\nsns.heatmap(data.corr(), annot=True, cmap=\"mako_r\")","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:59:48.035990Z","iopub.execute_input":"2022-08-07T00:59:48.037037Z","iopub.status.idle":"2022-08-07T00:59:48.429863Z","shell.execute_reply.started":"2022-08-07T00:59:48.036991Z","shell.execute_reply":"2022-08-07T00:59:48.428517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### I believe it will be more difficult to classify labels that are highly correlated with another label/category since it will have similar words/text.\n\n#### For example, **insult** has significant (though not very high) correlation with **toxic** and **obscence** comments, therefore, it might not be easier to classify.","metadata":{}},{"cell_type":"markdown","source":"# I. Multinomial Naive Bayes using TF-IDF","metadata":{}},{"cell_type":"code","source":"# Split the data into training and test.\n# 80 percent is used for training and 20 percent is used for testing.\n\nX = data['comment']\ny = data[labels].copy()\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, \n                                                    train_size=0.8, test_size=0.2, \n                                                    random_state=42, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:59:48.431225Z","iopub.execute_input":"2022-08-07T00:59:48.431616Z","iopub.status.idle":"2022-08-07T00:59:48.470705Z","shell.execute_reply.started":"2022-08-07T00:59:48.431581Z","shell.execute_reply":"2022-08-07T00:59:48.469642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the TF-IDF matrix\nvectorizer = TfidfVectorizer()\n\nX_train = vectorizer.fit_transform(X_train).toarray()\nX_test = vectorizer.transform(X_test).toarray()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:59:48.472299Z","iopub.execute_input":"2022-08-07T00:59:48.472680Z","iopub.status.idle":"2022-08-07T01:00:18.486779Z","shell.execute_reply.started":"2022-08-07T00:59:48.472644Z","shell.execute_reply":"2022-08-07T01:00:18.485426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fitting classifiers for all the six labels.\nclf = []\nfor i, label in enumerate(labels):\n    clf.append(MultinomialNB())\n    clf[i].fit(X_train, y_train[label])","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:00:18.488425Z","iopub.execute_input":"2022-08-07T01:00:18.489494Z","iopub.status.idle":"2022-08-07T01:04:53.606624Z","shell.execute_reply.started":"2022-08-07T01:00:18.489449Z","shell.execute_reply":"2022-08-07T01:04:53.603615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Getting the prediction values.\ny_pred = []\nfor i in range(len(labels)):\n    y_pred.append(clf[i].predict(X_test))\n\ny_pred = np.asarray(np.transpose(y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:04:53.609762Z","iopub.execute_input":"2022-08-07T01:04:53.610940Z","iopub.status.idle":"2022-08-07T01:05:49.216315Z","shell.execute_reply.started":"2022-08-07T01:04:53.610871Z","shell.execute_reply":"2022-08-07T01:05:49.214504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy: In multilabel classification, this function computes subset accuracy: the set of labels predicted \n# for a sample must exactly match the corresponding set of labels in y_true.\naccuracy_score(y_test.to_numpy(), y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:05:49.224895Z","iopub.execute_input":"2022-08-07T01:05:49.229056Z","iopub.status.idle":"2022-08-07T01:05:49.275919Z","shell.execute_reply.started":"2022-08-07T01:05:49.228967Z","shell.execute_reply":"2022-08-07T01:05:49.274339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hamming loss: Fraction of labels that are incorrectly predicted.\n# It is always between 0 and 1, lower being better.\nhamming_loss(y_test.to_numpy(), y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:05:49.278463Z","iopub.execute_input":"2022-08-07T01:05:49.279618Z","iopub.status.idle":"2022-08-07T01:05:49.306644Z","shell.execute_reply.started":"2022-08-07T01:05:49.279556Z","shell.execute_reply":"2022-08-07T01:05:49.305488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# II. LSTM","metadata":{}},{"cell_type":"code","source":"X = list(data['comment'])\ny = data[labels].values","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:29:06.888869Z","iopub.execute_input":"2022-08-07T01:29:06.889654Z","iopub.status.idle":"2022-08-07T01:32:14.327609Z","shell.execute_reply.started":"2022-08-07T01:29:06.889602Z","shell.execute_reply":"2022-08-07T01:32:14.325958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Defining some constants.\nMAX_LEN = 200 # Maximum number of words in a comment to be used.\nNUM_WORDS = 5000 # Number of unique words to be used.\nWORD_VEC_SIZE = 100 # Size of each word vector.","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:34:43.299784Z","iopub.execute_input":"2022-08-07T01:34:43.300242Z","iopub.status.idle":"2022-08-07T01:34:43.305329Z","shell.execute_reply.started":"2022-08-07T01:34:43.300202Z","shell.execute_reply":"2022-08-07T01:34:43.304103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the data into train and test.\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:34:45.011654Z","iopub.execute_input":"2022-08-07T01:34:45.012088Z","iopub.status.idle":"2022-08-07T01:34:45.091953Z","shell.execute_reply.started":"2022-08-07T01:34:45.012045Z","shell.execute_reply":"2022-08-07T01:34:45.090484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the tokenizer on the training comments.\ntokenizer = Tokenizer(num_words=NUM_WORDS)\ntokenizer.fit_on_texts(X_train)\n\nX_train = tokenizer.texts_to_sequences(X_train)\nX_test = tokenizer.texts_to_sequences(X_test)\n\nvocab_size = len(tokenizer.word_index) + 1\n\n# Pad the the training and test sequences so all of them have the same length.\nX_train = pad_sequences(X_train, padding='post', maxlen=MAX_LEN)\nX_test = pad_sequences(X_test, padding='post', maxlen=MAX_LEN)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:34:46.519402Z","iopub.execute_input":"2022-08-07T01:34:46.520241Z","iopub.status.idle":"2022-08-07T01:34:59.383568Z","shell.execute_reply.started":"2022-08-07T01:34:46.520190Z","shell.execute_reply":"2022-08-07T01:34:59.381983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The glove_dictionary contains words vs their respective embeddings.                                                                   #\nglove_dictionary = dict()\nwith open(\"../input/glove6b100dtxt/glove.6B.100d.txt\", encoding=\"utf8\") as f:\n    for line in f:\n        line = line.split()\n        word, coefficients = line[0], line[1:]\n        glove_dictionary[word] = np.asarray(coefficients, dtype='float32')","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:34:59.386211Z","iopub.execute_input":"2022-08-07T01:34:59.387322Z","iopub.status.idle":"2022-08-07T01:35:08.397404Z","shell.execute_reply.started":"2022-08-07T01:34:59.387268Z","shell.execute_reply":"2022-08-07T01:35:08.396211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the embedding matrix using the glove_dictionary  \nembedding_matrix = np.zeros((vocab_size, WORD_VEC_SIZE))\nfor word, i in tokenizer.word_index.items():\n    if word in glove_dictionary:\n        embedding_matrix[i] = glove_dictionary[word]","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:35:08.399516Z","iopub.execute_input":"2022-08-07T01:35:08.400033Z","iopub.status.idle":"2022-08-07T01:35:08.796295Z","shell.execute_reply.started":"2022-08-07T01:35:08.399967Z","shell.execute_reply":"2022-08-07T01:35:08.794950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model(embedding_matrix):\n    \"\"\"\n    Build the LSTM model.\n    \n    :param embedding_matrix: a matrix with the corresponding embedding of all words.\n    :return: the created LSTM model.\n    \"\"\"\n    # Input layer.\n    inp = tf.keras.layers.Input(shape=(MAX_LEN,), name=\"input\")\n\n    # Add embedding layer.\n    x = tf.keras.layers.Embedding(vocab_size, WORD_VEC_SIZE, \n                                  weights=[embedding_matrix], \n                                  trainable=False)(inp)\n\n    # Add LSTM layer.\n    x = tf.keras.layers.LSTM(128)(x)\n\n    # Output layer.\n    x = tf.keras.layers.Dense(6, activation='softmax', name=\"output\")(x)\n    \n    model = tf.keras.models.Model(inputs=inp, outputs=x)\n    \n    model.compile(loss='categorical_crossentropy', \n                  optimizer='rmsprop', \n                  metrics=['acc'])\n                  \n    return model","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:35:08.798760Z","iopub.execute_input":"2022-08-07T01:35:08.799133Z","iopub.status.idle":"2022-08-07T01:35:08.807602Z","shell.execute_reply.started":"2022-08-07T01:35:08.799098Z","shell.execute_reply":"2022-08-07T01:35:08.806533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_model(embedding_matrix)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:35:08.809168Z","iopub.execute_input":"2022-08-07T01:35:08.809532Z","iopub.status.idle":"2022-08-07T01:35:09.257760Z","shell.execute_reply.started":"2022-08-07T01:35:08.809498Z","shell.execute_reply":"2022-08-07T01:35:09.256428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model, to_file='LSTM_model.png', show_shapes=True, \n                          show_dtype=False, show_layer_names=True, dpi=96)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:06:15.194669Z","iopub.execute_input":"2022-08-07T01:06:15.195035Z","iopub.status.idle":"2022-08-07T01:06:16.398163Z","shell.execute_reply.started":"2022-08-07T01:06:15.194989Z","shell.execute_reply":"2022-08-07T01:06:16.396492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_fit = model.fit(X_train, y_train, epochs=5, batch_size=128, verbose=1, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:35:09.259032Z","iopub.execute_input":"2022-08-07T01:35:09.260219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, accuracy = model.evaluate(X_test, y_test, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:12:53.114163Z","iopub.status.idle":"2022-08-07T01:12:53.114589Z","shell.execute_reply.started":"2022-08-07T01:12:53.114386Z","shell.execute_reply":"2022-08-07T01:12:53.114404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplots(figsize=(11,6))\nplt.plot(model_fit.history['loss'], marker='.', color='b', \n         label= 'train loss')\nplt.plot(model_fit.history['val_loss'], marker = '+', color = 'g',\n         label = 'val loss')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:12:53.116649Z","iopub.status.idle":"2022-08-07T01:12:53.117114Z","shell.execute_reply.started":"2022-08-07T01:12:53.116853Z","shell.execute_reply":"2022-08-07T01:12:53.116870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplots(figsize=(11,6))\nplt.plot(model_fit.history['acc'], marker='.', color='b', \n         label= 'train accuracy')\nplt.plot(model_fit.history['val_acc'], marker = '+', color = 'g',\n         label = 'val accuracy')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T01:12:53.118584Z","iopub.status.idle":"2022-08-07T01:12:53.119009Z","shell.execute_reply.started":"2022-08-07T01:12:53.118803Z","shell.execute_reply":"2022-08-07T01:12:53.118823Z"},"trusted":true},"execution_count":null,"outputs":[]}]}