Rachel1809/toxic-comment-classification
0
1{2 "cells": [3 {4 "cell_type": "code",5 "execution_count": null,6 "metadata": {7 "colab": {8 "base_uri": "https://localhost:8080/"9 },10 "id": "8DfEKlbt_TMI",11 "outputId": "79666846-0691-490a-88b0-5f56f4769772"12 },13 "outputs": [14 {15 "output_type": "stream",16 "name": "stdout",17 "text": [18 "Mounted at /content/drive/\n"19 ]20 }21 ],22 "source": [23 "from google.colab import drive\n",24 "drive.mount('/content/drive/')"25 ],26 "id": "8DfEKlbt_TMI"27 },28 {29 "cell_type": "markdown",30 "metadata": {31 "id": "8c25705b"32 },33 "source": [34 "# 1. Import libraries and load data"35 ],36 "id": "8c25705b"37 },38 {39 "cell_type": "code",40 "execution_count": null,41 "metadata": {42 "id": "5b07ecd3"43 },44 "outputs": [],45 "source": [46 "import os\n",47 "import pandas as pd\n",48 "import tensorflow as tf\n",49 "import numpy as np"50 ],51 "id": "5b07ecd3"52 },53 {54 "cell_type": "code",55 "execution_count": null,56 "metadata": {57 "id": "91d7e1f0"58 },59 "outputs": [],60 "source": [61 "df = pd.read_csv(os.path.join(\"/content/drive/MyDrive/ColabNotebooks/data\", \"train.csv\"))"62 ],63 "id": "91d7e1f0"64 },65 {66 "cell_type": "code",67 "execution_count": null,68 "metadata": {69 "colab": {70 "base_uri": "https://localhost:8080/",71 "height": 81572 },73 "id": "1be479a4",74 "outputId": "88d487c7-8f13-43fe-e866-3c472a6f03d9"75 },76 "outputs": [77 {78 "output_type": "execute_result",79 "data": {80 "text/plain": [81 " id comment_text \\\n",82 "0 0000997932d777bf Explanation\\nWhy the edits made under my usern... \n",83 "1 000103f0d9cfb60f D'aww! He matches this background colour I'm s... \n",84 "2 000113f07ec002fd Hey man, I'm really not trying to edit war. It... \n",85 "3 0001b41b1c6bb37e \"\\nMore\\nI can't make any real suggestions on ... \n",86 "4 0001d958c54c6e35 You, sir, are my hero. Any chance you remember... \n",87 "... ... ... \n",88 "159566 ffe987279560d7ff \":::::And for the second time of asking, when ... \n",89 "159567 ffea4adeee384e90 You should be ashamed of yourself \\n\\nThat is ... \n",90 "159568 ffee36eab5c267c9 Spitzer \\n\\nUmm, theres no actual article for ... \n",91 "159569 fff125370e4aaaf3 And it looks like it was actually you who put ... \n",92 "159570 fff46fc426af1f9a \"\\nAnd ... I really don't think you understand... \n",93 "\n",94 " toxic severe_toxic obscene threat insult identity_hate \n",95 "0 0 0 0 0 0 0 \n",96 "1 0 0 0 0 0 0 \n",97 "2 0 0 0 0 0 0 \n",98 "3 0 0 0 0 0 0 \n",99 "4 0 0 0 0 0 0 \n",100 "... ... ... ... ... ... ... \n",101 "159566 0 0 0 0 0 0 \n",102 "159567 0 0 0 0 0 0 \n",103 "159568 0 0 0 0 0 0 \n",104 "159569 0 0 0 0 0 0 \n",105 "159570 0 0 0 0 0 0 \n",106 "\n",107 "[159571 rows x 8 columns]"108 ],109 "text/html": [110 "\n",111 " <div id=\"df-701bc9b7-727d-42d9-9139-de748ebf4501\">\n",112 " <div class=\"colab-df-container\">\n",113 " <div>\n",114 "<style scoped>\n",115 " .dataframe tbody tr th:only-of-type {\n",116 " vertical-align: middle;\n",117 " }\n",118 "\n",119 " .dataframe tbody tr th {\n",120 " vertical-align: top;\n",121 " }\n",122 "\n",123 " .dataframe thead th {\n",124 " text-align: right;\n",125 " }\n",126 "</style>\n",127 "<table border=\"1\" class=\"dataframe\">\n",128 " <thead>\n",129 " <tr style=\"text-align: right;\">\n",130 " <th></th>\n",131 " <th>id</th>\n",132 " <th>comment_text</th>\n",133 " <th>toxic</th>\n",134 " <th>severe_toxic</th>\n",135 " <th>obscene</th>\n",136 " <th>threat</th>\n",137 " <th>insult</th>\n",138 " <th>identity_hate</th>\n",139 " </tr>\n",140 " </thead>\n",141 " <tbody>\n",142 " <tr>\n",143 " <th>0</th>\n",144 " <td>0000997932d777bf</td>\n",145 " <td>Explanation\\nWhy the edits made under my usern...</td>\n",146 " <td>0</td>\n",147 " <td>0</td>\n",148 " <td>0</td>\n",149 " <td>0</td>\n",150 " <td>0</td>\n",151 " <td>0</td>\n",152 " </tr>\n",153 " <tr>\n",154 " <th>1</th>\n",155 " <td>000103f0d9cfb60f</td>\n",156 " <td>D'aww! He matches this background colour I'm s...</td>\n",157 " <td>0</td>\n",158 " <td>0</td>\n",159 " <td>0</td>\n",160 " <td>0</td>\n",161 " <td>0</td>\n",162 " <td>0</td>\n",163 " </tr>\n",164 " <tr>\n",165 " <th>2</th>\n",166 " <td>000113f07ec002fd</td>\n",167 " <td>Hey man, I'm really not trying to edit war. It...</td>\n",168 " <td>0</td>\n",169 " <td>0</td>\n",170 " <td>0</td>\n",171 " <td>0</td>\n",172 " <td>0</td>\n",173 " <td>0</td>\n",174 " </tr>\n",175 " <tr>\n",176 " <th>3</th>\n",177 " <td>0001b41b1c6bb37e</td>\n",178 " <td>\"\\nMore\\nI can't make any real suggestions on ...</td>\n",179 " <td>0</td>\n",180 " <td>0</td>\n",181 " <td>0</td>\n",182 " <td>0</td>\n",183 " <td>0</td>\n",184 " <td>0</td>\n",185 " </tr>\n",186 " <tr>\n",187 " <th>4</th>\n",188 " <td>0001d958c54c6e35</td>\n",189 " <td>You, sir, are my hero. Any chance you remember...</td>\n",190 " <td>0</td>\n",191 " <td>0</td>\n",192 " <td>0</td>\n",193 " <td>0</td>\n",194 " <td>0</td>\n",195 " <td>0</td>\n",196 " </tr>\n",197 " <tr>\n",198 " <th>...</th>\n",199 " <td>...</td>\n",200 " <td>...</td>\n",201 " <td>...</td>\n",202 " <td>...</td>\n",203 " <td>...</td>\n",204 " <td>...</td>\n",205 " <td>...</td>\n",206 " <td>...</td>\n",207 " </tr>\n",208 " <tr>\n",209 " <th>159566</th>\n",210 " <td>ffe987279560d7ff</td>\n",211 " <td>\":::::And for the second time of asking, when ...</td>\n",212 " <td>0</td>\n",213 " <td>0</td>\n",214 " <td>0</td>\n",215 " <td>0</td>\n",216 " <td>0</td>\n",217 " <td>0</td>\n",218 " </tr>\n",219 " <tr>\n",220 " <th>159567</th>\n",221 " <td>ffea4adeee384e90</td>\n",222 " <td>You should be ashamed of yourself \\n\\nThat is ...</td>\n",223 " <td>0</td>\n",224 " <td>0</td>\n",225 " <td>0</td>\n",226 " <td>0</td>\n",227 " <td>0</td>\n",228 " <td>0</td>\n",229 " </tr>\n",230 " <tr>\n",231 " <th>159568</th>\n",232 " <td>ffee36eab5c267c9</td>\n",233 " <td>Spitzer \\n\\nUmm, theres no actual article for ...</td>\n",234 " <td>0</td>\n",235 " <td>0</td>\n",236 " <td>0</td>\n",237 " <td>0</td>\n",238 " <td>0</td>\n",239 " <td>0</td>\n",240 " </tr>\n",241 " <tr>\n",242 " <th>159569</th>\n",243 " <td>fff125370e4aaaf3</td>\n",244 " <td>And it looks like it was actually you who put ...</td>\n",245 " <td>0</td>\n",246 " <td>0</td>\n",247 " <td>0</td>\n",248 " <td>0</td>\n",249 " <td>0</td>\n",250 " <td>0</td>\n",251 " </tr>\n",252 " <tr>\n",253 " <th>159570</th>\n",254 " <td>fff46fc426af1f9a</td>\n",255 " <td>\"\\nAnd ... I really don't think you understand...</td>\n",256 " <td>0</td>\n",257 " <td>0</td>\n",258 " <td>0</td>\n",259 " <td>0</td>\n",260 " <td>0</td>\n",261 " <td>0</td>\n",262 " </tr>\n",263 " </tbody>\n",264 "</table>\n",265 "<p>159571 rows × 8 columns</p>\n",266 "</div>\n",267 " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-701bc9b7-727d-42d9-9139-de748ebf4501')\"\n",268 " title=\"Convert this dataframe to an interactive table.\"\n",269 " style=\"display:none;\">\n",270 " \n",271 " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",272 " width=\"24px\">\n",273 " <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",274 " <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",275 " </svg>\n",276 " </button>\n",277 " \n",278 " <style>\n",279 " .colab-df-container {\n",280 " display:flex;\n",281 " flex-wrap:wrap;\n",282 " gap: 12px;\n",283 " }\n",284 "\n",285 " .colab-df-convert {\n",286 " background-color: #E8F0FE;\n",287 " border: none;\n",288 " border-radius: 50%;\n",289 " cursor: pointer;\n",290 " display: none;\n",291 " fill: #1967D2;\n",292 " height: 32px;\n",293 " padding: 0 0 0 0;\n",294 " width: 32px;\n",295 " }\n",296 "\n",297 " .colab-df-convert:hover {\n",298 " background-color: #E2EBFA;\n",299 " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",300 " fill: #174EA6;\n",301 " }\n",302 "\n",303 " [theme=dark] .colab-df-convert {\n",304 " background-color: #3B4455;\n",305 " fill: #D2E3FC;\n",306 " }\n",307 "\n",308 " [theme=dark] .colab-df-convert:hover {\n",309 " background-color: #434B5C;\n",310 " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",311 " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",312 " fill: #FFFFFF;\n",313 " }\n",314 " </style>\n",315 "\n",316 " <script>\n",317 " const buttonEl =\n",318 " document.querySelector('#df-701bc9b7-727d-42d9-9139-de748ebf4501 button.colab-df-convert');\n",319 " buttonEl.style.display =\n",320 " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",321 "\n",322 " async function convertToInteractive(key) {\n",323 " const element = document.querySelector('#df-701bc9b7-727d-42d9-9139-de748ebf4501');\n",324 " const dataTable =\n",325 " await google.colab.kernel.invokeFunction('convertToInteractive',\n",326 " [key], {});\n",327 " if (!dataTable) return;\n",328 "\n",329 " const docLinkHtml = 'Like what you see? Visit the ' +\n",330 " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",331 " + ' to learn more about interactive tables.';\n",332 " element.innerHTML = '';\n",333 " dataTable['output_type'] = 'display_data';\n",334 " await google.colab.output.renderOutput(dataTable, element);\n",335 " const docLink = document.createElement('div');\n",336 " docLink.innerHTML = docLinkHtml;\n",337 " element.appendChild(docLink);\n",338 " }\n",339 " </script>\n",340 " </div>\n",341 " </div>\n",342 " "343 ]344 },345 "metadata": {},346 "execution_count": 4347 }348 ],349 "source": [350 "df"351 ],352 "id": "1be479a4"353 },354 {355 "cell_type": "markdown",356 "metadata": {357 "id": "e352d92f"358 },359 "source": [360 "# 2. Preprocessing"361 ],362 "id": "e352d92f"363 },364 {365 "cell_type": "markdown",366 "metadata": {367 "id": "dc5fe893"368 },369 "source": [370 "## 2.1. Data overview"371 ],372 "id": "dc5fe893"373 },374 {375 "cell_type": "code",376 "execution_count": null,377 "metadata": {378 "colab": {379 "base_uri": "https://localhost:8080/",380 "height": 424381 },382 "id": "ea6fd11e",383 "outputId": "adb8a890-565d-4e5b-da14-d7f11db89735"384 },385 "outputs": [386 {387 "output_type": "execute_result",388 "data": {389 "text/plain": [390 " toxic severe_toxic obscene threat insult identity_hate\n",391 "0 0 0 0 0 0 0\n",392 "1 0 0 0 0 0 0\n",393 "2 0 0 0 0 0 0\n",394 "3 0 0 0 0 0 0\n",395 "4 0 0 0 0 0 0\n",396 "... ... ... ... ... ... ...\n",397 "159566 0 0 0 0 0 0\n",398 "159567 0 0 0 0 0 0\n",399 "159568 0 0 0 0 0 0\n",400 "159569 0 0 0 0 0 0\n",401 "159570 0 0 0 0 0 0\n",402 "\n",403 "[159571 rows x 6 columns]"404 ],405 "text/html": [406 "\n",407 " <div id=\"df-30c83a4a-ec86-4758-82b4-5a5e6df88b01\">\n",408 " <div class=\"colab-df-container\">\n",409 " <div>\n",410 "<style scoped>\n",411 " .dataframe tbody tr th:only-of-type {\n",412 " vertical-align: middle;\n",413 " }\n",414 "\n",415 " .dataframe tbody tr th {\n",416 " vertical-align: top;\n",417 " }\n",418 "\n",419 " .dataframe thead th {\n",420 " text-align: right;\n",421 " }\n",422 "</style>\n",423 "<table border=\"1\" class=\"dataframe\">\n",424 " <thead>\n",425 " <tr style=\"text-align: right;\">\n",426 " <th></th>\n",427 " <th>toxic</th>\n",428 " <th>severe_toxic</th>\n",429 " <th>obscene</th>\n",430 " <th>threat</th>\n",431 " <th>insult</th>\n",432 " <th>identity_hate</th>\n",433 " </tr>\n",434 " </thead>\n",435 " <tbody>\n",436 " <tr>\n",437 " <th>0</th>\n",438 " <td>0</td>\n",439 " <td>0</td>\n",440 " <td>0</td>\n",441 " <td>0</td>\n",442 " <td>0</td>\n",443 " <td>0</td>\n",444 " </tr>\n",445 " <tr>\n",446 " <th>1</th>\n",447 " <td>0</td>\n",448 " <td>0</td>\n",449 " <td>0</td>\n",450 " <td>0</td>\n",451 " <td>0</td>\n",452 " <td>0</td>\n",453 " </tr>\n",454 " <tr>\n",455 " <th>2</th>\n",456 " <td>0</td>\n",457 " <td>0</td>\n",458 " <td>0</td>\n",459 " <td>0</td>\n",460 " <td>0</td>\n",461 " <td>0</td>\n",462 " </tr>\n",463 " <tr>\n",464 " <th>3</th>\n",465 " <td>0</td>\n",466 " <td>0</td>\n",467 " <td>0</td>\n",468 " <td>0</td>\n",469 " <td>0</td>\n",470 " <td>0</td>\n",471 " </tr>\n",472 " <tr>\n",473 " <th>4</th>\n",474 " <td>0</td>\n",475 " <td>0</td>\n",476 " <td>0</td>\n",477 " <td>0</td>\n",478 " <td>0</td>\n",479 " <td>0</td>\n",480 " </tr>\n",481 " <tr>\n",482 " <th>...</th>\n",483 " <td>...</td>\n",484 " <td>...</td>\n",485 " <td>...</td>\n",486 " <td>...</td>\n",487 " <td>...</td>\n",488 " <td>...</td>\n",489 " </tr>\n",490 " <tr>\n",491 " <th>159566</th>\n",492 " <td>0</td>\n",493 " <td>0</td>\n",494 " <td>0</td>\n",495 " <td>0</td>\n",496 " <td>0</td>\n",497 " <td>0</td>\n",498 " </tr>\n",499 " <tr>\n",500 " <th>159567</th>\n",501 " <td>0</td>\n",502 " <td>0</td>\n",503 " <td>0</td>\n",504 " <td>0</td>\n",505 " <td>0</td>\n",506 " <td>0</td>\n",507 " </tr>\n",508 " <tr>\n",509 " <th>159568</th>\n",510 " <td>0</td>\n",511 " <td>0</td>\n",512 " <td>0</td>\n",513 " <td>0</td>\n",514 " <td>0</td>\n",515 " <td>0</td>\n",516 " </tr>\n",517 " <tr>\n",518 " <th>159569</th>\n",519 " <td>0</td>\n",520 " <td>0</td>\n",521 " <td>0</td>\n",522 " <td>0</td>\n",523 " <td>0</td>\n",524 " <td>0</td>\n",525 " </tr>\n",526 " <tr>\n",527 " <th>159570</th>\n",528 " <td>0</td>\n",529 " <td>0</td>\n",530 " <td>0</td>\n",531 " <td>0</td>\n",532 " <td>0</td>\n",533 " <td>0</td>\n",534 " </tr>\n",535 " </tbody>\n",536 "</table>\n",537 "<p>159571 rows × 6 columns</p>\n",538 "</div>\n",539 " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-30c83a4a-ec86-4758-82b4-5a5e6df88b01')\"\n",540 " title=\"Convert this dataframe to an interactive table.\"\n",541 " style=\"display:none;\">\n",542 " \n",543 " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",544 " width=\"24px\">\n",545 " <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",546 " <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",547 " </svg>\n",548 " </button>\n",549 " \n",550 " <style>\n",551 " .colab-df-container {\n",552 " display:flex;\n",553 " flex-wrap:wrap;\n",554 " gap: 12px;\n",555 " }\n",556 "\n",557 " .colab-df-convert {\n",558 " background-color: #E8F0FE;\n",559 " border: none;\n",560 " border-radius: 50%;\n",561 " cursor: pointer;\n",562 " display: none;\n",563 " fill: #1967D2;\n",564 " height: 32px;\n",565 " padding: 0 0 0 0;\n",566 " width: 32px;\n",567 " }\n",568 "\n",569 " .colab-df-convert:hover {\n",570 " background-color: #E2EBFA;\n",571 " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",572 " fill: #174EA6;\n",573 " }\n",574 "\n",575 " [theme=dark] .colab-df-convert {\n",576 " background-color: #3B4455;\n",577 " fill: #D2E3FC;\n",578 " }\n",579 "\n",580 " [theme=dark] .colab-df-convert:hover {\n",581 " background-color: #434B5C;\n",582 " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",583 " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",584 " fill: #FFFFFF;\n",585 " }\n",586 " </style>\n",587 "\n",588 " <script>\n",589 " const buttonEl =\n",590 " document.querySelector('#df-30c83a4a-ec86-4758-82b4-5a5e6df88b01 button.colab-df-convert');\n",591 " buttonEl.style.display =\n",592 " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",593 "\n",594 " async function convertToInteractive(key) {\n",595 " const element = document.querySelector('#df-30c83a4a-ec86-4758-82b4-5a5e6df88b01');\n",596 " const dataTable =\n",597 " await google.colab.kernel.invokeFunction('convertToInteractive',\n",598 " [key], {});\n",599 " if (!dataTable) return;\n",600 "\n",601 " const docLinkHtml = 'Like what you see? Visit the ' +\n",602 " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",603 " + ' to learn more about interactive tables.';\n",604 " element.innerHTML = '';\n",605 " dataTable['output_type'] = 'display_data';\n",606 " await google.colab.output.renderOutput(dataTable, element);\n",607 " const docLink = document.createElement('div');\n",608 " docLink.innerHTML = docLinkHtml;\n",609 " element.appendChild(docLink);\n",610 " }\n",611 " </script>\n",612 " </div>\n",613 " </div>\n",614 " "615 ]616 },617 "metadata": {},618 "execution_count": 5619 }620 ],621 "source": [622 "df[df.columns[2:]]"623 ],624 "id": "ea6fd11e"625 },626 {627 "cell_type": "code",628 "execution_count": null,629 "metadata": {630 "colab": {631 "base_uri": "https://localhost:8080/",632 "height": 389633 },634 "id": "7eb94a81",635 "outputId": "c765800b-02a7-4e91-fe92-ae13b8d943ba"636 },637 "outputs": [638 {639 "output_type": "execute_result",640 "data": {641 "text/plain": [642 " id comment_text \\\n",643 "6 0002bcb3da6cb337 COCKSUCKER BEFORE YOU PISS AROUND ON MY WORK \n",644 "12 0005c987bdfc9d4b Hey... what is it..\\n@ | talk .\\nWhat is it...... \n",645 "16 0007e25b2121310b Bye! \\n\\nDon't look, come or think of comming ... \n",646 "42 001810bf8c45bf5f You are gay or antisemmitian? \\n\\nArchangel WH... \n",647 "43 00190820581d90ce FUCK YOUR FILTHY MOTHER IN THE ASS, DRY! \n",648 "\n",649 " toxic severe_toxic obscene threat insult identity_hate \n",650 "6 1 1 1 0 1 0 \n",651 "12 1 0 0 0 0 0 \n",652 "16 1 0 0 0 0 0 \n",653 "42 1 0 1 0 1 1 \n",654 "43 1 0 1 0 1 0 "655 ],656 "text/html": [657 "\n",658 " <div id=\"df-d62ae80e-064e-4795-9ce5-c8cc1659ce62\">\n",659 " <div class=\"colab-df-container\">\n",660 " <div>\n",661 "<style scoped>\n",662 " .dataframe tbody tr th:only-of-type {\n",663 " vertical-align: middle;\n",664 " }\n",665 "\n",666 " .dataframe tbody tr th {\n",667 " vertical-align: top;\n",668 " }\n",669 "\n",670 " .dataframe thead th {\n",671 " text-align: right;\n",672 " }\n",673 "</style>\n",674 "<table border=\"1\" class=\"dataframe\">\n",675 " <thead>\n",676 " <tr style=\"text-align: right;\">\n",677 " <th></th>\n",678 " <th>id</th>\n",679 " <th>comment_text</th>\n",680 " <th>toxic</th>\n",681 " <th>severe_toxic</th>\n",682 " <th>obscene</th>\n",683 " <th>threat</th>\n",684 " <th>insult</th>\n",685 " <th>identity_hate</th>\n",686 " </tr>\n",687 " </thead>\n",688 " <tbody>\n",689 " <tr>\n",690 " <th>6</th>\n",691 " <td>0002bcb3da6cb337</td>\n",692 " <td>COCKSUCKER BEFORE YOU PISS AROUND ON MY WORK</td>\n",693 " <td>1</td>\n",694 " <td>1</td>\n",695 " <td>1</td>\n",696 " <td>0</td>\n",697 " <td>1</td>\n",698 " <td>0</td>\n",699 " </tr>\n",700 " <tr>\n",701 " <th>12</th>\n",702 " <td>0005c987bdfc9d4b</td>\n",703 " <td>Hey... what is it..\\n@ | talk .\\nWhat is it......</td>\n",704 " <td>1</td>\n",705 " <td>0</td>\n",706 " <td>0</td>\n",707 " <td>0</td>\n",708 " <td>0</td>\n",709 " <td>0</td>\n",710 " </tr>\n",711 " <tr>\n",712 " <th>16</th>\n",713 " <td>0007e25b2121310b</td>\n",714 " <td>Bye! \\n\\nDon't look, come or think of comming ...</td>\n",715 " <td>1</td>\n",716 " <td>0</td>\n",717 " <td>0</td>\n",718 " <td>0</td>\n",719 " <td>0</td>\n",720 " <td>0</td>\n",721 " </tr>\n",722 " <tr>\n",723 " <th>42</th>\n",724 " <td>001810bf8c45bf5f</td>\n",725 " <td>You are gay or antisemmitian? \\n\\nArchangel WH...</td>\n",726 " <td>1</td>\n",727 " <td>0</td>\n",728 " <td>1</td>\n",729 " <td>0</td>\n",730 " <td>1</td>\n",731 " <td>1</td>\n",732 " </tr>\n",733 " <tr>\n",734 " <th>43</th>\n",735 " <td>00190820581d90ce</td>\n",736 " <td>FUCK YOUR FILTHY MOTHER IN THE ASS, DRY!</td>\n",737 " <td>1</td>\n",738 " <td>0</td>\n",739 " <td>1</td>\n",740 " <td>0</td>\n",741 " <td>1</td>\n",742 " <td>0</td>\n",743 " </tr>\n",744 " </tbody>\n",745 "</table>\n",746 "</div>\n",747 " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-d62ae80e-064e-4795-9ce5-c8cc1659ce62')\"\n",748 " title=\"Convert this dataframe to an interactive table.\"\n",749 " style=\"display:none;\">\n",750 " \n",751 " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",752 " width=\"24px\">\n",753 " <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",754 " <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",755 " </svg>\n",756 " </button>\n",757 " \n",758 " <style>\n",759 " .colab-df-container {\n",760 " display:flex;\n",761 " flex-wrap:wrap;\n",762 " gap: 12px;\n",763 " }\n",764 "\n",765 " .colab-df-convert {\n",766 " background-color: #E8F0FE;\n",767 " border: none;\n",768 " border-radius: 50%;\n",769 " cursor: pointer;\n",770 " display: none;\n",771 " fill: #1967D2;\n",772 " height: 32px;\n",773 " padding: 0 0 0 0;\n",774 " width: 32px;\n",775 " }\n",776 "\n",777 " .colab-df-convert:hover {\n",778 " background-color: #E2EBFA;\n",779 " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",780 " fill: #174EA6;\n",781 " }\n",782 "\n",783 " [theme=dark] .colab-df-convert {\n",784 " background-color: #3B4455;\n",785 " fill: #D2E3FC;\n",786 " }\n",787 "\n",788 " [theme=dark] .colab-df-convert:hover {\n",789 " background-color: #434B5C;\n",790 " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",791 " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",792 " fill: #FFFFFF;\n",793 " }\n",794 " </style>\n",795 "\n",796 " <script>\n",797 " const buttonEl =\n",798 " document.querySelector('#df-d62ae80e-064e-4795-9ce5-c8cc1659ce62 button.colab-df-convert');\n",799 " buttonEl.style.display =\n",800 " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",801 "\n",802 " async function convertToInteractive(key) {\n",803 " const element = document.querySelector('#df-d62ae80e-064e-4795-9ce5-c8cc1659ce62');\n",804 " const dataTable =\n",805 " await google.colab.kernel.invokeFunction('convertToInteractive',\n",806 " [key], {});\n",807 " if (!dataTable) return;\n",808 "\n",809 " const docLinkHtml = 'Like what you see? Visit the ' +\n",810 " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",811 " + ' to learn more about interactive tables.';\n",812 " element.innerHTML = '';\n",813 " dataTable['output_type'] = 'display_data';\n",814 " await google.colab.output.renderOutput(dataTable, element);\n",815 " const docLink = document.createElement('div');\n",816 " docLink.innerHTML = docLinkHtml;\n",817 " element.appendChild(docLink);\n",818 " }\n",819 " </script>\n",820 " </div>\n",821 " </div>\n",822 " "823 ]824 },825 "metadata": {},826 "execution_count": 6827 }828 ],829 "source": [830 "df.loc[df.iloc[:, 2]==1].head()"831 ],832 "id": "7eb94a81"833 },834 {835 "cell_type": "code",836 "execution_count": null,837 "metadata": {838 "colab": {839 "base_uri": "https://localhost:8080/",840 "height": 87841 },842 "id": "2bb35d57",843 "outputId": "c9531968-a5f4-4348-a833-4a366ee59010"844 },845 "outputs": [846 {847 "output_type": "execute_result",848 "data": {849 "text/plain": [850 "'Hey... what is it..\\n@ | talk .\\nWhat is it... an exclusive group of some WP TALIBANS...who are good at destroying, self-appointed purist who GANG UP any one who asks them questions abt their ANTI-SOCIAL and DESTRUCTIVE (non)-contribution at WP?\\n\\nAsk Sityush to clean up his behavior than issue me nonsensical warnings...'"851 ],852 "application/vnd.google.colaboratory.intrinsic+json": {853 "type": "string"854 }855 },856 "metadata": {},857 "execution_count": 7858 }859 ],860 "source": [861 "df.iloc[12].comment_text"862 ],863 "id": "2bb35d57"864 },865 {866 "cell_type": "markdown",867 "metadata": {868 "id": "1fdd25c4"869 },870 "source": [871 "## 2.2. Data preprocessing"872 ],873 "id": "1fdd25c4"874 },875 {876 "cell_type": "code",877 "execution_count": null,878 "metadata": {879 "id": "c8bd9d59"880 },881 "outputs": [],882 "source": [883 "from tensorflow.keras.layers import TextVectorization"884 ],885 "id": "c8bd9d59"886 },887 {888 "cell_type": "code",889 "execution_count": null,890 "metadata": {891 "colab": {892 "base_uri": "https://localhost:8080/"893 },894 "id": "b8c03840",895 "outputId": "d16ec2c2-b1d1-4956-a11b-6f8e1960a5b8"896 },897 "outputs": [898 {899 "output_type": "execute_result",900 "data": {901 "text/plain": [902 "Index(['id', 'comment_text', 'toxic', 'severe_toxic', 'obscene', 'threat',\n",903 " 'insult', 'identity_hate'],\n",904 " dtype='object')"905 ]906 },907 "metadata": {},908 "execution_count": 9909 }910 ],911 "source": [912 "df.columns"913 ],914 "id": "b8c03840"915 },916 {917 "cell_type": "code",918 "execution_count": null,919 "metadata": {920 "id": "2e64c456"921 },922 "outputs": [],923 "source": [924 "X = df.comment_text\n",925 "y = df.iloc[:,2:].values"926 ],927 "id": "2e64c456"928 },929 {930 "cell_type": "code",931 "execution_count": null,932 "metadata": {933 "id": "c924ed65"934 },935 "outputs": [],936 "source": [937 "# number of words in vocab\n",938 "MAX_VOCAB = 200000"939 ],940 "id": "c924ed65"941 },942 {943 "cell_type": "code",944 "execution_count": null,945 "metadata": {946 "id": "d9e74b26"947 },948 "outputs": [],949 "source": [950 "vectorizer = TextVectorization(max_tokens=MAX_VOCAB, \n",951 " output_sequence_length=1800, \n",952 " output_mode='int')"953 ],954 "id": "d9e74b26"955 },956 {957 "cell_type": "code",958 "execution_count": null,959 "metadata": {960 "id": "b89a019a"961 },962 "outputs": [],963 "source": [964 "vectorizer.adapt(X.values)"965 ],966 "id": "b89a019a"967 },968 {969 "cell_type": "code",970 "execution_count": null,971 "metadata": {972 "colab": {973 "base_uri": "https://localhost:8080/"974 },975 "id": "832c78b5",976 "outputId": "c5d8489f-1b6e-4bcc-e4e8-42359d85c4ce"977 },978 "outputs": [979 {980 "output_type": "execute_result",981 "data": {982 "text/plain": [983 "<tf.Tensor: shape=(6,), dtype=int64, numpy=array([288, 263, 191, 3, 14, 463])>"984 ]985 },986 "metadata": {},987 "execution_count": 14988 }989 ],990 "source": [991 "vectorizer('Hello world, welcome to this project')[:6]"992 ],993 "id": "832c78b5"994 },995 {996 "cell_type": "code",997 "execution_count": null,998 "metadata": {999 "id": "d90fea8a"1000 },1001 "outputs": [],1002 "source": [1003 "processed_text = vectorizer(X.values)"1004 ],1005 "id": "d90fea8a"1006 },1007 {1008 "cell_type": "code",1009 "execution_count": null,1010 "metadata": {1011 "colab": {1012 "base_uri": "https://localhost:8080/"1013 },1014 "id": "9891f1b3",1015 "outputId": "16715f82-cc03-4bc9-bc4c-0d964111d0d3"1016 },1017 "outputs": [1018 {1019 "output_type": "execute_result",1020 "data": {1021 "text/plain": [1022 "<tf.Tensor: shape=(159571, 1800), dtype=int64, numpy=\n",1023 "array([[ 645, 76, 2, ..., 0, 0, 0],\n",1024 " [ 1, 54, 2489, ..., 0, 0, 0],\n",1025 " [ 425, 441, 70, ..., 0, 0, 0],\n",1026 " ...,\n",1027 " [32445, 7392, 383, ..., 0, 0, 0],\n",1028 " [ 5, 12, 534, ..., 0, 0, 0],\n",1029 " [ 5, 8, 130, ..., 0, 0, 0]])>"1030 ]1031 },1032 "metadata": {},1033 "execution_count": 161034 }1035 ],1036 "source": [1037 "processed_text"1038 ],1039 "id": "9891f1b3"1040 },1041 {1042 "cell_type": "code",1043 "execution_count": null,1044 "metadata": {1045 "id": "9176a3c0"1046 },1047 "outputs": [],1048 "source": [1049 "# MCSHBAP - map, cache, shuffle, batch, prefetch\n",1050 "# from_tensor_slices OR list_file\n",1051 "data = tf.data.Dataset.from_tensor_slices((processed_text, y))\n",1052 "data = data.cache()\n",1053 "data = data.shuffle(160000)\n",1054 "data = data.batch(16)\n",1055 "data = data.prefetch(8) # prevent bottleneck"1056 ],1057 "id": "9176a3c0"1058 },1059 {1060 "cell_type": "code",1061 "execution_count": null,1062 "metadata": {1063 "id": "042126d5"1064 },1065 "outputs": [],1066 "source": [1067 "batch_X, batch_y = data.as_numpy_iterator().next()"1068 ],1069 "id": "042126d5"1070 },1071 {1072 "cell_type": "code",1073 "execution_count": null,1074 "metadata": {1075 "colab": {1076 "base_uri": "https://localhost:8080/"1077 },1078 "id": "5b73aea2",1079 "outputId": "be6a586c-2d6e-459a-8748-ae4b4ec03125"1080 },1081 "outputs": [1082 {1083 "output_type": "execute_result",1084 "data": {1085 "text/plain": [1086 "(16, 1800)"1087 ]1088 },1089 "metadata": {},1090 "execution_count": 191091 }1092 ],1093 "source": [1094 "batch_X.shape"1095 ],1096 "id": "5b73aea2"1097 },1098 {1099 "cell_type": "code",1100 "execution_count": null,1101 "metadata": {1102 "id": "8286ce71"1103 },1104 "outputs": [],1105 "source": [1106 "train = data.take(int(len(data) * .7))\n",1107 "val = data.skip(int(len(data) * .7)).take(int(len(data)*.2))\n",1108 "test = data.take(int(len(data) * .9)).take(int(len(data)*.1))"1109 ],1110 "id": "8286ce71"1111 },1112 {1113 "cell_type": "code",1114 "execution_count": null,1115 "metadata": {1116 "colab": {1117 "base_uri": "https://localhost:8080/"1118 },1119 "id": "f06e8067",1120 "outputId": "8dadd560-5bfb-4d58-8301-d9f56f30a0b0"1121 },1122 "outputs": [1123 {1124 "output_type": "execute_result",1125 "data": {1126 "text/plain": [1127 "6981"1128 ]1129 },1130 "metadata": {},1131 "execution_count": 211132 }1133 ],1134 "source": [1135 "len(train)"1136 ],1137 "id": "f06e8067"1138 },1139 {1140 "cell_type": "code",1141 "execution_count": null,1142 "metadata": {1143 "colab": {1144 "base_uri": "https://localhost:8080/"1145 },1146 "id": "74d5fb4e",1147 "outputId": "7ddc0d55-360e-4283-a955-ce3dab49fc07"1148 },1149 "outputs": [1150 {1151 "output_type": "execute_result",1152 "data": {1153 "text/plain": [1154 "(array([[ 5495, 51, 29, ..., 0, 0, 0],\n",1155 " [ 33, 7, 69, ..., 0, 0, 0],\n",1156 " [ 24, 1805, 2256, ..., 0, 0, 0],\n",1157 " ...,\n",1158 " [ 46, 1377, 31, ..., 0, 0, 0],\n",1159 " [ 4354, 41514, 8, ..., 0, 0, 0],\n",1160 " [ 215, 8, 477, ..., 0, 0, 0]]),\n",1161 " array([[0, 0, 0, 0, 0, 0],\n",1162 " [0, 0, 0, 0, 0, 0],\n",1163 " [0, 0, 0, 0, 0, 0],\n",1164 " [0, 0, 0, 0, 0, 0],\n",1165 " [0, 0, 0, 0, 0, 0],\n",1166 " [0, 0, 0, 0, 0, 0],\n",1167 " [0, 0, 0, 0, 0, 0],\n",1168 " [0, 0, 0, 0, 0, 0],\n",1169 " [0, 0, 0, 0, 0, 0],\n",1170 " [0, 0, 0, 0, 0, 0],\n",1171 " [0, 0, 0, 0, 0, 0],\n",1172 " [0, 0, 0, 0, 0, 0],\n",1173 " [0, 0, 0, 0, 0, 0],\n",1174 " [0, 0, 0, 0, 0, 0],\n",1175 " [0, 0, 0, 0, 0, 0],\n",1176 " [0, 0, 0, 0, 0, 0]]))"1177 ]1178 },1179 "metadata": {},1180 "execution_count": 221181 }1182 ],1183 "source": [1184 "train.as_numpy_iterator().next()"1185 ],1186 "id": "74d5fb4e"1187 },1188 {1189 "cell_type": "markdown",1190 "metadata": {1191 "id": "-8f_Bi-OAc03"1192 },1193 "source": [1194 "# 3. Buiding model"1195 ],1196 "id": "-8f_Bi-OAc03"1197 },1198 {1199 "cell_type": "code",1200 "execution_count": null,