Kaggle competition: Quora Insincere Questions Classification
<SOS> metric: FScore_1 Kaggle data source: quora-insincere-questions-classification<prepare_x_and_y>
np.random.seed(7418880)
 tf.set_random_seed(7418880)
X = train_data[['Pclass', 'Sex', 'Age', 'SibSp', 'Embarked', 'Fare']]
Y = train_data['Survived']<normalization>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 print("Train shape : ", train_df.shape)
 print("Test_shape : ", test_df.shape)
 train_X = train_df["question_text"].fillna("_na_" ).values
test_X = test_df["question_text"].fillna("_na_" ).values
train_y = train_df['target'].values
embed_size = 300
max_words = 50000
max_len = 70
sc = StandardScaler()
X = sc.fit_transform(X )<split>
start_time = time.time()
tokenizer = Tokenizer(num_words=max_words)
 tokenizer.fit_on_texts(list(train_X))
train_X=tokenizer.texts_to_sequences(train_X)
 test_X=tokenizer.texts_to_sequences(test_X)
 train_X = pad_sequences(train_X, maxlen=max_len)
 test_X = pad_sequences(test_X, maxlen=max_len)
 print(train_X.shape, test_X.shape)
 print("Total time for tokenizing = {:.0f} s".format(time.time() -start_time))
trainX, testX, trainY, testY = train_test_split(X, Y, test_size=0.2, random_state=42 )<train_model>
start_time = time.time()
EMBEDDING_FILE = '.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt'
def get_coefs(word, *arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.rstrip().rsplit(' ')) for o in open(EMBEDDING_FILE))
all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
word_index = tokenizer.word_index
nb_words = min(max_words, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_words: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
print("Total time for embedding = {:.0f} s".format(time.time() -start_time))
model.fit(trainX, trainY )<predict_on_test>
def generate_model() :
input = Input(shape=(max_len,))
embed = Embedding(max_words, embed_size, weights=[embedding_matrix] )(input)
 x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(embed)
 x = Bidirectional(CuDNNGRU(64, return_sequences=True))(x)
 x = GlobalMaxPool1D()(x)
 x = Dense(16, activation="relu" )(x)
 x = Dropout(0.1 )(x)
 y = Bidirectional(CuDNNGRU(64, return_sequences=True))(embed)
 y = Bidirectional(CuDNNLSTM(64, return_sequences=True))(y)
 y = GlobalMaxPool1D()(y)
 y = Dense(16, activation="relu" )(y)
 y = Dropout(0.1 )(y)
 z= Concatenate()([x,y])
 output = Dense(1, activation="sigmoid" )(z)
 model = Model(inputs=input, outputs=output)
 return model
predict = model.predict(testX )<import_modules>
start_time = time.time()
uid = 1
version =1
n_splits = 5
n_epochs =2
batch_size =1024
skf = StratifiedKFold(n_splits=n_splits, random_state = 7418880, shuffle=False)
 val_preds = defaultdict(list)
 test_preds = {}
historyD={}
for ii,(train_index, val_index)in enumerate(skf.split(train_X, train_y)) :
X_train, X_val = train_X[train_index], train_X[val_index]
y_train, y_val = train_y[train_index], train_y[val_index]
model = generate_model()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 hist = model.fit(X_train, y_train, batch_size=batch_size, epochs=n_epochs,
validation_data=(X_val, y_val), verbose= True)
 historyD["fold{}".format(ii+1)] = hist.history
pred_val_y = model.predict([X_val], batch_size=batch_size, verbose=1)
 val_preds['fold{}'.format(ii+1)] = [pred_val_y.ravel() ,y_val]
pred_test_y = model.predict([test_X], batch_size=batch_size, verbose=1)
 test_preds['fold{}'.format(ii+1)] = pred_test_y.ravel()
del model
gc.collect()
with open('train_history_uid{}_v{}.pkl'.format(uid, version),'wb')as pklfile:
pickle.dump(historyD,pklfile)
 with open('val_preds_uid{}_v{}.pkl'.format(uid, version),'wb')as pklfile:
pickle.dump(val_preds,pklfile)
 with open('test_preds_uid{}_v{}.pkl'.format(uid, version),'wb')as pklfile:
pickle.dump(test_preds,pklfile)
 print("Total time for training = {:.0f} s".format(time.time() -start_time))
from sklearn.metrics import accuracy_score<compute_test_metric>
test01_df = pd.DataFrame()
for ii in range(len(val_preds)) :
threshL = []
y_preds, y_actual =(val_preds['fold{}'.format(ii+1)])
 for idx, thresh in enumerate(np.arange(0.1, 0.51, 0.01)) :
thresh = np.round(thresh,2)
 y_01 = [ 0 if x <thresh else 1 for x in y_preds]
threshL.append(( metrics.f1_score(y_actual,y_01), thresh))
threshL=sorted(threshL)
 best_F1, opt_thresh = threshL[-1]
print("For fold {0}, best validation F1 of {1:.5f} at threshold {2:.2f}".format(ii+1, best_F1,opt_thresh))
test01_df['fold{}'.format(ii)] = [ 0 if x<opt_thresh else 1 for x in test_preds['fold{}'.format(ii+1)]]
accuracy_score(predict, testY )<import_modules>
print(test01_df.sum(axis=1 ).value_counts() )
from sklearn.ensemble import RandomForestClassifier<choose_model_class>
out_df = pd.DataFrame({"qid":test_df["qid"].values})
 out_df['prediction'] = [ 0 if x<2.5 else 1 for x in test01_df.sum(axis=1)]
out_df.to_csv("submission.csv", index=False )