Kaggle competition: Quora Insincere Questions Classification
<SOS> metric: FScore_1 Kaggle data source: quora-insincere-questions-classification<train_on_grid>
color = sns.color_palette()
%matplotlib inline
py.init_notebook_mode(connected=True)
 pd.options.mode.chained_assignment = None
pd.options.display.max_columns = 999
knn_classifier=KNeighborsClassifier()
knn_param_grid = [{'n_neighbors':[10],'weights':['uniform'],'algorithm':['brute'],
}]
gs_KNN = GridSearchCV(estimator = knn_classifier,
param_grid = knn_param_grid,
scoring = 'accuracy',
cv = kfold,
n_jobs = -1)
 gs_KNN = gs_KNN.fit(X_train, Y_train)
 knn_best_params = gs_KNN.best_params_
knn_best_score = gs_KNN.best_score_
knn_best=gs_KNN.best_estimator_
knn_best_score<train_on_grid>
stop_words = set(stopwords.words('english'))
lr_classifier=LogisticRegression()
lr_param_grid = [{'penalty':['l1','l2'] , 'C':[1]}]
gs_LR = GridSearchCV(estimator = lr_classifier,
param_grid = lr_param_grid,
scoring = 'accuracy',
cv = kfold,
n_jobs = -1)
 gs_LR = gs_LR.fit(X_train, Y_train)
 lr_best_params = gs_LR.best_params_
lr_best_score = gs_LR.best_score_
lr_best=gs_LR.best_estimator_
lr_best_score<choose_model_class>
puncts = [',', '.', '"', ':', ')', '(', '-', '!', '?', '|', ';', "'", '$', '&', '/', '[', ']', '>', '%', '=', '
'·', '_', '{', '}', '©', '^', '®', '`', '<', '→', '°', '€', '™', '›', '♥', '←', '×', '§', '″', '′', 'Â', '█', '½', 'à', '…',
'“', '★', '”', '–', '●', 'â', '►', '−', '¢', '²', '¬', '░', '¶', '↑', '±', '¿', '▾', '═', '¦', '║', '―', '¥', '▓', '—', '‹', '─',
'▒', '：', '¼', '⊕', '▼', '▪', '†', '■', '’', '▀', '¨', '▄', '♫', '☆', 'é', '¯', '♦', '¤', '▲', 'è', '¸', '¾', 'Ã', '⋅', '‘', '∞',
'∙', '）', '↓', '、', '│', '（', '»', '，', '♪', '╩', '╚', '³', '・', '╦', '╣', '╔', '╗', '▬', '❤', 'ï', 'Ø', '¹', '≤', '‡', '√', ]
def clean_text(x):
x = str(x)
 for punct in puncts:
x = x.replace(punct, f' {punct} ')
 return x
adadtc_classifier=AdaBoostClassifier(dtc_best)
 adadtc_param_grid = [{'n_estimators':[500],
"learning_rate": [0.1],
"algorithm" : ["SAMME"],
}]
gs_ADADTC = GridSearchCV(estimator = adadtc_classifier,
param_grid = adadtc_param_grid,
scoring = 'accuracy',
cv = kfold,
n_jobs = -1)
 gs_ADADTC = gs_ADADTC.fit(X_train, Y_train)
 adadtc_best_params = gs_ADADTC.best_params_
adadtc_best_score = gs_ADADTC.best_score_
adadtc_best=gs_ADADTC.best_estimator_
adadtc_best_score<choose_model_class>
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 )
xt_classifier=ExtraTreesClassifier()
xt_param_grid = [{'n_estimators':[100],
'criterion':['entropy'],
'max_features':[None],
'max_depth':[None],
'min_samples_split':[2],
'min_samples_leaf':[10]
}]
gs_XT = GridSearchCV(estimator = xt_classifier,
param_grid = xt_param_grid,
scoring = 'accuracy',
cv = kfold,
n_jobs = -1)
 gs_XT = gs_XT.fit(X_train, Y_train)
 xt_best_params = gs_XT.best_params_
xt_best_score = gs_XT.best_score_
xt_best=gs_XT.best_estimator_
xt_best_score<train_model>
train_df["question_text"] = train_df["question_text"].apply(lambda x: clean_text(x))
test_df["question_text"] = test_df["question_text"].apply(lambda x: clean_text(x))
votingC = VotingClassifier(estimators=[('svc', svc_best),('dtc', dtc_best), 
('rfc', rfc_best),('knn',knn_best),('lr',lr_best),('adadtc',adadtc_best),('xt',xt_best)], voting='soft', n_jobs=-1)
 votingC = votingC.fit(X_train, Y_train )<save_to_csv>
countries = ["United States", "France", "India", "China", "Russia", "Saudi Arabia", "Iran", \
"Germany","England", "Europe", "Mexico", "Italy", "Spain", "Turkey", "Syria", "Israel"]
politicians = ["Trump", "Hillary", "Pence", "Obama", "Bush", "Clinton", "Putin", "Merkel"]
groups = ["Hispanics", "Blacks", "Whites", "Asians", "Browns", "Arabs", "Natives", "Caucasians", "Africans",\
"Indians", "Chineses", "Mexicans", "British", "Americans", "Spanish", "Greek", "Italians", \
"Turkish", "French", "English", "Germans", "Russians","Arabic", "Persians", "European", "Madeup"]
political_groups = ["democrats", "republicans", "liberals", "socialists", "communists"]
religious_groups = ["christians", "muslims", "jews", "buddhists", "hindus", "israelites", "zionists"]
test_Survived = pd.Series(votingC.predict(test), name="Survived")
 results = pd.concat([IDtest,test_Survived],axis=1)
 results.to_csv("ensemble_python_voting.csv",index=False )<import_modules>
goofy_test_questions = []
print("Important libraries loaded successfully" )<load_from_csv>
goofy_test_y = np.ones(len(goofy_test_questions))
for sentence in goofy_test_questions:
sentence = clean_text(sentence)
 goofy_test_X = np.asarray(goofy_test_questions )
ds_train=pd.read_csv("/kaggle/input/titanic/train.csv")
 ds_test=pd.read_csv("/kaggle/input/titanic/test.csv")
 ds_result=pd.read_csv("/kaggle/input/titanic/gender_submission.csv")
 print("Train and Test data sets are imported successfully" )<drop_column>
embed_size = 300
max_features = 90000
maxlen = 60
ds_train=ds_train.drop(['Ticket','Cabin'],axis=1)
 print("Columns Dropped Successfully")
 ds_train.head()<define_variables>
def get_long_sentences(sent_list, cut_off_len = maxlen):
ids_for_long = []
section_list = []
for i, sent in enumerate(list(sent_list)) :
for section in re.split(r'([\w+\s?]+[.?!])', sent):
if len(section)> 1:
section_list.append(section)
 ids_for_long.append(i)
 return section_list, ids_for_long
print("Number of teenagers and child passengers in ship are {}".format(len(ds_train[ds_train['Age'] < 20 ])) )<drop_column>
def get_generalized_sentences(sent_list, y_list):
global groups, political_groups, countries, religious_groups
general_X_list = []
general_y_list = []
for i, sent in enumerate(list(sent_list)) :
for word in sent.split() :
if(word in groups)or(word in political_groups)or(word in countries)or(word in religious_groups):
new_sent = sent.replace(word, "oovword")
 general_X_list.append(new_sent)
 general_y_list.append(y_list[i])
 return general_X_list, general_y_list
ds_train=ds_train.drop(['Embarked','Name'],axis=1)
 print("Columns Dropped Successfully")
 ds_train.head()<feature_engineering>
def dot_product(x, kernel):

 if K.backend() == 'tensorflow':
return K.squeeze(K.dot(x, K.expand_dims(kernel)) , axis=-1)
 else:
return K.dot(x, kernel )
ds_train['Family_Size'] = ds_train['SibSp'] + ds_train['Parch'] + 1
print("Family Size column created sucessfully")
 ds_train.head()<feature_engineering>
class AttentionWithContext(Layer):

 def __init__(self,
W_regularizer=None, u_regularizer=None, b_regularizer=None,
W_constraint=None, u_constraint=None, b_constraint=None,
bias=True, **kwargs):
self.supports_masking = True
self.init = initializers.get('glorot_uniform')
 self.W_regularizer = regularizers.get(W_regularizer)
 self.u_regularizer = regularizers.get(u_regularizer)
 self.b_regularizer = regularizers.get(b_regularizer)
 self.W_constraint = constraints.get(W_constraint)
 self.u_constraint = constraints.get(u_constraint)
 self.b_constraint = constraints.get(b_constraint)
 self.bias = bias
super(AttentionWithContext, self ).__init__(**kwargs)
 def build(self, input_shape):
assert len(input_shape)== 3
self.W = self.add_weight(( input_shape[-1], input_shape[-1],),
initializer=self.init,
name='{}_W'.format(self.name),
regularizer=self.W_regularizer,
constraint=self.W_constraint)
 if self.bias:
self.b = self.add_weight(( input_shape[-1],),
initializer='zero',
name='{}_b'.format(self.name),
regularizer=self.b_regularizer,
constraint=self.b_constraint)
 self.u = self.add_weight(( input_shape[-1],),
initializer=self.init,
name='{}_u'.format(self.name),
regularizer=self.u_regularizer,
constraint=self.u_constraint)
 super(AttentionWithContext, self ).build(input_shape)
 def compute_mask(self, input, input_mask=None):
return None
def call(self, x, mask=None):
uit = dot_product(x, self.W)
 if self.bias:
uit += self.b
uit = K.tanh(uit)
 ait = dot_product(uit, self.u)
 a = K.exp(ait)
 if mask is not None:
a *= K.cast(mask, K.floatx())
 a /= K.cast(K.sum(a, axis=1, keepdims=True)+ K.epsilon() , K.floatx())
 a = K.expand_dims(a)
 weighted_input = x * a
return K.sum(weighted_input, axis=1)
 def compute_output_shape(self, input_shape):
return input_shape[0], input_shape[-1]
age_by_pclass_sex = ds_train.groupby(['Sex', 'Pclass'] ).median() ['Age']
for pclass in range(1, 4):
for sex in ['female', 'male']:
print('Median age of Pclass {} {}s: {}'.format(pclass, sex, age_by_pclass_sex[sex][pclass]))
print('Median age of all passengers: {}'.format(ds_train['Age'].median()))
ds_train['Age'] = ds_train.groupby(['Sex', 'Pclass'])['Age'].apply(lambda x: x.fillna(x.median()))<categorify>
train_df, val_df = train_test_split(train_df, test_size=0.1, random_state=2018)
 train_y = train_df['target'].values
val_y = val_df['target'].values
ds_train=ds_train.replace(to_replace='male',value=0)
 ds_train=ds_train.replace(to_replace='female',value=1)
 ds_train.head()<train_model>
train_X = train_df["question_text"].fillna("_na_" ).values
val_X = val_df["question_text"].fillna("_na_" ).values
test_X = test_df["question_text"].fillna("_na_" ).values
X_train=ds_train.drop(['Survived'],axis=1)
 y_train=ds_train['Survived'].values
print('X_train shape: {}'.format(X_train.shape))
print('y_train shape: {}'.format(y_train.shape))<choose_model_class>
generalized_train_X, generalized_train_y = get_generalized_sentences(train_X, train_y )
classifier_rf=RandomForestClassifier(criterion='gini',
n_estimators=1100,
max_depth=5,
min_samples_split=4,
min_samples_leaf=5,
max_features='auto',
oob_score=True,
random_state=42,
n_jobs=-1,
verbose=1)
 classifier_rf.fit(X_train,y_train )<train_model>
long_val_X, long_id_list = get_long_sentences(val_X)
 long_test_X, long_test_id = get_long_sentences(test_X )
classifier_xgb=XGBClassifier(max_depth=3, n_estimators=300, learning_rate=0.05)
 classifier_xgb.fit(X_train,y_train )<feature_engineering>
tokenizer = Tokenizer(num_words=max_features)
 tokenizer_list = list(train_X)
 tokenizer.fit_on_texts(tokenizer_list)
 train_X = tokenizer.texts_to_sequences(train_X)
 val_X = tokenizer.texts_to_sequences(val_X)
 test_X = tokenizer.texts_to_sequences(test_X)
 long_val_X = tokenizer.texts_to_sequences(np.asarray(long_val_X))
long_test_X = tokenizer.texts_to_sequences(np.asarray(long_test_X))
generalized_train_X = tokenizer.texts_to_sequences(np.asarray(generalized_train_X))
generalized_train_y = np.asarray(generalized_train_y )
age_by_pclass_sex = ds_test.groupby(['Sex', 'Pclass'] ).median() ['Age']
for pclass in range(1, 4):
for sex in ['female', 'male']:
print('Median age of Pclass {} {}s: {}'.format(pclass, sex, age_by_pclass_sex[sex][pclass]))
print('Median age of all passengers: {}'.format(ds_test['Age'].median()))
ds_test['Age'] = ds_test.groupby(['Sex', 'Pclass'])['Age'].apply(lambda x: x.fillna(x.median()))<feature_engineering>
goofy_test_X = tokenizer.texts_to_sequences(goofy_test_X )
null_index=ds_test['Fare'].isnull().index
medianFare=ds_test['Fare'].median()
ds_test.at[null_index,'Fare'] = medianFare
print("Missing Fare updated as Median Fare :{}".format(medianFare))<categorify>
train_X = pad_sequences(train_X, maxlen=maxlen)
 val_X = pad_sequences(val_X, maxlen=maxlen)
 test_X = pad_sequences(test_X, maxlen=maxlen)
 long_val_X = pad_sequences(long_val_X, maxlen=maxlen)
 long_test_X = pad_sequences(long_test_X, maxlen=maxlen)
 generalized_train_X = pad_sequences(generalized_train_X, maxlen=maxlen )
ds_test=ds_test.drop(['Ticket','Cabin','Embarked','Name'],axis=1)
 print("Columns Dropped Successfully")
 ds_test['Family_Size'] = ds_test['SibSp'] + ds_test['Parch'] + 1
print("Family Size column created sucessfully")
 ds_test=ds_test.replace(to_replace='male',value=0)
 ds_test=ds_test.replace(to_replace='female',value=1)
 X_test=ds_test
X_test.head()<save_to_csv>
goofy_test_X = pad_sequences(goofy_test_X, maxlen=maxlen )
y_pred_rf=classifier_rf.predict(X_test)
 y_pred_xgb=classifier_xgb.predict(X_test)
 y_pred_rf=y_pred_rf.ravel()
y_pred_xgb=y_pred_xgb.ravel()
submission_df_rf = pd.DataFrame(columns=['PassengerId', 'Survived'])
 submission_df_rf['PassengerId'] = X_test['PassengerId'].astype(int)
 submission_df_rf['Survived'] = y_pred_rf
submission_df_rf.to_csv('submissions_rf.csv', header=True, index=False)
 submission_df_xgb = pd.DataFrame(columns=['PassengerId', 'Survived'])
 submission_df_xgb['PassengerId'] = X_test['PassengerId'].astype(int)
 submission_df_xgb['Survived'] = y_pred_xgb
submission_df_xgb.to_csv('submissions_xgb.csv', header=True, index=False )<compute_train_metric>
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.split(" ")) 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]
random_vector = np.random.rand(300)
 word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix1 = 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_matrix1[i] = embedding_vector
else:
embedding_matrix1[i] = random_vector
accuracies_rf = cross_val_score(estimator = classifier_rf, X = X_train, y = y_train, cv = 10)
 accuracies_xgb = cross_val_score(estimator = classifier_xgb, X = X_train, y = y_train, cv = 10 )<compute_test_metric>
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_matrix2 = 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_matrix2[i] = embedding_vector
print("Accuracies for 10 Fold in Random Forest Model is {}".format(accuracies_rf))
print("Accuracies for 10 Fold in XG Boost Model is {}".format(accuracies_xgb))<compute_test_metric>
inp = Input(shape=(maxlen,))
model1_out = Embedding(max_features, embed_size, weights=[embedding_matrix1],trainable=False )(inp)
 model1_out = Bidirectional(CuDNNGRU(128, return_sequences=True))(model1_out)
 model1_out = AttentionWithContext()(model1_out)
 model1_out = Dense(64, activation="relu" )(model1_out)
 model1_out = Dropout(0.1 )(model1_out)
 model1_out = Dense(32, activation="relu" )(model1_out)
 model1_out = Dropout(0.1 )(model1_out)
 model1_out = Dense(1, activation="sigmoid" )(model1_out)
 model = Model(inputs=inp, outputs=model1_out)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 print(model.summary() )
print("Mean Accuracy for Random Forest Model is {}".format(accuracies_rf.mean()))
print("Mean Accuracy for XG Boost Model is {}".format(accuracies_xgb.mean()))
print("Standard Deviation for Random Forest Model is {}".format(accuracies_rf.std()))
print("Standard Deviation for XG Boost Model is {}".format(accuracies_xgb.std()))<train_on_grid>
inp = Input(shape=(maxlen,))
model2_out = Embedding(max_features, embed_size, weights=[embedding_matrix2],trainable=False )(inp)
 model2_out = Bidirectional(CuDNNGRU(128, return_sequences=True))(model2_out)
 model2_out = AttentionWithContext()(model2_out)
 model2_out = Dense(64, activation="relu" )(model2_out)
 model2_out = Dropout(0.1 )(model2_out)
 model2_out = Dense(32, activation="relu" )(model2_out)
 model2_out = Dropout(0.1 )(model2_out)
 model2_out = Dense(1, activation="sigmoid" )(model2_out)
 model2 = Model(inputs=inp, outputs=model2_out)
 model2.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 print(model2.summary() )
param_grid = { 'bootstrap': [True],
'max_depth': [80, 90, 100, 110],
'max_features': [2, 3],
'min_samples_leaf': [3, 4, 5],
'min_samples_split': [8, 10, 12],
'n_estimators': [100, 300, 500, 1000] }
grid_search = GridSearchCV(estimator = classifier_rf, param_grid = param_grid,cv = 3, n_jobs = -1)
 grid_search = grid_search.fit(X_train, y_train )<find_best_params>
def train_model(model, all_train_X, all_train_y, all_val_X, all_val_y, epochs=2):
filepath="weights_best.h5"
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=2, save_best_only=True, mode='min')
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=1, min_lr=0.0001, verbose=2)
 earlystopping = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=2, verbose=2, mode='auto')
 callbacks = [checkpoint, reduce_lr]
for e in range(epochs):
model.fit(all_train_X, all_train_y, batch_size=1024, epochs=1, validation_data=(all_val_X, all_val_y), callbacks=callbacks)
 model.load_weights(filepath)
 return model
best_accuracy = grid_search.best_score_
best_parameters = grid_search.best_params_
print("Best Accuracy for Random Forest Classifier is {}".format(best_accuracy))
print("Best Parameters for Random Forest Classifier is {}".format(best_parameters))<choose_model_class>
model = train_model(model, train_X, train_y, val_X, val_y, epochs=12 )
classifier_rf_new = RandomForestClassifier(n_estimators = 719,
bootstrap=False,
max_depth=464,
max_features=0.3,
min_samples_leaf=1,
min_samples_split=2,
random_state=42)
 classifier_rf_new.fit(X_train, y_train )<save_to_csv>
pred_val_y = model.predict(val_X, batch_size=1024, verbose=1 )
print("Predicting Results from new Classifier and Converting into Submission file")
 y_pred_rf_new=classifier_rf_new.predict(X_test)
 y_pred_rf_new=y_pred_rf_new.ravel()
submission_df_rf_new = pd.DataFrame(columns=['PassengerId', 'Survived'])
 submission_df_rf_new['PassengerId'] = X_test['PassengerId'].astype(int)
 submission_df_rf_new['Survived'] = y_pred_rf_new
submission_df_rf_new.to_csv('submissions_rf_new.csv', header=True, index=False)
 print("Created Submission file from new classifier successfully" )<set_options>
model2 = train_model(model2, train_X, train_y, val_X, val_y, epochs=5 )
%matplotlib inline
warnings.filterwarnings("ignore" )<load_from_csv>
alt_pred_val_y = model2.predict(val_X, batch_size=1024, verbose=1 )
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 df_test = pd.read_csv("/kaggle/input/titanic/test.csv")
 test = df_test.copy()
gender_submission = pd.read_csv("/kaggle/input/titanic/gender_submission.csv" )<sort_values>
f1, threshold = f1_smart(val_y, pred_val_y)
 print('Optimal F1: {} at threshold: {}'.format(f1, threshold))
train.isnull().sum().sort_values(ascending = False )<count_missing_values>
f1, alt_threshold = f1_smart(val_y, alt_pred_val_y)
 print('Optimal F1: {} at threshold: {}'.format(f1, alt_threshold))
test.isnull().sum().sort_values(ascending = False )<count_missing_values>
long_pred_val_y = model.predict(long_val_X, batch_size=1024, verbose=1 )
train.Pclass.isnull().sum()<count_values>
def update_preds(pred_val_y, long_pred_val_y, long_id_list):
global threshold
threshold_range = 0.10
copy_pred_val_y = pred_val_y
for i, pred in enumerate(list(pred_val_y)) :
if(pred <(threshold + threshold_range)) and(pred >(threshold - threshold_range)) :
count = 1
sum_pred = pred
for long_id, long_pred in zip(long_id_list, long_pred_val_y.tolist()):
if long_id == i:
if(long_pred >(threshold + threshold_range)) or(long_pred <(threshold - threshold_range)) :
if long_pred[0] > pred:
copy_pred_val_y[i] = long_pred[0]
return copy_pred_val_y
train.Name.value_counts()<drop_column>
pred_val_y = update_preds(pred_val_y, long_pred_val_y, long_id_list )
train.drop(columns = ["Name","PassengerId"], axis = 1, inplace = True)
 test.drop(columns = ["Name","PassengerId"], axis = 1, inplace = True )<count_missing_values>
f1, threshold = f1_smart(val_y, pred_val_y)
 print('Optimal F1: {} at threshold: {}'.format(f1, threshold))
train.Sex.isnull().sum()<count_missing_values>
all_train_X = np.concatenate(( train_X,val_X),axis=0)
 all_train_y = np.concatenate(( train_y,val_y),axis=0)
 all_train_X = np.concatenate(( train_X,generalized_train_X),axis=0)
 all_train_y = np.concatenate(( train_y,generalized_train_y),axis=0)
 model = train_model(model, train_X, train_y, val_X, val_y, epochs=5 )
train.Age.isnull().sum()<count_values>
def seq_ensemble(pred_y, alt_pred_y):
global threshold, alt_threshold
threshold_range_list = [0.15,0.12,0.10,0.08,0.05,0.03,0.02,0.01]
copy_pred_val_y = pred_val_y
for threshold_range in threshold_range_list:
for i, pred_pair in enumerate(zip(list(pred_val_y),list(alt_pred_y))):
if(pred_pair[0] <(threshold + threshold_range)) and \ 
(pred_pair[0] >(threshold - threshold_range)) :
if(pred_pair[1] >(alt_threshold + threshold_range)) or \ 
(pred_pair[1] <(alt_threshold - threshold_range)) :
copy_pred_val_y[i] = pred_pair[1]
return copy_pred_val_y
train.Ticket.value_counts()<count_unique_values>
pred_val_y = seq_ensemble(pred_val_y, alt_pred_val_y )
print("There are {} unique Ticket values.".format(len(train.Ticket.unique())) )<drop_column>
f1, threshold = f1_smart(val_y, pred_val_y)
 print('Optimal F1: {} at threshold: {}'.format(f1, threshold))
train.drop("Ticket", axis = 1, inplace = True)
 test.drop("Ticket", axis = 1, inplace = True )<drop_column>
pred_test_y = model.predict(test_X, batch_size=1024, verbose=1 )
train.drop("Fare", axis = 1, inplace = True)
 test.drop("Fare", axis = 1, inplace = True )<count_values>
long_pred_test_y = model.predict(long_test_X, batch_size=1024, verbose=1 )
train.Cabin.value_counts()<drop_column>
pred_test_y = update_preds(pred_test_y, long_pred_test_y, long_test_id )
train.drop("Cabin", axis = 1, inplace = True)
 test.drop("Cabin", axis =1, inplace = True )<count_values>
pred_test_y =(pred_test_y>threshold ).astype(int)
 out_df = pd.DataFrame({"qid":test_df["qid"].values})
 out_df['prediction'] = pred_test_y
out_df.to_csv("submission.csv", index=False )
<count_missing_values><EOS> 
pred_goofy_y = model.predict(goofy_test_X, batch_size=10, verbose=1)
 f1, threshold = f1_smart(goofy_test_y, pred_goofy_y)
 print('Optimal F1: {} at threshold: {}'.format(f1, threshold))
for question,pred in zip(goofy_test_questions, pred_goofy_y):
print(pred, " ", question, "
" )