{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport keras\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow_hub as hub\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelBinarizer, LabelEncoder\nfrom sklearn.metrics  import classification_report, confusion_matrix\n\nimport os\nos.listdir(\"../input\")","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/train.csv', sep=',')\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"935d2e9f5cb8fe2a8d407fa3749ab654029da4c0"},"cell_type":"code","source":"test = pd.read_csv('../input/test.csv')\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85f759484b13c1c735af13074c35f656699e5d57"},"cell_type":"code","source":"sns.countplot(train['target']);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3a1142e9d2f82d60ec78eb5ecd5013d854451d11"},"cell_type":"markdown","source":"## Uniform data"},{"metadata":{"trusted":true,"_uuid":"0177b27e12fe891a9344f04e9b5ae7bafaa76765"},"cell_type":"code","source":"t1 = train[train['target']==1]\nt0 = train[train['target']==0][:99999]\n\ndel train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9fd4329d98af78de161bf66fa75816cd89c20aa2"},"cell_type":"code","source":"train = t1.append(t0)\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2f6fd2ecbc5b4e85c94952e1917429de88b1c7f4"},"cell_type":"code","source":"train['length']  = train['question_text'].map(len)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b574a18cf98d6c3b2071443f12a0bc6382956d37"},"cell_type":"code","source":"train['length'].describe()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"55f72b9b83f4b61a7f76df4394ff45f57024619f"},"cell_type":"markdown","source":"## Data transformation"},{"metadata":{"trusted":true,"_uuid":"08a260af926a8ac43ebf61094c896eca92ff1207"},"cell_type":"code","source":"import nltk, re, string\nfrom keras.preprocessing import sequence, text\ndef text_preprocess(text):\n    text = text.lower()\n    text = re.sub('[!\"#$%&\\'()*+,-./:;<=>?@[\\\\]^_`{|}~]', ' ', text)\n    return text","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"596bb431a8bee241f589b77a7aacc9bce70389c5"},"cell_type":"code","source":"train['clean_text'] = train['question_text'].map(text_preprocess)\ntest['clean_text'] = test['question_text'].map(text_preprocess)\n\ntrain['clean_text'].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"29baa65928ab58c604a046cf9b671f691ec92a5a"},"cell_type":"code","source":"max_features = 2000\ntokenizer = text.Tokenizer(num_words=max_features, lower=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"05a71588483a36e421f991b23530e005819f91eb"},"cell_type":"code","source":"train['clean_text'][:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"442f674ac5917a2e9a1435ea0982fbdb1bd0c8f0"},"cell_type":"code","source":"tokenizer.fit_on_texts(train['clean_text'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f54a526355ce472899abf2996a941a67cd625880"},"cell_type":"code","source":"X_train = tokenizer.texts_to_sequences(train['clean_text'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7c17a78a782f12500b33412d6dc10d85e6b25c87"},"cell_type":"code","source":"X_test = tokenizer.texts_to_sequences(test['clean_text'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5f1ba05e820617676cc67a8460389615f9f5dcba"},"cell_type":"markdown","source":"##  Model creation"},{"metadata":{"trusted":true,"_uuid":"c62e356b24062f9a91c61765e5b945bea14f0b6d"},"cell_type":"code","source":"def encode(x):\n    return keras.utils.to_categorical(x)\n\ndef decode(x):\n    return np.argmax(x, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"81c5d2a3f9a3cb9e4aa9b520a3cadf0ff50948fa"},"cell_type":"code","source":"x_train = list(X_train)\ny_train = list(train['target'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b342819217119eafa00f10bfa3cdda4d75d16cdf"},"cell_type":"code","source":"y_train = encode(y_train)\ny_train","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"452f351ea2ca96931cfe492322d4284d3730b990"},"cell_type":"markdown","source":"## Model Training"},{"metadata":{"trusted":true,"_uuid":"8c06a6298deb66156ca0efd220b2ff4ff2bfdb19"},"cell_type":"code","source":"from keras.models import Sequential, Model\nfrom keras.layers import Dense, Activation, Dropout, Lambda, Input, Embedding, Bidirectional, LSTM\nimport keras.backend as K\n\nfrom keras.preprocessing.text import text_to_word_sequence\nimport tensorflow as tf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"caedbec08b3f7cbc472efc3ca0bd33f023b9676e"},"cell_type":"code","source":"max_features = 2196","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9a3a9cd6027133a43456d8546534f51362077734"},"cell_type":"code","source":"model = Sequential()\nhidden_size = 128\n# model.add(Dense(activation='relu'))\nmodel.add(Embedding(max_features, hidden_size))\nmodel.add(Bidirectional(LSTM(hidden_size, activation='elu', dropout=0.4, recurrent_dropout=0.2)))\nmodel.add(Dense(2, activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"49fbcb1489a64505d2e9e6e881ca729ee9aabba8"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9530cb4632ab9f6e9cb4ade7389e6ade3ba90f12"},"cell_type":"code","source":"model.compile(loss='binary_crossentropy', optimizer='adam',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6ee40df55748798e25c5f4745cf3deaf8ac4968e"},"cell_type":"code","source":"x_train = sequence.pad_sequences(X_train, maxlen=71, padding='post', truncating='post')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a383c769b904ff80fa2101f6d7dac83fed6db041"},"cell_type":"code","source":"model.fit(x_train, y_train, batch_size=128, epochs=10, shuffle=True, validation_split=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d47f2d24b3fd6b141d3b4c9ebe51888b942b606f"},"cell_type":"code","source":"score = model.evaluate(x=x_train[25:10025], y=y_train[25:10025], batch_size=16)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c082e46c4d52a679f9f9b849defa00fbfa2f0ab7"},"cell_type":"code","source":"score","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5436ac599704d432a0ac081dbb679fc02f3c46e4"},"cell_type":"markdown","source":"## Metrics Score"},{"metadata":{"trusted":true,"_uuid":"bf7157bcad7aa1a75065cf3333431fb8279752ef"},"cell_type":"code","source":"# y_pr = decode(y_preds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"de9f5fd8191567abd520f6e147c5ad6368d4f84e"},"cell_type":"code","source":"# c = confusion_matrix(np.argmax(y_train, axis=1), y_pr)\n# sns.heatmap(c, annot=True);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8d954235b3f6a0fc09265ec195075334c6fbfef7"},"cell_type":"markdown","source":"## Model Prediction Test"},{"metadata":{"trusted":true,"_uuid":"5ed4976860234e86448a5f7903a247a5f61d891c"},"cell_type":"code","source":"x_test = list(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"10b231e3d0de587e0b7094dd251095d6778aa01b"},"cell_type":"code","source":"x_test = sequence.pad_sequences(x_test, maxlen=71, padding='post', truncating='post')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e6d153f1e581a4866e7659d351a1f2b259ec4f5"},"cell_type":"code","source":"test_preds = model.predict(x_test, batch_size=64)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0a662b1a58f1c511429bf5d5b43a4fd645b4e83e"},"cell_type":"code","source":"test_preds = np.argmax(test_preds, axis=1)\ntest_preds","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7619996efab2b8d2c73943df0ae29c3233f26662"},"cell_type":"markdown","source":"## Submission "},{"metadata":{"trusted":true,"_uuid":"f56e521c23ee3149ed6655d40378fa7444e22d4b"},"cell_type":"code","source":"sub = pd.read_csv('../input/sample_submission.csv')\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c12b98446185c6e7ea48a268b799fddeaeb82727"},"cell_type":"code","source":"sub['prediction'] = test_preds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"558b52440896d73042e450d76ca8c841bdf496bd"},"cell_type":"code","source":"sns.countplot(test_preds);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"527a6f3b90fb98001d43b69c4febf0417c90c22c"},"cell_type":"code","source":"sub.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}