{"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-07-04T19:38:26.137757Z","iopub.execute_input":"2022-07-04T19:38:26.138780Z","iopub.status.idle":"2022-07-04T19:38:26.149826Z","shell.execute_reply.started":"2022-07-04T19:38:26.138725Z","shell.execute_reply":"2022-07-04T19:38:26.148601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nz= zipfile.ZipFile('/kaggle/input/jigsaw-toxic-comment-classification-challenge/test.csv.zip')\nz.extractall()\n\nz= zipfile.ZipFile('/kaggle/input/jigsaw-toxic-comment-classification-challenge/train.csv.zip')\nz.extractall()\n\nz= zipfile.ZipFile('/kaggle/input/jigsaw-toxic-comment-classification-challenge/test_labels.csv.zip')\nz.extractall()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:26.220195Z","iopub.execute_input":"2022-07-04T19:38:26.220854Z","iopub.status.idle":"2022-07-04T19:38:27.405808Z","shell.execute_reply.started":"2022-07-04T19:38:26.220817Z","shell.execute_reply":"2022-07-04T19:38:27.404684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom tensorflow.keras.preprocessing.text import Tokenizer\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:27.407826Z","iopub.execute_input":"2022-07-04T19:38:27.408484Z","iopub.status.idle":"2022-07-04T19:38:27.414018Z","shell.execute_reply.started":"2022-07-04T19:38:27.408443Z","shell.execute_reply":"2022-07-04T19:38:27.412973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"train.csv\")\ntest_df = pd.read_csv(\"test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:27.416796Z","iopub.execute_input":"2022-07-04T19:38:27.417057Z","iopub.status.idle":"2022-07-04T19:38:28.983989Z","shell.execute_reply.started":"2022-07-04T19:38:27.417034Z","shell.execute_reply":"2022-07-04T19:38:28.983015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:28.986848Z","iopub.execute_input":"2022-07-04T19:38:28.987237Z","iopub.status.idle":"2022-07-04T19:38:28.997247Z","shell.execute_reply.started":"2022-07-04T19:38:28.987201Z","shell.execute_reply":"2022-07-04T19:38:28.996191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:28.999021Z","iopub.execute_input":"2022-07-04T19:38:28.999395Z","iopub.status.idle":"2022-07-04T19:38:29.014996Z","shell.execute_reply.started":"2022-07-04T19:38:28.999358Z","shell.execute_reply":"2022-07-04T19:38:29.013926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenizer = Tokenizer(\n    num_words=5000,\n    filters='!\"#$%&()*+,-./:;<=>?@[\\\\]^_`{|}~\\t\\n',\n    lower=True,\n    split=' ',\n    char_level=False,\n    oov_token=0 #token 0 si no está en el vocabulario\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:29.016387Z","iopub.execute_input":"2022-07-04T19:38:29.016808Z","iopub.status.idle":"2022-07-04T19:38:29.045540Z","shell.execute_reply.started":"2022-07-04T19:38:29.016774Z","shell.execute_reply":"2022-07-04T19:38:29.044555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenizer.fit_on_texts(\n    train_df[\"comment_text\"].to_list()\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:29.048717Z","iopub.execute_input":"2022-07-04T19:38:29.049372Z","iopub.status.idle":"2022-07-04T19:38:40.104799Z","shell.execute_reply.started":"2022-07-04T19:38:29.049334Z","shell.execute_reply":"2022-07-04T19:38:40.103780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_sequences = tokenizer.texts_to_sequences(train_df[\"comment_text\"].to_list())\ntest_sequences = tokenizer.texts_to_sequences(test_df[\"comment_text\"].to_list())","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:40.106260Z","iopub.execute_input":"2022-07-04T19:38:40.106734Z","iopub.status.idle":"2022-07-04T19:38:57.004183Z","shell.execute_reply.started":"2022-07-04T19:38:40.106698Z","shell.execute_reply":"2022-07-04T19:38:57.003136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_sequences[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:57.006603Z","iopub.execute_input":"2022-07-04T19:38:57.007199Z","iopub.status.idle":"2022-07-04T19:38:57.014482Z","shell.execute_reply.started":"2022-07-04T19:38:57.007160Z","shell.execute_reply":"2022-07-04T19:38:57.013495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tok_config = tokenizer.get_config()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:57.019862Z","iopub.execute_input":"2022-07-04T19:38:57.020619Z","iopub.status.idle":"2022-07-04T19:38:58.658279Z","shell.execute_reply.started":"2022-07-04T19:38:57.020534Z","shell.execute_reply":"2022-07-04T19:38:58.657291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tok_config.keys()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:58.659583Z","iopub.execute_input":"2022-07-04T19:38:58.660628Z","iopub.status.idle":"2022-07-04T19:38:58.667259Z","shell.execute_reply.started":"2022-07-04T19:38:58.660587Z","shell.execute_reply":"2022-07-04T19:38:58.666339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tok_config[\"index_word\"][:100]","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:58.668493Z","iopub.execute_input":"2022-07-04T19:38:58.669550Z","iopub.status.idle":"2022-07-04T19:38:58.677639Z","shell.execute_reply.started":"2022-07-04T19:38:58.669513Z","shell.execute_reply":"2022-07-04T19:38:58.676674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tok_config[\"word_index\"][:100]","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:58.678840Z","iopub.execute_input":"2022-07-04T19:38:58.679687Z","iopub.status.idle":"2022-07-04T19:38:58.689302Z","shell.execute_reply.started":"2022-07-04T19:38:58.679636Z","shell.execute_reply":"2022-07-04T19:38:58.688137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lengths = [len(texto) for texto in train_sequences]","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:58.691026Z","iopub.execute_input":"2022-07-04T19:38:58.691534Z","iopub.status.idle":"2022-07-04T19:38:58.709347Z","shell.execute_reply.started":"2022-07-04T19:38:58.691361Z","shell.execute_reply":"2022-07-04T19:38:58.708225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import Counter\nfrom matplotlib import pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:58.710849Z","iopub.execute_input":"2022-07-04T19:38:58.711272Z","iopub.status.idle":"2022-07-04T19:38:58.720206Z","shell.execute_reply.started":"2022-07-04T19:38:58.711234Z","shell.execute_reply":"2022-07-04T19:38:58.719264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(18,6))\nplt.hist(lengths, bins=500)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:38:58.723877Z","iopub.execute_input":"2022-07-04T19:38:58.724150Z","iopub.status.idle":"2022-07-04T19:39:00.315671Z","shell.execute_reply.started":"2022-07-04T19:38:58.724126Z","shell.execute_reply":"2022-07-04T19:39:00.314682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_sequences_padded = pad_sequences(\n    train_sequences,\n    maxlen=200,\n    dtype='int32',\n    padding='pre',\n    truncating='pre',\n    value=0.0\n)\n\ntest_sequences_padded = pad_sequences(\n    test_sequences,\n    maxlen=200,\n    dtype='int32',\n    padding='pre',\n    truncating='pre',\n    value=0.0\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:39:00.317338Z","iopub.execute_input":"2022-07-04T19:39:00.317706Z","iopub.status.idle":"2022-07-04T19:39:02.686000Z","shell.execute_reply.started":"2022-07-04T19:39:00.317668Z","shell.execute_reply":"2022-07-04T19:39:02.684997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lengths = [len(texto) for texto in train_sequences_padded]\nplt.figure(figsize=(18,6))\nplt.hist(lengths, bins=500)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:39:02.687570Z","iopub.execute_input":"2022-07-04T19:39:02.687961Z","iopub.status.idle":"2022-07-04T19:39:04.337738Z","shell.execute_reply.started":"2022-07-04T19:39:02.687924Z","shell.execute_reply":"2022-07-04T19:39:04.336612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_sequences_padded[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:39:04.339278Z","iopub.execute_input":"2022-07-04T19:39:04.339624Z","iopub.status.idle":"2022-07-04T19:39:04.347010Z","shell.execute_reply.started":"2022-07-04T19:39:04.339589Z","shell.execute_reply":"2022-07-04T19:39:04.345918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import LSTM, Dense, Embedding\nfrom tensorflow.keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:39:04.348597Z","iopub.execute_input":"2022-07-04T19:39:04.348972Z","iopub.status.idle":"2022-07-04T19:39:04.357980Z","shell.execute_reply.started":"2022-07-04T19:39:04.348932Z","shell.execute_reply":"2022-07-04T19:39:04.356766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Embedding(5001, 64, mask_zero=True, input_length=200))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:39:04.359777Z","iopub.execute_input":"2022-07-04T19:39:04.360467Z","iopub.status.idle":"2022-07-04T19:39:04.383307Z","shell.execute_reply.started":"2022-07-04T19:39:04.360429Z","shell.execute_reply":"2022-07-04T19:39:04.382358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(LSTM(100, return_sequences=True, unroll=True))\nmodel.add(LSTM(100, unroll=True))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:39:04.384628Z","iopub.execute_input":"2022-07-04T19:39:04.385096Z","iopub.status.idle":"2022-07-04T19:39:09.182122Z","shell.execute_reply.started":"2022-07-04T19:39:04.385058Z","shell.execute_reply":"2022-07-04T19:39:09.180842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Dense(100, activation=\"relu\"))\nmodel.add(Dense(6, activation=\"sigmoid\"))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:39:09.183854Z","iopub.execute_input":"2022-07-04T19:39:09.184176Z","iopub.status.idle":"2022-07-04T19:39:09.210067Z","shell.execute_reply.started":"2022-07-04T19:39:09.184148Z","shell.execute_reply":"2022-07-04T19:39:09.209033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.metrics import AUC\n\nauc = AUC(\n    num_thresholds=200,\n    curve='ROC',\n    summation_method='interpolation',\n    name=None,\n    dtype=None,\n    thresholds=None,\n    multi_label=False,\n    num_labels=None,\n    label_weights=None,\n    from_logits=False\n)\nmodel.compile(loss=\"binary_crossentropy\", optimizer=\"RMSProp\", metrics=[auc, \"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:39:09.211506Z","iopub.execute_input":"2022-07-04T19:39:09.211879Z","iopub.status.idle":"2022-07-04T19:39:09.228443Z","shell.execute_reply.started":"2022-07-04T19:39:09.211843Z","shell.execute_reply":"2022-07-04T19:39:09.227612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train_df.iloc[:,2:].values","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:39:09.229634Z","iopub.execute_input":"2022-07-04T19:39:09.230060Z","iopub.status.idle":"2022-07-04T19:39:09.237366Z","shell.execute_reply.started":"2022-07-04T19:39:09.230023Z","shell.execute_reply":"2022-07-04T19:39:09.236201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"es = EarlyStopping(monitor=\"val_auc\", patience=5,restore_best_weights=True )","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:39:09.238944Z","iopub.execute_input":"2022-07-04T19:39:09.239670Z","iopub.status.idle":"2022-07-04T19:39:09.247433Z","shell.execute_reply.started":"2022-07-04T19:39:09.239617Z","shell.execute_reply":"2022-07-04T19:39:09.246336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_sequences_padded, y_train, batch_size=128, epochs=10, validation_split=0.2, callbacks=[es])","metadata":{"execution":{"iopub.status.busy":"2022-07-04T19:39:09.249044Z","iopub.execute_input":"2022-07-04T19:39:09.249436Z","iopub.status.idle":"2022-07-04T20:08:13.267887Z","shell.execute_reply.started":"2022-07-04T19:39:09.249402Z","shell.execute_reply":"2022-07-04T20:08:13.267021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = model.predict(test_sequences_padded, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:08:13.272840Z","iopub.execute_input":"2022-07-04T20:08:13.273096Z","iopub.status.idle":"2022-07-04T20:11:44.685626Z","shell.execute_reply.started":"2022-07-04T20:08:13.273072Z","shell.execute_reply":"2022-07-04T20:11:44.684666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:11:44.688132Z","iopub.execute_input":"2022-07-04T20:11:44.689063Z","iopub.status.idle":"2022-07-04T20:11:44.695622Z","shell.execute_reply.started":"2022-07-04T20:11:44.689021Z","shell.execute_reply":"2022-07-04T20:11:44.694680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels_df = pd.read_csv(\"test_labels.csv\")\ntest_labels_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:11:44.697305Z","iopub.execute_input":"2022-07-04T20:11:44.698891Z","iopub.status.idle":"2022-07-04T20:11:44.856618Z","shell.execute_reply.started":"2022-07-04T20:11:44.698853Z","shell.execute_reply":"2022-07-04T20:11:44.855685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[[\"toxic\", \"severe_toxic\", \"obscene\", \"threat\", \"insult\", \"identity_hate\"]] = y_test","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:11:44.857948Z","iopub.execute_input":"2022-07-04T20:11:44.858494Z","iopub.status.idle":"2022-07-04T20:11:44.871579Z","shell.execute_reply.started":"2022-07-04T20:11:44.858452Z","shell.execute_reply":"2022-07-04T20:11:44.870748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:13:38.669085Z","iopub.execute_input":"2022-07-04T20:13:38.669511Z","iopub.status.idle":"2022-07-04T20:13:38.687032Z","shell.execute_reply.started":"2022-07-04T20:13:38.669476Z","shell.execute_reply":"2022-07-04T20:13:38.685946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.drop(columns=\"comment_text\").to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T20:13:42.136841Z","iopub.execute_input":"2022-07-04T20:13:42.137366Z","iopub.status.idle":"2022-07-04T20:13:44.307353Z","shell.execute_reply.started":"2022-07-04T20:13:42.137334Z","shell.execute_reply":"2022-07-04T20:13:44.306228Z"},"trusted":true},"execution_count":null,"outputs":[]}]}