Kaggle competition: Plant Seedlings Classification
<SOS> metric: MeanFScore Kaggle data source: plant-seedlings-classification<train_model>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from glob import glob
import cv2
import keras.backend as k
import tensorflow as tf
import os
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from keras.utils import np_utils
lr_c=LogisticRegression(C=1, class_weight={0:0.62, 1:0.38}, max_iter=5000,
penalty='l2',
random_state=None, solver='lbfgs', verbose=0,
warm_start=True)
 lr_c.fit(x_train,y_train.ravel())
 lr_pred=lr_c.predict(x_test)
 lr_ac=accuracy_score(y_test.ravel() , lr_pred)
 print('LogisticRegression_accuracy test:',lr_ac)
 print("AUC",roc_auc_score(y_test.ravel() , lr_pred))<compute_train_metric>
images_path = '.. /input/train/*/*.png'
images = glob(images_path)
 train_images = []
train_labels = []
for img in images:
train_images.append(cv2.resize(cv2.imread(img),(70, 70)))
 train_labels.append(img.split('/')[-2])
 train_X = np.asarray(train_images)
 train_Y = pd.DataFrame(train_labels )
cv = StratifiedShuffleSplit(n_splits = 15, test_size =.25, random_state = 0)
 x = scaler.fit_transform(x)
 accuracies = cross_val_score(LogisticRegression(solver='liblinear',class_weight={0:0.62, 1:0.38}), x,y, cv = cv)
 print("CV accuracy on 15 chunks: {}".format(accuracies))
print("Mean CV accuracy: {}".format(round(accuracies.mean() ,5)) )<compute_train_metric>
encoder = LabelEncoder()
encoder.fit(train_Y[0])
 encoded_labels = encoder.transform(train_Y[0])
 categorical_labels = np_utils.to_categorical(encoded_labels )
svm=SVC(kernel='rbf',C=10,gamma=0.1)
 svm.fit(x_train,y_train.ravel())
 svm_pred=svm.predict(x_test)
 print('Accuracy is ',metrics.accuracy_score(svm_pred,y_test.ravel()))
print('AUC: ',roc_auc_score(y_test.ravel() , svm_pred))<train_on_grid>
x_train,x_test,y_train,y_test=train_test_split(train_X,categorical_labels,test_size=0.25,random_state=7)
SVMC = SVC(probability=True,class_weight={0:0.62, 1:0.38})
 svc_param_grid = {'kernel': ['rbf'],
'gamma': [ 0.001,0.008, 0.01,0.02,0.05, 0.1, 1],
'C': [1, 10, 12, 14, 20, 25, 30, 40,50,60, 100,200,300, 1000]}
gsSVMC = GridSearchCV(SVMC,param_grid = svc_param_grid, cv=5, scoring="accuracy", n_jobs=-1)
 gsSVMC.fit(x_train,y_train)
 SVMC_best = gsSVMC.best_estimator_
SVMC_best.fit(x_train, y_train.ravel())
 pred_svm = SVMC_best.predict(x_test)
 acc_svm = accuracy_score(y_test.ravel() , pred_svm)
 print('Accuracy', acc_svm)
 print('AUC: ',roc_auc_score(y_test.ravel() , pred_svm))
print(SVMC_best )<compute_train_metric>
import keras
from keras import layers
from keras.layers import Input, Dense, Activation,ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.vgg16 import VGG16
rdf_c=RandomForestClassifier(n_estimators=100,criterion='entropy',random_state=0)
 rdf_c.fit(x_train,y_train.ravel())
 rdf_pred=rdf_c.predict(x_test)
 rdf_ac=accuracy_score(rdf_pred,y_test.ravel())
 print('Accuracy of random forrest classifier: ',rdf_ac)
 print('AUC: ',roc_auc_score(y_test.ravel() , rdf_pred))<train_on_grid>
base_model = VGG16(include_top=False, weights='imagenet', input_shape=(70, 70, 3))
knn_clf = KNeighborsClassifier()
parameters_knn = {"n_neighbors": [3, 5, 10, 15], "weights": ["uniform", "distance"], "algorithm": ["auto", "ball_tree", "kd_tree"],
"leaf_size": [20, 30, 50]}
grid_knn = GridSearchCV(knn_clf, parameters_knn, scoring='accuracy', cv=5, n_jobs=-1)
 grid_knn.fit(x_train, y_train)
 knn_clf = grid_knn.best_estimator_
knn_clf.fit(x_train, y_train.ravel())
 pred_knn = knn_clf.predict(x_test)
 acc_knn = accuracy_score(y_test.ravel() , pred_knn)
 print("The accuracy of KNeighbors is: " + str(acc_knn))
print('AUC: ',roc_auc_score(y_test.ravel() , pred_knn))<train_on_grid>
model = Sequential()
model.add(base_model)
 model.add(layers.Flatten())
 model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(12, activation='sigmoid'))
xg_clf = XGBClassifier()
parameters_xg = {"objective" : ["reg:linear"], "n_estimators" : [5, 10, 15, 20]}
grid_xg = GridSearchCV(xg_clf, parameters_xg, scoring='accuracy',cv=5,n_jobs=-1)
 grid_xg.fit(x_train, y_train)
 xg_clf = grid_xg.best_estimator_
xg_clf.fit(x_train, y_train.ravel())
 pred_xg = xg_clf.predict(x_test)
 acc_xg = accuracy_score(y_test.ravel() , pred_xg)
 print("The accuracy of XGBoost is: " + str(acc_xg))
print('AUC: ',roc_auc_score(y_test.ravel() , pred_xg))<compute_train_metric>
opt = keras.optimizers.adam(lr=0.0001, decay=1e-6)
 model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'] )
nr.seed(1115)
 nn_mod = MLPClassifier(hidden_layer_sizes =(100,100,), max_iter=1750, solver='sgd')
 nn_mod.fit(x_train, y_train.ravel())
 scores = nn_mod.predict(x_test)
 nnacc = accuracy_score(y_test.ravel() , scores)
 print('The accuracy of MLP classifier: ',nnacc)
 print('AUC: ',roc_auc_score(y_test.ravel() , scores))<choose_model_class>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
rotation_range=0,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
vertical_flip=False )
Bagg_estimators = [10,25,50,75,100,150,250];
cv = StratifiedShuffleSplit(n_splits=10, test_size=.33, random_state=15)
 parameters = {'n_estimators':Bagg_estimators }
gridBG = GridSearchCV(BaggingClassifier(base_estimator= None,
bootstrap_features=False),
param_grid=parameters,
cv=cv,
n_jobs = -1)
 bg_mod=gridBG.fit(x_train,y_train.ravel())
 bg_pred = bg_mod.predict(x_test)
 bg_acc = accuracy_score(y_test.ravel() , bg_pred)
 print("Bagging accuracy: ", bg_acc)
 print('AUC: ',roc_auc_score(y_test.ravel() , bg_pred))<define_search_space>
datagen.fit(x_train )
n_estimators = [100,140,145,150,160, 170,175,180,185];
cv = StratifiedShuffleSplit(n_splits=10, test_size=.30, random_state=15)
 learning_rate = [0.1,1,0.01,0.5]
parameters = {'n_estimators':n_estimators,
'learning_rate':learning_rate}
gridAda = GridSearchCV(AdaBoostClassifier(base_estimator= None, 
),
param_grid=parameters,
cv=cv,
n_jobs = -1)
 ada_mod= gridAda.fit(x_train,y_train.ravel())
 ada_pred = ada_mod.predict(x_test)
 ada_acc = accuracy_score(y_test.ravel() , ada_pred)
 print("Adaboost accuracy: ", ada_acc)
 print('AUC: ',roc_auc_score(y_test.ravel() , ada_pred))<compute_train_metric>
model.fit_generator(datagen.flow(x_train, y_train,
batch_size=50),
steps_per_epoch=x_train.shape[0],
epochs=1,
validation_data=(x_test, y_test),
verbose=1)
gradient_boost = GradientBoostingClassifier()
gradient_boost.fit(x_train, y_train.ravel())
 ygbc_pred = gradient_boost.predict(x_test)
 gradient_accy = round(accuracy_score(ygbc_pred, y_test.ravel()), 3)
 print('Gradient Boosting accuracy: ',gradient_accy)
 print('AUC: ',roc_auc_score(y_test.ravel() , ygbc_pred)) 
<compute_train_metric>
[loss, accuracy] = model.evaluate(x_test, y_test )
ExtraTreesClassifier = ExtraTreesClassifier()
ExtraTreesClassifier.fit(x_train, y_train.ravel())
 y_pred = ExtraTreesClassifier.predict(x_test)
 extraTree_accy = accuracy_score(y_pred, y_test.ravel())
 print('ExtraTrees classifier accuracy: ',extraTree_accy)
 print('AUC: ',roc_auc_score(y_test.ravel() , y_pred))<compute_train_metric>
print('Test Set Accuracy: '+str(accuracy*100)+"%");
GaussianProcessClassifier = GaussianProcessClassifier()
GaussianProcessClassifier.fit(x_train, y_train.ravel())
 yg_pred = GaussianProcessClassifier.predict(x_test)
 gau_pro_accy = accuracy_score(yg_pred, y_test.ravel())
 print('Gaussian process classifier: ', gau_pro_accy)
 print('AUC: ',roc_auc_score(y_test.ravel() , yg_pred))<train_model>
test_images_path = '.. /input/test/*.png'
test_images = glob(test_images_path)
 test_images_arr = []
test_files = []
for img in test_images:
test_images_arr.append(cv2.resize(cv2.imread(img),(70, 70)))
 test_files.append(img.split('/')[-1])
 test_X = np.asarray(test_images_arr )
voting_classifier = VotingClassifier(estimators=[ 
('gradient_boosting', gradient_boost), 
('bagging_classifier', bg_mod), 
('ada_classifier',ada_mod), 
('XGB_Classifier', xg_clf), 
('gaussian_process_classifier', GaussianProcessClassifier)
 ],voting='hard')
 voting_classifier = voting_classifier.fit(x_train,y_train.ravel() )<compute_train_metric>
predictions = model.predict(test_X )
vote_pred = voting_classifier.predict(x_test)
 voting_accy = round(accuracy_score(vote_pred, y_test.ravel()), 4)
 print('Voting accuracy of the combined classifiers: ',voting_accy)
 print('AUC: ',round(roc_auc_score(y_test.ravel() , vote_pred), 4)) 
<compute_test_metric>
preds = np.argmax(predictions, axis=1)
 pred_str = encoder.classes_[preds]
<predict_on_test><EOS> 
final_predictions = {'file':test_files, 'species':pred_str}
final_predictions = pd.DataFrame(final_predictions)
 final_predictions.to_csv("submission.csv", index=False )