Kaggle competition: Quora Insincere Questions Classification
<SOS> metric: FScore_1 Kaggle data source: quora-insincere-questions-classification<sort_values>
import os
import time
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import metrics
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Dense, Input, LSTM, Embedding, Dropout, Activation, CuDNNLSTM, Conv1D,Dropout
from keras.layers import Bidirectional, GlobalMaxPool1D,MaxPooling1D,BatchNormalization,Flatten,RepeatVector,Permute,concatenate
from keras.models import Model
train_df[['Pclass','Survived']].groupby(['Pclass'],as_index=False ).mean().sort_values(by='Survived',ascending=False )<sort_values>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
train_df[['SibSp','Survived']].groupby(['SibSp'],as_index=False ).mean().sort_values(by='Survived',ascending=False )<sort_values>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
train_df[['Parch','Survived']].groupby(['Parch'],as_index=False ).mean().sort_values(by='Survived',ascending=False )<feature_engineering>
train, dev = train_test_split(train, test_size=0.1, random_state=2018)
 embed_size = 300
max_features = 50000
maxlen = 40
train_X = train["question_text"].fillna("_na_" ).values
dev_X = dev["question_text"].fillna("_na_" ).values
test_X = test["question_text"].fillna("_na_" ).values
tokenizer = Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(train_X))
train_X = tokenizer.texts_to_sequences(train_X)
 dev_X = tokenizer.texts_to_sequences(dev_X)
 test_X = tokenizer.texts_to_sequences(test_X)
 train_X = pad_sequences(train_X, maxlen=maxlen)
 dev_X = pad_sequences(dev_X, maxlen=maxlen)
 test_X = pad_sequences(test_X, maxlen=maxlen)
 train_y = train['target'].values
dev_y = dev['target'].values
dataset["Fsize"] = dataset["SibSp"] + dataset["Parch"] + 1
dataset['Single'] = dataset['Fsize'].map(lambda s: 1 if s == 1 else 0)
 dataset['SmallF'] = dataset['Fsize'].map(lambda s: 1 if s == 2 else 0)
 dataset['MedF'] = dataset['Fsize'].map(lambda s: 1 if 3 <= s <= 4 else 0)
 dataset['LargeF'] = dataset['Fsize'].map(lambda s: 1 if s >= 5 else 0)
<feature_engineering>
dataset["Fare"] = dataset["Fare"].map(lambda i: np.log(i)if i > 0 else 0 )<data_type_conversions>
EMBEDDING_FILE = '.. /input/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE)if len(o)>100)
 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_features, 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_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
dataset['Fare']=dataset['Fare'].fillna(dataset['Fare'].dropna().median())
<feature_engineering>
def f1(y_true, y_pred):
def recall(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 return recall
def precision(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 precision = true_positives /(predicted_positives + K.epsilon())
 return precision
precision = precision(y_true, y_pred)
 recall = recall(y_true, y_pred)
 return 2*(( precision*recall)/(precision+recall+K.epsilon()))
dataset.loc[dataset['Fare'] <=2.06,'Fare']=0
dataset.loc[(dataset['Fare'] <=2.67)&(dataset['Fare'] > 2.06),'Fare']=1
dataset.loc[(dataset['Fare'] <=3.44)&(dataset['Fare'] > 2.67),'Fare']=2
dataset.loc[(dataset['Fare'] <=6.2)&(dataset['Fare'] > 3.44),'Fare']=3
dataset.loc[dataset['Fare'] > 6.2 ,'Fare']=4
dataset['Fare']=dataset['Fare'].astype(int )<filter>
def net(input_shape):
sentence_indices = Input(input_shape, dtype='int32')
 X = Embedding(max_features, embed_size,weights=[embedding_matrix] )(sentence_indices)
 activations = Bidirectional(CuDNNLSTM(128, return_sequences=True))(X)
 activations = Dropout(0.1 )(activations)
 attention = Dense(1, activation='tanh' )(activations)
 attention = Flatten()(attention)
 attention = Activation('softmax' )(attention)
 attention = RepeatVector(128 )(attention)
 attention = Permute([2, 1] )(attention)
 X = concatenate([activations, attention])
 activations = Bidirectional(CuDNNLSTM(128, return_sequences=True))(X)
 activations = Dropout(0.1 )(activations)
 attention = Dense(1, activation='tanh' )(activations)
 attention = Flatten()(attention)
 attention = Activation('softmax' )(attention)
 attention = RepeatVector(128 )(attention)
 attention = Permute([2, 1] )(attention)
 X = concatenate([activations, attention])
 X = Bidirectional(CuDNNLSTM(64, return_sequences=True))(X)
 X = Dropout(0.1 )(X)
 X = GlobalMaxPool1D()(X)
 X = Dense(16, activation="relu" )(X)
 X = Dropout(0.1 )(X)
 X = Dense(1, activation="sigmoid" )(X)
 model = Model(inputs=sentence_indices, outputs=X)
 return model
model=net(( maxlen,))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy',f1])
 print(model.summary() )
index_NaN_age = list(dataset["Age"][dataset["Age"].isnull() ].index)
 for i in index_NaN_age:
age_med = dataset["Age"].median()
age_pred = dataset["Age"][(( dataset['SibSp'] == dataset.iloc[i]["SibSp"])& 
(dataset['Parch'] == dataset.iloc[i]["Parch"])& 
(dataset['Pclass'] == dataset.iloc[i]["Pclass"])) ].median()
if not np.isnan(age_pred):
dataset['Age'].iloc[i] = age_pred
else :
dataset['Age'].iloc[i] = age_med 
<feature_engineering>
weight={0:0.4,1:1}
dataset['AgeBand']=pd.cut(dataset['Age'],5)
<groupby>
model.fit(train_X, train_y, batch_size=512, epochs=3 , validation_data=(dev_X, dev_y),class_weight=weight )
dataset[['AgeBand','Survived']].groupby(['AgeBand'],as_index=False ).mean()<feature_engineering>
model.fit(train_X, train_y, batch_size=512, epochs=2, validation_data=(dev_X, dev_y),class_weight=weight )
dataset.loc[dataset['Age'] <=16 , 'Age' ] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <=32), 'Age' ] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <=48), 'Age' ] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <=64), 'Age' ] = 3
dataset.loc[(dataset['Age'] > 64), 'Age' ] = 4
dataset['Age']=dataset['Age'].astype(int)
<drop_column>
f1=[]
pred_noemb_val_y = model.predict([dev_X], batch_size=1024, verbose=1)
 for thresh in np.arange(0.4, 0.702, 0.01):
thresh = np.round(thresh, 2)
 f1.append(metrics.f1_score(dev_y,(pred_noemb_val_y>thresh ).astype(int)))
 print("F1 score at threshold {0} is {1}".format(thresh, metrics.f1_score(dev_y,(pred_noemb_val_y>thresh ).astype(int))))
f1=np.array(f1 )
dataset=dataset.drop(['Name','Parch','PassengerId','SibSp','Fsize','FareBand','AgeBand'],axis=1 )<categorify>
thresh=np.argmax(f1)*0.01+0.4
print(thresh )
dataset=pd.get_dummies(dataset)
<categorify>
pred_noemb_val_y1 = model.predict([test_X], verbose=1)
 label1=(pred_noemb_val_y1>thresh ).astype(int)
 model=None
dataset=pd.get_dummies(dataset,prefix=['Pclass','Age','Fare'],columns=['Pclass','Age','Fare'] )<split>
test_labels=label1
train = dataset[:train_len]
test = dataset[train_len:]
test.drop(labels=["Survived"],axis = 1,inplace=True )<prepare_x_and_y>
submission=pd.read_csv(".. /input/sample_submission.csv")
 submission["prediction"]=test_labels
submission.head()
submission.to_csv("submission.csv",index=False )