{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.model_selection import train_test_split\nfrom keras.preprocessing.text import Tokenizer\nfrom keras.layers import Dropout, LSTM, Dense, Flatten\nfrom keras.preprocessing.sequence import pad_sequences\nfrom keras.models import Sequential\nfrom keras.layers.embeddings import Embedding\nimport matplotlib.pyplot as plt\n\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# load train and test data\ntrain_df = pd.read_csv(\"../input/train.csv\")\ntest_df = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"80aa49a49368e94fbd4e67963b21e44cd095877d"},"cell_type":"code","source":"# First 5 data\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"655acc27f223ee5bc241604f2bbf771e78b6faac"},"cell_type":"code","source":"# First 5 data\ntest_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"589d16298a28b9a7c337f3b23685af11c00327ef"},"cell_type":"code","source":"# Features and target\nX = train_df.drop(\"target\", axis = 1)\ny = train_df[\"target\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c984cac6b2b13f733f12c44c58706dbc5c4b111f"},"cell_type":"code","source":"# Split data into train and test \nx_train, x_val, y_train, y_val = train_test_split(X, y, test_size = 0.2, shuffle = True, random_state = 34)\nx_train.shape, y_train.shape, x_val.shape, y_val.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f0b6776c880ae71dafbc76ca14c65dae237dfe94"},"cell_type":"code","source":"# Fill nan/na value with 0\nx_train = x_train[\"question_text\"].fillna(\"_NA_\").values\nx_val = x_val[\"question_text\"].fillna(\"_NA_\").values\nx_test = test_df[\"question_text\"].fillna(\"_NA_\").values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3d322f28f7e16bc9cb482321c03eb12ec933d7d8"},"cell_type":"code","source":"# Attribute that we will use in function\nmax_words = 100000\nmax_len = 200\n# it takes lot of time that's why only for 2 epoch\nepoch = 1\nembedding_vecor_length = 32\nbatch_size = 1024\n# Tokenize text and select top 100k features\ntokenizer = Tokenizer(num_words=max_words)\ntokenizer.fit_on_texts(list(x_train))\nx_train = tokenizer.texts_to_sequences(x_train)\nx_val = tokenizer.texts_to_sequences(x_val)\nx_test = tokenizer.texts_to_sequences(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b1aa4940ed6789b178dd22e27a9b3809a04a1a2b"},"cell_type":"code","source":"# Padding \nx_trn_seq = pad_sequences(x_train, maxlen = max_len)\nx_val_seq = pad_sequences(x_val, maxlen = max_len)\nx_test = pad_sequences(x_test, maxlen = max_len)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8d3f149af820f8493cbe25321de0f74868f35ea"},"cell_type":"code","source":"# Build sequential model with 2 LSTM layer follwed by dropout and flatten\nmodel = Sequential()\nmodel.add(Embedding(max_words, embedding_vecor_length, input_length = max_len))\nmodel.add(LSTM(100, return_sequences = True))\nmodel.add(Dropout(0.5))\n\nmodel.add(LSTM(100, return_sequences = True))\nmodel.add(Dropout(0.5))\nmodel.add(Flatten())\nmodel.add(Dense(1, activation = \"sigmoid\"))\nmodel.compile(loss = \"binary_crossentropy\", optimizer = \"rmsprop\", metrics = [\"accuracy\"])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3af71b3f03cc7c8951f62a09a770072acc429a16"},"cell_type":"code","source":"# Train model that gives you history about train loss and validation loss\nhistory = model.fit(x_trn_seq, y_train, batch_size, epochs = epoch, validation_data = (x_val_seq, y_val))\n# Accuracy on validation data\nscore = model.evaluate(x_val_seq, y_val, verbose = 0)\nprint(\"Validation Score:\", score[0])\nprint(\"Validation Accuracy\", score[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"185b6feba0c1946c658065e921e75ba4d9a4f556"},"cell_type":"code","source":"# Due to time constraints, commented it\n'''\n# Plot validataion and train loss\nx = range(1, epoch + 1)\nval_loss = history.history[\"val_loss\"]\ntrain_loss = history.history[\"loss\"]\nplt.plot(x, val_loss, \"b\", label = \"Validation loss\")\nplt.plot(x, train_loss, \"r\", label = \"Train loss\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Categorical Crossentropy loss\")\nplt.legend()\nplt.show()\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"acac2aed1bba1ed4fc95863669982ad842430f74"},"cell_type":"code","source":"# predict results\npred = model.predict(x_test)\nsample_submission = pd.DataFrame({\"qid\": test_df[\"qid\"].values})\nsample_submission[\"prediction\"] = pred\nsample_submission.to_csv(\"submission.csv\", index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ea5beb113d4ffbeff71c8082483dce2307028564"},"cell_type":"markdown","source":"NOTE: - Will update it soon."}],"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}