Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<choose_model_class>
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from skimage.io import imread
from keras.applications import resnet50
from keras.optimizers import Adam
from keras.layers import GlobalAveragePooling2D, Dense
from keras.models import Model
from keras.callbacks import ModelCheckpoint
_, NUMBER_OF_FEATURES = x_train.shape
def create_network() :
network = Sequential()
network.add(Dense(units = 9, kernel_initializer = 'uniform', activation = 'relu', input_shape=(NUMBER_OF_FEATURES,)))
 network.add(Dense(units = 9, kernel_initializer = 'uniform', activation = 'relu'))
network.add(Dense(units = 5, kernel_initializer = 'uniform', activation = 'relu'))
network.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
network.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
 return network
NN_classifier = KerasClassifier(build_fn=create_network,
epochs=200,
batch_size=32,
verbose=1)
 classifiers.append(NN_classifier )<find_best_model_class>
train = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 test = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 y_train = np.array(train.has_cactus )
def evaluate_with_cross_validation(classifiers, test_size, split_num, X, y):
ret_results = {}
cv_results = []
for classifier in tqdm(classifiers):
clf_name = classifier_name(classifier)
 print('cross validate with {0}'.format(clf_name))
n_jobs = None if clf_name in ['KerasClassifier'] else -1
cv_results.append(cross_val_score(classifier, X, y = y, scoring = "accuracy", cv = kfold, n_jobs=n_jobs))
cv_means = []
cv_std = []
for index, cv_result in enumerate(cv_results):
mean_accuracy = cv_result.mean()
std = cv_result.std()
cv_means.append(mean_accuracy)
 cv_std.append(std)
 classifier = classifiers[index]
clf_name = classifier_name(classifier)
 ret_results[clf_name] =(mean_accuracy, classifier)
 print('{0:30s}: {1:.4f}(±{2:.4f})'.format(clf_name, mean_accuracy, std))
cv_res = pd.DataFrame({"CrossValMeans":cv_means,"CrossValerrors": cv_std,"Algorithm": list(ret_results.keys())})
 g = sns.barplot("CrossValMeans","Algorithm",data = cv_res, palette="Set3",orient = "h",**{'xerr':cv_std})
 g.set_xlabel("Mean Accuracy")
 g = g.set_title("Cross validation scores")
 return ret_results
split_num = 10
test_size = 0.2
classifier_results = evaluate_with_cross_validation(classifiers, test_size, split_num, x_train, y_train )<choose_model_class>
X_train = []
for name in train.id:
X_train.append(imread('.. /input/aerial-cactus-identification/train/train/'+name))
DTC = DecisionTreeClassifier()
adaDTC = AdaBoostClassifier(DTC, random_state=7)
 ada_param_grid = {"base_estimator__criterion" : ["gini", "entropy"],
"base_estimator__splitter" : ["best", "random"],
"algorithm" : ["SAMME","SAMME.R"],
"n_estimators" :[1,2],
"learning_rate": [0.0001, 0.001, 0.01, 0.1, 0.2, 0.3,1.5]}
gsadaDTC = GridSearchCV(adaDTC,param_grid = ada_param_grid, cv=kfold, scoring="accuracy", n_jobs=-1, verbose = 1)
 gsadaDTC.fit(x_train,y_train)
 ada_best = gsadaDTC.best_estimator_
ada_accuracy = gsadaDTC.best_score_
classifier_results['ada_best'] =(ada_accuracy, ada_best)
 ada_accuracy<define_search_space>
X_test = []
for name in test.id:
X_test.append(imread('.. /input/aerial-cactus-identification/test/test/'+name))
ExtC = ExtraTreesClassifier()
ex_param_grid = {"max_depth": [None],
"max_features": [1, 3, 10],
"min_samples_split": [2, 3, 10],
"min_samples_leaf": [1, 3, 10],
"bootstrap": [False],
"n_estimators" :[100,300],
"criterion": ["gini"]}
gsExtC = GridSearchCV(ExtC,param_grid = ex_param_grid, cv=kfold, scoring="accuracy", n_jobs=-1, verbose = 1)
 gsExtC.fit(x_train,y_train)
 ext_best = gsExtC.best_estimator_
ext_accuracy = gsExtC.best_score_
classifier_results['ext_best'] =(ext_accuracy, ext_best)
 ext_accuracy<train_on_grid>
X_train = np.array(X_train)
 X_test = np.array(X_test )
RFC = RandomForestClassifier()
rf_param_grid = {"max_depth": [None],
"max_features": [1, 3, 10],
"min_samples_split": [2, 3, 10],
"min_samples_leaf": [1, 3, 10],
"bootstrap": [False],
"n_estimators" :[100,300],
"criterion": ["gini"]}
gsRFC = GridSearchCV(RFC,param_grid = rf_param_grid, cv=kfold, scoring="accuracy", n_jobs=-1, verbose = 1)
 gsRFC.fit(x_train,y_train)
 RFC_best = gsRFC.best_estimator_
rfc_accuracy = gsRFC.best_score_
classifier_results['RFC_best'] =(rfc_accuracy, RFC_best)
 rfc_accuracy<train_on_grid>
X_train = resnet50.preprocess_input(X_train)
 X_test = resnet50.preprocess_input(X_test )
GBC = GradientBoostingClassifier()
gb_param_grid = {'loss' : ["deviance"],
'n_estimators' : [100,200,300],
'learning_rate': [0.1, 0.05, 0.01],
'max_depth': [4, 8],
'min_samples_leaf': [100,150],
'max_features': [0.3, 0.1]
}
gsGBC = GridSearchCV(GBC,param_grid = gb_param_grid, cv=kfold, scoring="accuracy", n_jobs=-1, verbose = 1)
 gsGBC.fit(x_train,y_train)
 GBC_best = gsGBC.best_estimator_
gs_accuracy = gsGBC.best_score_
classifier_results['GBC_best'] =(gs_accuracy, GBC_best)
 gs_accuracy<train_on_grid>
base_model = resnet50.ResNet50(include_top=False, weights='imagenet' )
SVMC = SVC(probability=True)
 svc_param_grid = {'kernel': ['rbf'],
'gamma': [ 0.001, 0.01, 0.1, 1],
'C': [1, 10, 50, 100,200,300, 1000]}
gsSVMC = GridSearchCV(SVMC,param_grid = svc_param_grid, cv=kfold, scoring="accuracy", n_jobs=-1, verbose = 1)
 gsSVMC.fit(x_train,y_train)
 SVMC_best = gsSVMC.best_estimator_
gsSVMC_accuracy = gsSVMC.best_score_
classifier_results['SVMC_best'] =(gsSVMC_accuracy, SVMC_best)
 gsSVMC_accuracy<find_best_params>
x = base_model.output
x = GlobalAveragePooling2D()(x)
 x = Dense(1024, activation='relu' )(x)
 predictions = Dense(1, activation='sigmoid' )(x)
 model = Model(inputs = base_model.input, outputs = predictions )
enable_classifiers_num = 5
enable_classifiers = sorted(classifier_results.items() , key=lambda kv: kv[1][0], reverse=True)[:enable_classifiers_num]
print('Top {0:,} classifiers'.format(enable_classifiers_num ).upper())
 for clf_name, tup in enable_classifiers:
print('\t{0:30s}: {1:.5f}'.format(clf_name, tup[0]))<train_on_grid>
model.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.0001), metrics=['accuracy'] )
estimators = [(n, tup[1])for n, tup in enable_classifiers]
vc_params = {
'estimators': estimators,
'voting': 'soft',
'n_jobs': -1,
'flatten_transform': True
}
voting_classifier = VotingClassifier(**vc_params)
 voting_classifier = voting_classifier.fit(x_train, y_train)
 cv_result = cross_val_score(voting_classifier, x_train, y = y_train, scoring = "accuracy", cv = kfold, n_jobs=-1)
 print('{0:30s}: {1:.4f}(±{2:.4f})'.format(classifier_name(voting_classifier), cv_result.mean() , cv_result.std()))<save_to_csv>
model.load_weights('.. /input/weights/model.hdf5' )
best_classifier = voting_classifier
best_classifier.fit(x_train, y_train)
 predictions = best_classifier.predict(x_test)
 submission_file = pd.DataFrame({ 'PassengerId': PassengerId,
'Survived': predictions })
 ts = datetime.now().strftime("%Y%m%d%H%M")
 filename = 'submission_file_{0}.csv'.format(ts)
 submission_file.to_csv(filename, index=False)
 submission_file<set_options>
model.fit(X_train[:128], y_train[:128], epochs=30 )
%matplotlib inline
sns.set_style('whitegrid')
 sns.set_context('talk')
 params = {'legend.fontsize': 'x-large',
'figure.figsize':(30, 10),
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
plt.rcParams.update(params)
<load_from_csv>
checkpoint = ModelCheckpoint(filepath='model.hdf5')
 model.fit(X_train, y_train, epochs=100, batch_size=128, callbacks=[checkpoint] )
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 print(train_df.shape)
 print(test_df.shape )<concatenate>
sample = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv' )
test_df['Survived'] = 0
full = pd.concat([train_df, test_df],
axis=0,
ignore_index=True,
sort=False )<rename_columns>
sample.has_cactus = model.predict(X_test )
full.rename(columns={'Pclass':'TicketClass',
'SibSp':'Sibling_Spouse',
'Parch':'Parent_Children',
'Fare':'TicketFare'},
inplace=True )<sort_values>
sample.to_csv('sample_submission.csv', index=False )