{"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)\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":"from sklearn.model_selection import GridSearchCV,StratifiedKFold\nfrom keras.wrappers.scikit_learn import KerasClassifier\nfrom sklearn.model_selection import cross_val_score,train_test_split\nfrom scipy import stats\nfrom sklearn import metrics\n\nfrom keras.models import Sequential # initialize neural network library\nfrom keras.layers import Dense # build our layers library\nfrom keras.preprocessing.text import Tokenizer\n\nimport string,re\nfrom collections import Counter\nimport nltk\nfrom nltk.corpus import stopwords\n\n#spaCy based imports\nimport spacy\nfrom spacy.lang.en.stop_words import STOP_WORDS\nfrom spacy.lang.en import English","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"adc77e189bd86036cdf7672f0806c18f13c8e6c7"},"cell_type":"code","source":"import time\nfrom tqdm import tqdm\nimport math\nfrom keras.preprocessing.text import Tokenizer\nfrom keras.preprocessing.sequence import pad_sequences\nfrom keras.layers import Dense, Input, LSTM, Embedding, Dropout, Activation, CuDNNGRU, Conv1D,CuDNNLSTM\nfrom keras.layers import Bidirectional, GlobalMaxPool1D\nfrom keras.models import Model\nfrom keras import initializers, regularizers, constraints, optimizers, layers\nfrom sklearn import model_selection as ms\nfrom sklearn.model_selection import KFold,StratifiedKFold\nfrom sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.svm import LinearSVC\nfrom keras.layers import Input, Dense, Embedding, concatenate\nfrom keras.layers import CuDNNGRU, Bidirectional, GlobalAveragePooling1D, GlobalMaxPooling1D\nfrom keras.preprocessing import text, sequence\nfrom keras.layers import Reshape, Flatten, Concatenate, Dropout, SpatialDropout1D\nfrom keras.optimizers import Adam\nfrom keras.models import Model\nfrom keras import backend as K\nfrom keras.engine.topology import Layer\nfrom keras import initializers, regularizers, constraints, optimizers, layers\nfrom keras.layers import *\nfrom keras.callbacks import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4393bf6f917dcda674e331ad90a678044edc17d9"},"cell_type":"code","source":"df_train = pd.read_csv(\"../input/train.csv\")\ndf_test = pd.read_csv(\"../input/test.csv\")\n\nprint(\"train data shape --\",df_train.shape)\nprint(\"test data shape --\",df_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"688f6392c5d7340c4e8ce972fc8fd09dfc57d810"},"cell_type":"code","source":"df_train[\"question_text\"] = df_train[\"question_text\"].apply(lambda x: x.replace('.',' fullstop '))\ndf_train[\"question_text\"] = df_train[\"question_text\"].apply(lambda x: x.replace('?',' endofquestion '))\ndf_train[\"question_text\"] = df_train[\"question_text\"].apply(lambda x: x.replace(',',' comma '))\ndf_train[\"question_text\"] = df_train[\"question_text\"].apply(lambda x: x.replace('!',' exclamationmark '))\ndf_train[\"question_text\"] = df_train[\"question_text\"].apply(lambda x: x.replace('-',' hyphen '))\ndf_train[\"question_text\"] = df_train[\"question_text\"].apply(lambda x: x.replace('/',' backslash '))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"defc152cc23b31550299eb88e2f57d7f4077f97d"},"cell_type":"code","source":"df_train[\"question_text\"] = df_train[\"question_text\"].apply(lambda x: x.replace('fullstop','.'))\ndf_train[\"question_text\"] = df_train[\"question_text\"].apply(lambda x: x.replace('endofquestion','?'))\ndf_train[\"question_text\"] = df_train[\"question_text\"].apply(lambda x: x.replace('comma',','))\ndf_train[\"question_text\"] = df_train[\"question_text\"].apply(lambda x: x.replace('exclamationmark','!'))\ndf_train[\"question_text\"] = df_train[\"question_text\"].apply(lambda x: x.replace('hyphen','-'))\ndf_train[\"question_text\"] = df_train[\"question_text\"].apply(lambda x: x.replace('backslash','/'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7573bb7d98c0fdfb9f8cb0ff7d778538e8ede57"},"cell_type":"code","source":"df_test[\"question_text\"] = df_test[\"question_text\"].apply(lambda x: x.replace('.',' fullstop '))\ndf_test[\"question_text\"] = df_test[\"question_text\"].apply(lambda x: x.replace('?',' endofquestion '))\ndf_test[\"question_text\"] = df_test[\"question_text\"].apply(lambda x: x.replace(',',' comma '))\ndf_test[\"question_text\"] = df_test[\"question_text\"].apply(lambda x: x.replace('!',' exclamationmark '))\ndf_test[\"question_text\"] = df_test[\"question_text\"].apply(lambda x: x.replace('-',' hyphen '))\ndf_test[\"question_text\"] = df_test[\"question_text\"].apply(lambda x: x.replace('/',' backslash '))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"284ca1ecf0d40a24ca156408c1b633c790fb0fc0"},"cell_type":"code","source":"df_test[\"question_text\"] = df_test[\"question_text\"].apply(lambda x: x.replace('fullstop','.'))\ndf_test[\"question_text\"] = df_test[\"question_text\"].apply(lambda x: x.replace('endofquestion','?'))\ndf_test[\"question_text\"] = df_test[\"question_text\"].apply(lambda x: x.replace('comma',','))\ndf_test[\"question_text\"] = df_test[\"question_text\"].apply(lambda x: x.replace('exclamationmark','!'))\ndf_test[\"question_text\"] = df_test[\"question_text\"].apply(lambda x: x.replace('hyphen','-'))\ndf_test[\"question_text\"] = df_test[\"question_text\"].apply(lambda x: x.replace('backslash','/'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"55c9023eea534adb69728288ada53ac12d3dbb61"},"cell_type":"code","source":"df_combined = pd.concat([df_train,df_test],axis=0)\n\nprint(\"combined shape \",df_combined.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d820709a9400baa1efe531debd7dde6e8bb01b00"},"cell_type":"code","source":"questions = list(df_combined['question_text'].values)\nlength_of_sentences = []\nfor s in questions:\n    length_of_sentences.append(len(s.split()))\nprint(questions[:5])\nprint(\"\\nlength of sentences\",length_of_sentences[:5])\nprint(\"\\nstats\",stats.describe(length_of_sentences))\nprint(\"\\npercentile\",\"-->\",np.percentile(length_of_sentences,99.99))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f1577f82146f1d52e187146901ff4ace02a75125"},"cell_type":"code","source":"## some config values \nembed_size = 300 # how big is each word vector\nmax_features = 60000 # how many unique words to use (i.e num rows in embedding vector)\nmaxlen = 60 # max number of words in a question to use\n\ntotal_X = df_combined[\"question_text\"].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7dc4a79a27454904a3877ce6f26806facde8176"},"cell_type":"code","source":"tokenizer = Tokenizer(num_words=max_features,filters='\"#$%()+-:;<=>@[\\\\]^_`{|}~\\t\\n',)\n\ntokenizer.fit_on_texts(list(total_X))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0a9a07c43b4de8f92a76d74fad0dd24d971dc977"},"cell_type":"code","source":"WORDS = tokenizer.word_counts\nprint(len(WORDS))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bdb46d2d66a866c9df5370f2a3a0801e3db57d13"},"cell_type":"code","source":"train_X = df_train[\"question_text\"].values\ntest_X = df_test[\"question_text\"].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"10b0b0a7d82ab5475e3442db5c37ac01c4446e1b"},"cell_type":"code","source":"train_X = tokenizer.texts_to_sequences(train_X)\ntest_X = tokenizer.texts_to_sequences(test_X)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"13e25295bc71bdbf09e6f09a7608927ef4f8642e"},"cell_type":"code","source":"## Pad the sentences \ntrain_X = pad_sequences(train_X, maxlen=maxlen)\ntest_X = pad_sequences(test_X, maxlen=maxlen)\n\n## Get the target values\ntrain_y = df_train['target'].values","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8ddbb2ba436c7402b677551dfe1599d3bd2656f2"},"cell_type":"markdown","source":"Trying different Embeddings and compare the results on basis of val_loss."},{"metadata":{"trusted":true,"_uuid":"b2ea9d1a6c163b96b49b8ebd7bf1b3dfdcaf0d32"},"cell_type":"code","source":"def get_embeddings(embedtype):\n    if embedtype is \"glove\":\n        EMBEDDING_FILE = '../input/embeddings/glove.840B.300d/glove.840B.300d.txt'\n    elif embedtype is \"fastext\":\n            EMBEDDING_FILE = '../input/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec'\n    elif embedtype is \"paragram\":\n                EMBEDDING_FILE = '../input/embeddings/paragram_300_sl999/paragram_300_sl999.txt'\n    \n    def get_coefs(word,*arr): \n        return word, np.asarray(arr, dtype='float32')\n    \n    embeddings_index = dict(get_coefs(*o.split(\" \")) for o in open(EMBEDDING_FILE,encoding=\"latin\") if len(o)>100)\n\n    all_embs = np.stack(embeddings_index.values())\n    emb_mean,emb_std = all_embs.mean(), all_embs.std()\n    \n    vocab_size = all_embs.shape[0]\n    embed_size = all_embs.shape[1]\n    word_index = tokenizer.word_index\n    \n    print(\"embed size-->\",embed_size)\n    print(\"total words in embeddings-->\",vocab_size)\n    print(\"total words in data-->\",len(word_index))\n    \n    nb_words = min(max_features, len(word_index))\n    embedding_matrix = np.random.normal(emb_mean, emb_std, (nb_words, embed_size))\n    \n    count_common_words =0\n    for word, i in word_index.items():\n        if i >= max_features: continue\n        embedding_vector = embeddings_index.get(word)\n        if embedding_vector is not None:\n            embedding_matrix[i] = embedding_vector\n            count_common_words = count_common_words+1\n    print(count_common_words,\" common words fount in \",embedtype)\n    print(\"{0}% common words found in {1}\".format((count_common_words*100/len(word_index)),embedtype))\n    \n    return embedding_matrix\n                ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0d6b858b0d7916acf2c6f043a856c2e69f3e77c1"},"cell_type":"code","source":"embedding_glove = get_embeddings(embedtype=\"glove\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f4cecf607057a3fea0957cc0728d1e6b6ab74aa4"},"cell_type":"code","source":"embedding_paragram = get_embeddings(embedtype=\"paragram\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"488c39aa7f01bb4760aa1740bd4117460918f264"},"cell_type":"code","source":"mean_gl_par_embedding = np.mean([embedding_glove,embedding_paragram],axis=0)\nprint(\"mean glove paragram embedding shape--> \",mean_gl_par_embedding.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"74506fc55b7b72a8dcbe376bbd677e5fefe527f0"},"cell_type":"code","source":"\nclass Attention(Layer):\n    def __init__(self, step_dim,\n                 W_regularizer=None, b_regularizer=None,\n                 W_constraint=None, b_constraint=None,\n                 bias=True, **kwargs):\n        self.supports_masking = True\n        self.init = initializers.get('glorot_uniform')\n\n        self.W_regularizer = regularizers.get(W_regularizer)\n        self.b_regularizer = regularizers.get(b_regularizer)\n\n        self.W_constraint = constraints.get(W_constraint)\n        self.b_constraint = constraints.get(b_constraint)\n\n        self.bias = bias\n        self.step_dim = step_dim\n        self.features_dim = 0\n        super(Attention, self).__init__(**kwargs)\n\n    def build(self, input_shape):\n        assert len(input_shape) == 3\n\n        self.W = self.add_weight((input_shape[-1],),\n                                 initializer=self.init,\n                                 name='{}_W'.format(self.name),\n                                 regularizer=self.W_regularizer,\n                                 constraint=self.W_constraint)\n        self.features_dim = input_shape[-1]\n\n        if self.bias:\n            self.b = self.add_weight((input_shape[1],),\n                                     initializer='zero',\n                                     name='{}_b'.format(self.name),\n                                     regularizer=self.b_regularizer,\n                                     constraint=self.b_constraint)\n        else:\n            self.b = None\n\n        self.built = True\n\n    def compute_mask(self, input, input_mask=None):\n        return None\n\n    def call(self, x, mask=None):\n        features_dim = self.features_dim\n        step_dim = self.step_dim\n\n        eij = K.reshape(K.dot(K.reshape(x, (-1, features_dim)),\n                        K.reshape(self.W, (features_dim, 1))), (-1, step_dim))\n\n        if self.bias:\n            eij += self.b\n\n        eij = K.tanh(eij)\n\n        a = K.exp(eij)\n\n        if mask is not None:\n            a *= K.cast(mask, K.floatx())\n\n        a /= K.cast(K.sum(a, axis=1, keepdims=True) + K.epsilon(), K.floatx())\n\n        a = K.expand_dims(a)\n        weighted_input = x * a\n        return K.sum(weighted_input, axis=1)\n\n    def compute_output_shape(self, input_shape):\n        return input_shape[0],  self.features_dim","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db95bc2b3efa52a340dd32a3d41697f1c9bbb7df"},"cell_type":"code","source":"class CyclicLR(Callback):\n    \"\"\"This callback implements a cyclical learning rate policy (CLR).\n    The method cycles the learning rate between two boundaries with\n    some constant frequency, as detailed in this paper (https://arxiv.org/abs/1506.01186).\n    The amplitude of the cycle can be scaled on a per-iteration or \n    per-cycle basis.\n    This class has three built-in policies, as put forth in the paper.\n    \"triangular\":\n        A basic triangular cycle w/ no amplitude scaling.\n    \"triangular2\":\n        A basic triangular cycle that scales initial amplitude by half each cycle.\n    \"exp_range\":\n        A cycle that scales initial amplitude by gamma**(cycle iterations) at each \n        cycle iteration.\n    For more detail, please see paper.\n    \n    # Example\n        ```python\n            clr = CyclicLR(base_lr=0.001, max_lr=0.006,\n                                step_size=2000., mode='triangular')\n            model.fit(X_train, Y_train, callbacks=[clr])\n        ```\n    \n    Class also supports custom scaling functions:\n        ```python\n            clr_fn = lambda x: 0.5*(1+np.sin(x*np.pi/2.))\n            clr = CyclicLR(base_lr=0.001, max_lr=0.006,\n                                step_size=2000., scale_fn=clr_fn,\n                                scale_mode='cycle')\n            model.fit(X_train, Y_train, callbacks=[clr])\n        ```    \n    # Arguments\n        base_lr: initial learning rate which is the\n            lower boundary in the cycle.\n        max_lr: upper boundary in the cycle. Functionally,\n            it defines the cycle amplitude (max_lr - base_lr).\n            The lr at any cycle is the sum of base_lr\n            and some scaling of the amplitude; therefore \n            max_lr may not actually be reached depending on\n            scaling function.\n        step_size: number of training iterations per\n            half cycle. Authors suggest setting step_size\n            2-8 x training iterations in epoch.\n        mode: one of {triangular, triangular2, exp_range}.\n            Default 'triangular'.\n            Values correspond to policies detailed above.\n            If scale_fn is not None, this argument is ignored.\n        gamma: constant in 'exp_range' scaling function:\n            gamma**(cycle iterations)\n        scale_fn: Custom scaling policy defined by a single\n            argument lambda function, where \n            0 <= scale_fn(x) <= 1 for all x >= 0.\n            mode paramater is ignored \n        scale_mode: {'cycle', 'iterations'}.\n            Defines whether scale_fn is evaluated on \n            cycle number or cycle iterations (training\n            iterations since start of cycle). Default is 'cycle'.\n    \"\"\"\n\n    def __init__(self, base_lr=0.001, max_lr=0.006, step_size=2000., mode='triangular',\n                 gamma=1., scale_fn=None, scale_mode='cycle'):\n        super(CyclicLR, self).__init__()\n\n        self.base_lr = base_lr\n        self.max_lr = max_lr\n        self.step_size = step_size\n        self.mode = mode\n        self.gamma = gamma\n        if scale_fn == None:\n            if self.mode == 'triangular':\n                self.scale_fn = lambda x: 1.\n                self.scale_mode = 'cycle'\n            elif self.mode == 'triangular2':\n                self.scale_fn = lambda x: 1/(2.**(x-1))\n                self.scale_mode = 'cycle'\n            elif self.mode == 'exp_range':\n                self.scale_fn = lambda x: gamma**(x)\n                self.scale_mode = 'iterations'\n        else:\n            self.scale_fn = scale_fn\n            self.scale_mode = scale_mode\n        self.clr_iterations = 0.\n        self.trn_iterations = 0.\n        self.history = {}\n\n        self._reset()\n\n    def _reset(self, new_base_lr=None, new_max_lr=None,\n               new_step_size=None):\n        \"\"\"Resets cycle iterations.\n        Optional boundary/step size adjustment.\n        \"\"\"\n        if new_base_lr != None:\n            self.base_lr = new_base_lr\n        if new_max_lr != None:\n            self.max_lr = new_max_lr\n        if new_step_size != None:\n            self.step_size = new_step_size\n        self.clr_iterations = 0.\n        \n    def clr(self):\n        cycle = np.floor(1+self.clr_iterations/(2*self.step_size))\n        x = np.abs(self.clr_iterations/self.step_size - 2*cycle + 1)\n        if self.scale_mode == 'cycle':\n            return self.base_lr + (self.max_lr-self.base_lr)*np.maximum(0, (1-x))*self.scale_fn(cycle)\n        else:\n            return self.base_lr + (self.max_lr-self.base_lr)*np.maximum(0, (1-x))*self.scale_fn(self.clr_iterations)\n        \n    def on_train_begin(self, logs={}):\n        logs = logs or {}\n\n        if self.clr_iterations == 0:\n            K.set_value(self.model.optimizer.lr, self.base_lr)\n        else:\n            K.set_value(self.model.optimizer.lr, self.clr())        \n            \n    def on_batch_end(self, epoch, logs=None):\n        \n        logs = logs or {}\n        self.trn_iterations += 1\n        self.clr_iterations += 1\n\n        self.history.setdefault('lr', []).append(K.get_value(self.model.optimizer.lr))\n        self.history.setdefault('iterations', []).append(self.trn_iterations)\n\n        for k, v in logs.items():\n            self.history.setdefault(k, []).append(v)\n        \n        K.set_value(self.model.optimizer.lr, self.clr())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fcbc4488f711a9779d80fdcc76161f577c5146ee"},"cell_type":"code","source":"def f1(y_true, y_pred):\n    '''\n    metric from here \n    https://stackoverflow.com/questions/43547402/how-to-calculate-f1-macro-in-keras\n    '''\n    def recall(y_true, y_pred):\n        \"\"\"Recall metric.\n\n        Only computes a batch-wise average of recall.\n\n        Computes the recall, a metric for multi-label classification of\n        how many relevant items are selected.\n        \"\"\"\n        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n        possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n        recall = true_positives / (possible_positives + K.epsilon())\n        return recall\n\n    def precision(y_true, y_pred):\n        \"\"\"Precision metric.\n\n        Only computes a batch-wise average of precision.\n\n        Computes the precision, a metric for multi-label classification of\n        how many selected items are relevant.\n        \"\"\"\n        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n        predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n        precision = true_positives / (predicted_positives + K.epsilon())\n        return precision\n    precision = precision(y_true, y_pred)\n    recall = recall(y_true, y_pred)\n    return 2*((precision*recall)/(precision+recall+K.epsilon()))\n    \n\nclr = CyclicLR(base_lr=0.001, max_lr=0.01,\n                        step_size=300., mode='exp_range',\n                        gamma=0.99994)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8e88783736710cf21a8f89a4577a398eaffb52a","scrolled":true},"cell_type":"code","source":"def build_model():\n    inp = Input(shape=(maxlen,))\n    x = Embedding(max_features, embed_size, weights=[mean_gl_par_embedding],trainable=False)(inp)\n    x = SpatialDropout1D(rate=0.1)(x)\n    x1 = Bidirectional(CuDNNGRU(200, return_sequences=True))(x)\n    x2 = Bidirectional(CuDNNGRU(128, return_sequences=True))(x)\n\n    atten_1 = Attention(maxlen)(x1) # skip connect\n    #avg_pool = GlobalAveragePooling1D()(x1)\n    atten_2 = Attention(maxlen)(x2)\n    #max_pool = GlobalMaxPooling1D()(x2)\n    \n    x = concatenate([atten_1,atten_2])\n    x = Dense(128, activation=\"relu\")(x)\n    x = Dropout(0.2)(x)\n    x = BatchNormalization()(x)\n    x = Dense(1, activation=\"sigmoid\")(x)\n    model = Model(inputs=inp, outputs=x)\n    model.compile(loss='binary_crossentropy', optimizer=Adam(),)\n    \n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f554f3fa59b609fe4b19322ef19549c94d0778c8"},"cell_type":"code","source":"def f1_smart(y_true, y_pred):\n    args = np.argsort(y_pred)\n    tp = y_true.sum()\n    fs = (tp - np.cumsum(y_true[args[:-1]])) / np.arange(y_true.shape[0] + tp - 1, tp, -1)\n    res_idx = np.argmax(fs)\n    return 2 * fs[res_idx], (y_pred[args[res_idx]] + y_pred[args[res_idx + 1]]) / 2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3318e67024ad81c3616845d9f6f5c222adf92434","scrolled":false},"cell_type":"code","source":"kfold = StratifiedKFold(n_splits=5, random_state=1990, shuffle=True)\nbestscore = []\ny_test = np.zeros((test_X.shape[0], ))\nfilepath=\"weights_best_mean.h5\"\nfor i, (train_index, valid_index) in enumerate(kfold.split(train_X, train_y)):\n    X_train, X_val, Y_train, Y_val = train_X[train_index], train_X[valid_index], train_y[train_index], train_y[valid_index]\n    checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')\n    reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.6, patience=1, min_lr=0.0001, verbose=2)\n    earlystopping = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=2, verbose=2, mode='auto')\n    callbacks = [clr,]\n    model = build_model()\n    if i == 0:print(model.summary()) \n    model.fit(X_train, Y_train, batch_size=512, epochs=2, validation_data=(X_val, Y_val), verbose=1, callbacks=[checkpoint,clr,])\n    model.load_weights(filepath)\n    y_pred = model.predict([X_val], batch_size=1024, verbose=1)\n    y_test += np.squeeze(model.predict([test_X], batch_size=1024, verbose=2))/5\n    f1, threshold = f1_smart(np.squeeze(Y_val), np.squeeze(y_pred))\n    print('Optimal F1: {:.4f} at threshold: {:.4f}'.format(f1, threshold))\n    bestscore.append(threshold)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6abafc6bd2b50e51cd822d7d991e80ad56c224fb"},"cell_type":"code","source":"print(\"mean threshold--> \",np.mean(bestscore))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a6c60e352c40d8e89125f0c9e01568edd1939089"},"cell_type":"code","source":"print(y_test.shape)\npred_test_y = (y_test>np.mean(bestscore)).astype(int)\nout_df = pd.DataFrame({\"qid\":df_test[\"qid\"].values})\nout_df['prediction'] = pred_test_y\nout_df.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"496ed9f78a180044637d7462d197a12a267bcf73"},"cell_type":"code","source":"","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}