Kaggle competition: Quora Insincere Questions Classification
<SOS> metric: FScore_1 Kaggle data source: quora-insincere-questions-classification<categorify>
np.random.seed(42)
 warnings.filterwarnings('ignore' )
to_vw_only_text("test_only_text.vw", test_texts, is_train=False )<import_modules>
EMBEDDING_FILE = '.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt'
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 submission = pd.read_csv('.. /input/sample_submission.csv' )
!vw --oaa 3 train_part_only_text.vw -f model_only_text_part.vw -b 28 --random_seed 17 \
--loss_function logistic --ngram 2 --passes 10 -k -c<install_modules>
X_train = train["question_text"].fillna("fillna" ).values
y_train = train["target"].values
X_test = test["question_text"].fillna("fillna" ).values
max_features = 40000
maxlen = 50
embed_size = 300
tokenizer = text.Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(X_train)+ list(X_test))
X_train = tokenizer.texts_to_sequences(X_train)
 X_test = tokenizer.texts_to_sequences(X_test)
 x_train = sequence.pad_sequences(X_train, maxlen=maxlen)
 x_test = sequence.pad_sequences(X_test, maxlen=maxlen )
!vw -i model_only_text_part.vw -t -d valid_only_text.vw -p valid_pred1.txt \
--random_seed 17 -r valid_prob1.txt<compute_train_metric>
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))
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.zeros(( 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
def evaluate_vw_prediction(path_to_vw_pred_probs, is_test=False, target=None, write_submission=False,
submission_file=None, test_index=test_texts.index, columns=['EAP', 'MWS', 'HPL']):
def sigmoid(z):
return 1 /(1 + np.exp(-z))
with open(path_to_vw_pred_probs)as pred_file:
pred_probs = np.array([[float(pair.split(':')[1])for pair in line.strip().split() ]
for line in pred_file.readlines() ])
 pred_probs = sigmoid(pred_probs)
 if target is not None and not is_test:
print(log_loss(target, pred_probs))
if write_submission and submission_file is not None:
subm_df = pd.DataFrame(pred_probs, columns=columns)
 subm_df.index = test_index
subm_df.to_csv(submission_file )<predict_on_test>
class F1Evaluation(Callback):
def __init__(self, validation_data=() , interval=1):
super(Callback, self ).__init__()
self.interval = interval
self.X_val, self.y_val = validation_data
def on_epoch_end(self, epoch, logs={}):
if epoch % self.interval == 0:
y_pred = self.model.predict(self.X_val, verbose=0)
 y_pred =(y_pred > 0.35 ).astype(int)
 score = f1_score(self.y_val, y_pred)
 print("
F1 Score - epoch: %d - score: %.6f
" %(epoch+1, score))
evaluate_vw_prediction('valid_prob1.txt',
target=valid_texts['author_code'] )<import_modules>
filter_sizes = [1,2,3,5]
num_filters = 36
def get_model() :
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix] )(inp)
 x = SpatialDropout1D(0.4 )(x)
 conv_0 = Conv1D(num_filters, kernel_size=(filter_sizes[0]),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_1 = Conv1D(num_filters, kernel_size=(filter_sizes[1]),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_2 = Conv1D(num_filters, kernel_size=(filter_sizes[2]),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_3 = Conv1D(num_filters, kernel_size=(filter_sizes[3]),
kernel_initializer='he_normal', activation='elu' )(x)
 maxpool_0 = MaxPool1D(pool_size=(maxlen - filter_sizes[0] + 1))(conv_0)
 maxpool_1 = MaxPool1D(pool_size=(maxlen - filter_sizes[1] + 1))(conv_1)
 maxpool_2 = MaxPool1D(pool_size=(maxlen - filter_sizes[2] + 1))(conv_2)
 maxpool_3 = MaxPool1D(pool_size=(maxlen - filter_sizes[3] + 1))(conv_3)
 z = Concatenate(axis=1 )([maxpool_0, maxpool_1, maxpool_2, maxpool_3])
 z = Flatten()(z)
 z = BatchNormalization()(z)
 outp = Dense(1, activation="sigmoid" )(z)
 model = Model(inputs=inp, outputs=outp)
 model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 return model
model = get_model()
%%time
!vw --oaa 3 train_only_text.vw -f model_only_text.vw -b 28 --random_seed 17 \
--loss_function logistic --ngram 2 --passes 10 -k -c --quiet<save_to_csv>
batch_size = 1024
epochs = 4
X_tra, X_val, y_tra, y_val = train_test_split(x_train, y_train, train_size=0.95,
random_state=233)
 F1_Score = F1Evaluation(validation_data=(X_val, y_val), interval=1)
 hist = model.fit(X_tra, y_tra, batch_size=batch_size, epochs=epochs,
validation_data=(X_val, y_val),
callbacks=[F1_Score], verbose=True )
evaluate_vw_prediction('test_prob1.txt',
is_test=True, write_submission=True,
submission_file='submission1_only_text.csv' )<define_variables>
filter_sizes = [1,2,3,5]
num_filters = 36
def get_model() :
inp = Input(shape=(maxlen,))
x = Lambda(lambda x: K.reverse(x,axes=-1))(inp)
 x = Embedding(max_features, embed_size, weights=[embedding_matrix] )(x)
 x = SpatialDropout1D(0.4 )(x)
 conv_0 = Conv1D(num_filters, kernel_size=(filter_sizes[0]),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_1 = Conv1D(num_filters, kernel_size=(filter_sizes[1]),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_2 = Conv1D(num_filters, kernel_size=(filter_sizes[2]),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_3 = Conv1D(num_filters, kernel_size=(filter_sizes[3]),
kernel_initializer='he_normal', activation='elu' )(x)
 maxpool_0 = MaxPool1D(pool_size=(maxlen - filter_sizes[0] + 1))(conv_0)
 maxpool_1 = MaxPool1D(pool_size=(maxlen - filter_sizes[1] + 1))(conv_1)
 maxpool_2 = MaxPool1D(pool_size=(maxlen - filter_sizes[2] + 1))(conv_2)
 maxpool_3 = MaxPool1D(pool_size=(maxlen - filter_sizes[3] + 1))(conv_3)
 z = Concatenate(axis=1 )([maxpool_0, maxpool_1, maxpool_2, maxpool_3])
 z = Flatten()(z)
 z = BatchNormalization()(z)
 outp = Dense(1, activation="sigmoid" )(z)
 model = Model(inputs=inp, outputs=outp)
 model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 return model
model_flip = get_model()
max_words_in_text = train_texts['text'].apply(lambda text: len(re.findall("\w{3,}", text.strip())) ).max()
max_unique_words_in_text = train_texts['text'].apply(lambda text: len(set(re.findall("\w{3,}", text.strip())))).max()
max_aver_word_len_in_text = train_texts['text'].apply(lambda text:
sum([len(w)for w in re.findall("\w{3,}", text.strip())])/
len(re.findall("\w{3,}", text.strip())) ).max()<count_unique_values>
hist_flip = model_flip.fit(X_tra, y_tra, batch_size=batch_size, epochs=epochs,
validation_data=(X_val, y_val),
callbacks=[F1_Score], verbose=True )
max_words_in_text, max_unique_words_in_text, max_aver_word_len_in_text<categorify>
val_y_pred1 = model.predict(X_val, batch_size=1024, verbose = True)
 val_y_pred2 = model_flip.predict(X_val, batch_size=1024, verbose = True)
def to_vw_text_and_some_features(out_vw, df, is_train=True):
with open(out_vw, "w")as out:
for i in range(df.shape[0]):
if is_train:
target = df["author_code"].iloc[i]
else:
target = 1
text = df["text"].iloc[i].strip().replace('|', '' ).replace(':', '' ).lower()
words = re.findall("\w{3,}", text)
 new_text = " ".join(words)
 num_words = round(len(words)/ max_words_in_text, 4)
 num_uniq_words = round(len(set(words)) / max_unique_words_in_text, 4)
 aver_word_len = round(sum([len(w)for w in words])/ len(words)/ max_aver_word_len_in_text, 4)
 features = [num_words, num_uniq_words, aver_word_len]
features_vw = ' '.join(['{}:{}'.format(i[0], i[1])for i in zip(range(len(features)) , features)])
 s = "{} |text {} |num {}
".format(target, new_text, features_vw)
 out.write(s)
<feature_engineering>
val_y_pred = np.mean([val_y_pred1,val_y_pred2],axis = 0 )
to_vw_text_and_some_features("train_part_text_feat.vw", train_texts_part )<feature_engineering>
y_pred1 = model.predict(x_test, batch_size=1024, verbose = True)
 y_pred2 = model_flip.predict(x_test, batch_size=1024, verbose = True)
 y_pred = np.mean([y_pred1,y_pred2],axis = 0 )
to_vw_text_and_some_features("valid_text_feat.vw", valid_texts )<feature_engineering>
np.corrcoef([y_pred1[:,0],y_pred2[:,0]] )
to_vw_text_and_some_features("train_text_feat.vw", train_texts )<feature_engineering>
best_threshold = 0.01
best_score = 0.0
for threshold in range(1, 100):
threshold = threshold / 100
score = f1_score(y_val, val_y_pred > threshold)
 if score > best_score:
best_threshold = threshold
best_score = score
print("Score at threshold=0.5 is {}".format(f1_score(y_val, val_y_pred > 0.5)))
 print("Optimal threshold is {} with a score of {}".format(best_threshold, best_score))
<set_options><EOS> 
y_pred =(y_pred > best_threshold ).astype(int)
 submission['prediction'] = y_pred
submission.to_csv('submission.csv', index=False )