Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<categorify>
print(os.listdir(".. /input"))
seed = 4529
np.random.seed(seed )
list_category_features = ['TicketClass','Sex','Age','TicketFare','Embarked','Title']
dummy_features = pd.get_dummies(full[list_category_features], columns=list_category_features)
 full.drop(list_category_features, axis=1,inplace=True)
 full = pd.concat([full, dummy_features], axis=1)
 full.sample(10 )<split>
base_dir = os.path.join(".. ", "input")
 train_df = pd.read_csv(os.path.join(base_dir, "train.csv"))
train_dir = os.path.join(base_dir, "train/train")
 test_dir = os.path.join(base_dir, "test/test")
 print(train_df.head() )
train_df_new = full.iloc[:891]
y = train_df_new['Survived']
X = train_df_new.drop(['Survived'], axis=1)
 test_df_new = full.iloc[891:]
test_df_new = test_df_new.drop(['Survived'], axis=1)
 X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size=0.3,
random_state=42)
 print(X_train.shape, X_test.shape )<choose_model_class>
%load_ext tensorboard.notebook
%tensorboard --logdir logs
logistic = LogisticRegression()
logistic.fit(X_train, y_train)
 y_pred = logistic.predict(X_test)
 print('Logistic Regression model score:',
np.round(logistic.score(X_test, y_test), 3))<compute_test_metric>
def model_report(y_test, y_pred):
print('Confusion Matrix:
',
metrics.confusion_matrix(y_true=y_test,
y_pred=y_pred,
labels=[0, 1]))
print('{:-^30}'.format('|'))
print('{:15}{:.3f}'.format('Accuracy:',
metrics.accuracy_score(y_test,
y_pred)))
 print('{:-^30}'.format('|'))
print('{:15}0:{:.3f}|1:{:.3f}'.format('Precision:',
metrics.precision_score(y_test,y_pred,average=None)[0],
metrics.precision_score(y_test,y_pred,average=None)[1]))
print('{:-^30}'.format('|'))
print('{:15}0:{:.3f}|1:{:.3f}'.format('Recall:',
metrics.recall_score(y_test,y_pred,average=None)[0],
metrics.recall_score(y_test,y_pred,average=None)[1]))
print('{:-^30}'.format('|'))
print('{:15}0:{:.3f}|1:{:.3f}'.format('f1-score:',
metrics.f1_score(y_test,y_pred,average=None)[0],
metrics.f1_score(y_test,y_pred,average=None)[1]))
print('{:-^30}'.format('|'))
model_report(y_test, y_pred )<choose_model_class>
train_df['has_cactus'] = train_df['has_cactus'].astype(str)
 batch_size = 64
train_size = 15750
validation_size = 1750
datagen = ImageDataGenerator(
 rescale=1./255,
horizontal_flip=True,
vertical_flip=False,
brightness_range=(1, 1.3),
shear_range=0.05,
validation_split=0.1)
 data_args = {
"dataframe": train_df,
"directory": train_dir,
"x_col": 'id',
"y_col": 'has_cactus',
"shuffle": True,
"target_size":(32, 32),
"batch_size": batch_size,
"class_mode": 'binary'
}
train_generator = datagen.flow_from_dataframe(**data_args, subset='training')
 validation_generator = datagen.flow_from_dataframe(**data_args, subset='validation' )
model = LogisticRegression()
model.fit(X_train, y_train)
 predicted = model.predict(X_test)
 report = classification_report(y_test, predicted, digits=3)
 print(report )<define_search_model>
model = Sequential([
Conv2D(64,(3,3), padding='same', activation="relu", input_shape=(32, 32, 3)) ,
BatchNormalization() ,
Conv2D(64,(3,3), padding='same', activation="relu"),
BatchNormalization() ,
Conv2D(64,(3,3), padding='same', activation="relu"),
BatchNormalization() ,
MaxPooling2D(2,2),
Dropout(0.4),
Conv2D(128,(3,3), padding='same', activation="relu"),
BatchNormalization() ,
Conv2D(128,(3,3), padding='same', activation="relu"),
BatchNormalization() ,
Conv2D(128,(3,3), padding='same', activation="relu"),
BatchNormalization() ,
MaxPooling2D(2,2),
Dropout(0.4),
Conv2D(256,(3,3), padding='same', activation="relu"),
BatchNormalization() ,
Conv2D(256,(3,3), padding='same', activation="relu"),
BatchNormalization() ,
MaxPooling2D(2,2),
Dropout(0.4),
Conv2D(256,(3,3), padding='same', activation="relu"),
BatchNormalization() ,
Conv2D(256,(3,3), padding='same', activation="relu"),
BatchNormalization() ,
MaxPooling2D(2,2),
Dropout(0.4),
GlobalAveragePooling2D() ,
Dense(units=256, activation='relu'),
Dropout(0.5),
Dense(units=256, activation='relu'),
Dropout(0.5),
Dense(units=1, activation='sigmoid')
 ])
 model.compile(optimizer=Adam(lr=0.001),
loss='binary_crossentropy',
metrics=['acc'])
 model.summary()
kfold = model_selection.KFold(n_splits=10, random_state=0)
 model = LogisticRegression()
scoring = 'accuracy'
results = model_selection.cross_val_score(model,
X,
y,
cv=kfold,
scoring=scoring)
 print("Average Accuracy: {:.3f}".format(results.mean()))<choose_model_class>
ckpt_path = 'aerial_cactus_detection.hdf5'
earlystop = EarlyStopping(monitor='val_acc', patience=10, verbose=1, restore_best_weights=True)
 reducelr = ReduceLROnPlateau(monitor='val_acc', factor=0.5, patience=3, verbose=1, min_lr=1.e-6)
 modelckpt_cb = ModelCheckpoint(ckpt_path, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
 tb = TensorBoard()
callbacks = [earlystop, reducelr, modelckpt_cb, tb]
names = ["Nearest Neighbors",
"Linear SVM",
"RBF SVM",
"Gaussian Process",
"Decision Tree",
"Random Forest",
"Neural Net",
"AdaBoost",
"Naive Bayes",
"Logistic Regression"]
classifiers = [
KNeighborsClassifier(3),
SVC(kernel="linear", C=0.025),
SVC(gamma=2, C=1),
GaussianProcessClassifier(1.0 * RBF(1.0)) ,
DecisionTreeClassifier(max_depth=5),
RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
MLPClassifier(alpha=1),
AdaBoostClassifier() ,
GaussianNB() ,
LogisticRegression() ]
results = {}
for name, clf in zip(names, classifiers):
results[name] = {}
results[name]['Score'] = model_selection.cross_val_score(clf,
X,
y,
cv=kfold,
scoring=scoring ).mean()
results[name]['Confusion_matrix'] = metrics.confusion_matrix(y,
model_selection.cross_val_predict(clf,
X,
y,
cv=kfold))<sort_values>
history = model.fit_generator(train_generator,
validation_data=validation_generator,
steps_per_epoch=train_size//batch_size,
validation_steps=validation_size//batch_size,
epochs=100, verbose=1,
shuffle=True,
callbacks=callbacks )
for clf_name in sorted(names, key=lambda x:(results[x]['Score'])) :
print('{:19} :{:.3f}'.format(clf_name, results[clf_name]['Score']))<train_on_grid>
test_df = pd.read_csv(os.path.join(base_dir, "sample_submission.csv"))
print(test_df.head())
 test_images = []
images = test_df['id'].values
for image_id in images:
test_images.append(cv2.imread(os.path.join(test_dir, image_id)))
 test_images = np.asarray(test_images)
 test_images = test_images / 255.0
print("Number of Test set images: " + str(len(test_images)) )
hyper_params={'max_depth':range(5,21,5),
'min_samples_split':range(2,9,2),
'min_samples_leaf':range(1,6,1),
'max_leaf_nodes':range(2,11,1)}
grid = GridSearchCV(RandomForestClassifier(random_state=1),
param_grid=hyper_params)
 grid.fit(X,y)
 print('Best Score: {:.4f}'.format(grid.best_score_))
print('Best Parameters setting:',grid.best_params_ )<train_model>
pred = model.predict(test_images)
 test_df['has_cactus'] = pred
test_df.to_csv('aerial-cactus-submission.csv', index = False )