{"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\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\nimport tensorflow as tf\n\n\nfrom tensorflow.keras.preprocessing.text import Tokenizer\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences\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":"train = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')\n''' for checking if there are any missing values in the dataset:\nfor col in train:\n    print (train[col].isnull().sum())\n    \nThere are no missing values in the dataset as seen\n    \n'''\n'''\n#number of insincere questions in our dataset\ninsin=len(train.loc[train['target']==1])\nsin=len(train.loc[train['target']==0])\ninsin/len(train)\n#So only 6 percent of the dataset comprises of insincere questions\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df, test_df = train_test_split(train, test_size=0.2)\ntr_sentences = train_df['question_text'].tolist()\ntr_labels = train_df['target'].tolist()\n\nte_sentences = test_df['question_text'].tolist()\nte_labels = test_df['target'].tolist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tokenizer = Tokenizer(num_words=95000, oov_token=\"<OOV>\")\ntokenizer.fit_on_texts(tr_sentences)\n\ntr_sequences = tokenizer.texts_to_sequences(tr_sentences)\ntr_padded = pad_sequences(tr_sequences,maxlen=100, padding = 'post', truncating='pre')\n\nte_sequences = tokenizer.texts_to_sequences(te_sentences)\nte_padded = pad_sequences(te_sequences, maxlen=100, padding='post', truncating = 'pre')\nprint('done')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.Embedding(95000, 300, input_length = 100),\n    tf.keras.layers.Bidirectional(tf.keras.layers.CuDNNLSTM(128, return_sequences=True)),\n    tf.keras.layers.GlobalAveragePooling1D(),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(64, activation='relu'),\n    tf.keras.layers.Dropout(0.2), \n    tf.keras.layers.Dense(32, activation = 'relu'), \n    tf.keras.layers.Dropout(0.2), \n    tf.keras.layers.Dense(1, activation='sigmoid')\n])\n\nmodel.compile(loss='binary_crossentropy',optimizer='adam', metrics=['accuracy'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(tr_padded, tr_labels, epochs=2, batch_size=512, validation_data=(te_padded, te_labels), verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Thanks to : https://www.kaggle.com/advaitsave/lstm-using-tensorflow-2-with-embeddings\nfrom sklearn import metrics\npred_test_y = model.predict([te_padded], batch_size=1024, verbose=1)\n\nopt_prob = None\nf1_max = 0\n\nfor thresh in np.arange(0.1, 0.501, 0.01):\n    thresh = np.round(thresh, 2)\n    f1 = metrics.f1_score(te_labels, (pred_test_y > thresh).astype(int))\n    print('F1 score at threshold {} is {}'.format(thresh, f1))\n    \n    if f1 > f1_max:\n        f1_max = f1\n        opt_prob = thresh\n        \nprint('Optimal probabilty threshold is {} for maximum F1 score {}'.format(opt_prob, f1_max))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we train the model on the entire dataset:"},{"metadata":{"trusted":true},"cell_type":"code","source":"sentences = train['question_text'].tolist()\nlabels = train['target'].tolist()\ntest_submission=test['question_text'].tolist()\n\ntokenizer_final = Tokenizer(num_words=95000, oov_token=\"<OOV>\")\ntokenizer_final.fit_on_texts(sentences)\n\nsequences = tokenizer.texts_to_sequences(sentences)\ntest_submission = tokenizer.texts_to_sequences(test_submission)\n\npadded = pad_sequences(sequences,maxlen=100, padding = 'post', truncating='pre')\ntest_submission = pad_sequences(test_submission, maxlen=100, padding = 'post', truncating='pre')\nprint('done')\n\nhistory = model.fit(padded, labels, epochs=2, batch_size=512, verbose=1)\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Submission of the prediction file:"},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\npred_submission = model.predict([test_submission], batch_size=1024, verbose=1)\npred_submission = (pred_submission > opt_prob).astype(int)\n\ndf_submission = pd.DataFrame({'qid': test['qid'].values})\ndf_submission['prediction'] = pred_submission\ndf_submission.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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}