Kaggle competition: Quora Insincere Questions Classification
<SOS> metric: FScore_1 Kaggle data source: quora-insincere-questions-classification<define_search_space>
print(os.listdir(".. /input"))
param_grid = {'criterion': ['gini', 'entropy'],
'max_depth': [2,4,6,8,10,None],
'random_state': [0]}
tune_model = model_selection.GridSearchCV(tree.DecisionTreeClassifier() , param_grid=param_grid, scoring='roc_auc', cv=cv_split)
 tune_model.fit(train[X], train[y])
 print('Parameters: ', tune_model.best_params_ )<train_on_grid>
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()))
def calc_ratio(p):
return p/(1-p)
 def pretoken(X):
X = X.map(lambda x : re.sub(r'[^\w\s]','',x))
X = X.map(lambda x : x.lower())
 X = X.map(lambda x : x.split())
 return X
def tokenize_padding(X, max_length, token):
X = token.texts_to_sequences(X)
 X = pad_sequences(X, maxlen=max_length, padding='post')
 return X
def train_preproc(X, max_length):
X = pretoken(X)
 token = Tokenizer()
token.fit_on_texts(X_train)
 X = tokenize_padding(X,max_length,token)
 return X, token, len(token.word_index)+ 1
def test_preproc(X, max_length, token):
X = pretoken(X)
 X = tokenize_padding(X,max_length,token)
 return X
def cutoff(x, frac):
if x > frac:
return 1
else:
return 0
clf = tree.DecisionTreeClassifier()
results = model_selection.cross_validate(clf, train[X], train[y], cv=cv_split)
 clf.fit(train[X], train[y])
 results['test_score'].mean() *100
fs = feature_selection.RFECV(clf, step=1, scoring='accuracy', cv=cv_split)
 fs.fit(train[X], train[y])
 X = train[X].columns.values[fs.get_support() ]
results = model_selection.cross_validate(clf, train[X], train[y], cv=cv_split)
 print('Shape New: ', train[X].shape)
 print('Features to use: ', X )<train_on_grid>
trainData = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
tuned = model_selection.GridSearchCV(tree.DecisionTreeClassifier() , param_grid=param_grid, scoring = 'roc_auc', cv=cv_split)
 tuned.fit(train[X], train[y])
 param_grid = tuned.best_params_
param_grid<train_on_grid>
X_train , X_dev, y_train, y_dev = train_test_split(trainData['question_text'], trainData['target'], test_size=0.2, random_state=93)
 max_sen_length = 30
X_train, token, vocab_size = train_preproc(X_train, max_sen_length)
 X_dev = test_preproc(X_dev, max_sen_length, token)
 X_test = test_preproc(test['question_text'], max_sen_length,token )
clf = ensemble.GradientBoostingClassifier()
results = model_selection.cross_validate(clf, train[X], train[y], cv=cv_split)
 clf.fit(train[X], train[y])
 results['test_score'].mean() *100<predict_on_test>
embedding_matrix = np.random.normal(glove_mean, glove_std, [vocab_size,300])
 for word, i in token.word_index.items() :
try:
embedding_matrix[i] = embeddings_index[word]
except:
continue
test['Survived'] = clf.predict(test[X] )<save_to_csv>
model2 = Sequential()
model2.add(Embedding(vocab_size, 300, weights = [embedding_matrix], input_length = max_sen_length, trainable = False))
model2.add(SpatialDropout1D(0.2))
model2.add(Bidirectional(CuDNNLSTM(100, return_sequences = True)))
 model2.add(Bidirectional(CuDNNLSTM(100, return_sequences = True)))
 model2.add(Flatten())
 model2.add(Dense(16, activation = 'relu'))
model2.add(Dropout(0.5))
model2.add(Dense(8, activation = 'relu'))
model2.add(Dropout(0.5))
model2.add(Dense(1, activation = 'sigmoid'))
opt = keras.optimizers.Adam(lr = 1e-3, decay = 1e-5)
 model2.compile(loss = 'binary_crossentropy', optimizer = opt, metrics = [f1,'accuracy'])
 model2.fit(X_train, y_train, batch_size = 256, epochs = 5, validation_data =(X_dev,y_dev), verbose = 2 )
submit = pd.DataFrame({ 'PassengerId' : col1, 'Survived': test['Survived'] } ).set_index('PassengerId')
 submit['Survived'] = submit['Survived'].astype('int')
 submit.to_csv('submission.csv' )<install_modules>
filter_size = [1,2,3,4]
conv_lst = []
pool_lst = []
flat_lst = []
model_input = Input(shape=(max_sen_length,), dtype='int32', name = 'model_input')
 emb_layer = Embedding(vocab_size, 300, weights = [embedding_matrix], input_length = max_sen_length, trainable = False )(model_input)
 x = SpatialDropout1D(0.4 )(emb_layer)
 for i in range(len(filter_size)) :
conv_lst += [Conv1D(filters = 64, kernel_size = filter_size[i], padding = 'same', name = 'conv'+str(filter_size[i]))(x)]
pool_lst += [MaxPooling1D(pool_size = max_sen_length+1-filter_size[i] )(conv_lst[i])]
flat_lst += [Flatten()(pool_lst[i])]
x = concatenate(flat_lst)
 x = Dense(16, activation = 'relu' )(x)
 x = Dropout(0.5 )(x)
 x = Dense(8, activation = 'relu' )(flat_lst[i])
 model_output = Dense(1, activation = 'sigmoid' )(x)
 opt = keras.optimizers.Adam(lr = 1e-3, decay = 1e-5)
 model = Model(inputs=model_input, outputs = model_output)
 model.compile(loss = 'binary_crossentropy', optimizer = opt, metrics = [f1,'accuracy'])
 model.fit(X_train, y_train, epochs = 3, batch_size=256, validation_data =(X_dev,y_dev), verbose = 2 )
!pip install -U pandas-profiling==2.9.0<import_modules>
ensemble_p = 0.3
y_dev_predict = pd.DataFrame(ensemble_p*model.predict(X_dev)+(1-ensemble_p)*model2.predict(X_dev))
f1_lst = []
for i in range(1,7):
f1_lst += [f1_score(y_dev, y_dev_predict[0].map(lambda x : cutoff(x, i/10)))]
ensemble_cutoff =(np.argmax(f1_lst)+ 1)/10
<import_modules><EOS> 
y_pred = ensemble_p*model.predict(X_test)+(1-ensemble_p)*model2.predict(X_test)
 y_pred = pd.DataFrame(y_pred)
 my_submission = pd.DataFrame({'qid': test.qid, 'prediction': y_pred[0].map(lambda x : cutoff(x, ensemble_cutoff)) })
 my_submission.to_csv('submission.csv',index=False )