Kaggle competition: Santander Customer Satisfaction
<SOS> metric: AUC Kaggle data source: santander-customer-satisfaction<choose_model_class>
print(os.listdir(".. /input"))
model[4]=Sequential()
model[4].add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu', input_shape=(28,28,1)))
 model[4].add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
model[4].add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
model[4].add(MaxPool2D(pool_size=2))
model[4].add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
model[4].add(Conv2D(filters=192, kernel_size=3, padding='same', activation='relu'))
model[4].add(MaxPool2D(pool_size=2))
model[4].add(Conv2D(filters=192, kernel_size=5, padding='same', activation='relu'))
model[4].add(MaxPool2D(pool_size=2, padding='same'))
model[4].add(Flatten())
 model[4].add(Dense(256, activation='relu'))
model[4].add(Dense(10, activation='softmax'))
model[4].compile(optimizer=optimizer,loss="categorical_crossentropy",metrics=['accuracy'] )<choose_model_class>
class EstimatorSelectionHelper:
def __init__(self, models, params):
if not set(models.keys() ).issubset(set(params.keys())) :
missing_params = list(set(models.keys())- set(params.keys()))
raise ValueError("Some estimators are missing parameters: %s" % missing_params)
 self.models = models
self.params = params
self.keys = models.keys()
self.grid_searches = {}
def fit(self, X, y, cv=3, n_jobs=3, verbose=1, scoring=None, refit=False):
for key in self.keys:
print("Running GridSearchCV for %s." % key)
 model = self.models[key]
params = self.params[key]
gs = GridSearchCV(model, params, cv=cv, n_jobs=n_jobs,
verbose=verbose, scoring=scoring, refit=refit,
return_train_score=True)
 gs.fit(X,y)
 self.grid_searches[key] = gs
def score_summary(self, sort_by='mean_score'):
def row(key, scores, params):
d = {
'estimator': key,
'min_score': min(scores),
'max_score': max(scores),
'mean_score': np.mean(scores),
'std_score': np.std(scores),
}
return pd.Series({**params,**d})
 rows = []
for k in self.grid_searches:
print(k)
 params = self.grid_searches[k].cv_results_['params']
scores = []
for i in range(self.grid_searches[k].cv):
key = "split{}_test_score".format(i)
 r = self.grid_searches[k].cv_results_[key]
scores.append(r.reshape(len(params),1))
all_scores = np.hstack(scores)
 for p, s in zip(params,all_scores):
rows.append(( row(k, s, p)))
 df = pd.concat(rows, axis=1 ).T.sort_values([sort_by], ascending=False)
 columns = ['estimator', 'min_score', 'mean_score', 'max_score', 'std_score']
columns = columns + [c for c in df.columns if c not in columns]
return df[columns]
model[5]=Sequential()
model[5].add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu', input_shape=(28,28,1)))
 model[5].add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
model[5].add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
model[5].add(MaxPool2D(pool_size=2))
model[5].add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
model[5].add(Conv2D(filters=192, kernel_size=3, padding='same', activation='relu'))
model[5].add(MaxPool2D(pool_size=2))
model[5].add(Conv2D(filters=192, kernel_size=5, padding='same', activation='relu'))
model[5].add(MaxPool2D(pool_size=2, padding='same'))
model[5].add(Flatten())
 model[5].add(Dense(256, activation='relu'))
model[5].add(Dense(10, activation='softmax'))
model[5].compile(optimizer=optimizer,loss="categorical_crossentropy",metrics=['accuracy'] )<init_hyperparams>
train_data = pd.read_csv(".. /input/train.csv")
 test_data = pd.read_csv(".. /input/test.csv")
 remove = []
c = train_data.columns
for i in range(len(c)-1):
v = train_data[c[i]].values
for j in range(i+1,len(c)) :
if np.array_equal(v,train_data[c[j]].values):
remove.append(c[j])
 train_data.drop(remove, axis=1, inplace=True)
 train_X, test_X, train_y, test_y = train_test_split(train_data.drop(["TARGET","ID"],axis=1),
train_data.TARGET,
random_state=0,
test_size=0.25 )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<choose_model_class>
models1 = {
'ExtraTreesClassifier': ExtraTreesClassifier() ,
'RandomForestClassifier': RandomForestClassifier() ,
'AdaBoostClassifier': AdaBoostClassifier() ,
'GradientBoostingClassifier': GradientBoostingClassifier() ,
'XGBClassifier': XGBClassifier()
}
params1 = {
'ExtraTreesClassifier': { 'n_estimators': [64, 128, 256, 512, 1024] },
'RandomForestClassifier': { 'n_estimators': [64, 128, 256, 512, 1024], 'min_samples_split': [2, 4, 8, 16] },
'AdaBoostClassifier': { 'n_estimators': [64, 128, 256, 512, 1024] },
'GradientBoostingClassifier': { 'n_estimators': [64, 128, 256, 512, 1024], 'learning_rate': [0.8, 1.0] },
'XGBClassifier': { 'n_estimators': [64, 128, 256, 512, 1024], 'max_depth': [1, 2, 3, 4, 5, 6]}
}
helper1 = EstimatorSelectionHelper(models1, params1)
 helper1.fit(train_X[important_features], train_y, scoring='roc_auc', n_jobs=2, refit= True)
 helper1.score_summary(sort_by='mean_score' )
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x )<define_variables>
clf = helper1.grid_searches.get('XGBClassifier' ).best_estimator_
print(helper1.grid_searches.get('XGBClassifier' ).best_params_ )
<train_model><EOS> 
clf.fit(train_data.drop(["TARGET","ID"],axis=1)[important_features], train_data.TARGET)
 submission_preds = clf.predict_proba(test_data[important_features])[:,1]
output = pd.DataFrame({'ID': test_data.ID,
'TARGET': submission_preds})
 output.to_csv('submission.csv', index=False )