Kaggle competition: Aerial Cactus Identification
RF.fit(x_train,x_test)
 model7pred = RF.predict(y_train)
 submission7 = pd.DataFrame(columns = ['PassengerId','Survived'])
 submission7['PassengerId'] = result['PassengerId']
submission7['Survived'] = model7pred
submission7.to_csv('RandomForest(No HT ).csv',index = False )<compute_train_metric>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import scipy
import cv2
import keras
GBC = GradientBoostingClassifier(random_state = 5)
 estimator.append(( 'GBC',GradientBoostingClassifier(random_state = 5)))
 cv = cross_val_score(GBC,x_train,x_test,cv=10)
 accuracy8 = cv.mean()
accuracy.append(accuracy8)
 print(cv)
 print(cv.mean() )<train_model>
import random
GBC.fit(x_train,x_test)
 GBC.score(y_train,y_test )<save_to_csv>
train_data = pd.read_csv('.. /input/train.csv' )
GBC.fit(x_train,x_test)
 model8pred = GBC.predict(y_train)
 submission8 = pd.DataFrame(columns = ['PassengerId','Survived'])
 submission8['PassengerId'] = result['PassengerId']
submission8['Survived'] = model8pred
submission8.to_csv('GradientBoosting(No HT ).csv',index = False )<compute_train_metric>
train_data.has_cactus.value_counts()
XGB = XGBClassifier(random_state = 5)
 estimator.append(( 'XGB', XGBClassifier(random_state = 5)))
 cv = cross_val_score(XGB,x_train,x_test,cv=10)
 accuracy9 = cv.mean()
accuracy.append(accuracy9)
 print(cv)
 print(cv.mean() )<train_model>
positive_examples = train_data[train_data.has_cactus==1]
negative_examples = train_data[train_data.has_cactus==0]
XGB.fit(x_train,x_test)
 XGB.score(y_train,y_test )<save_to_csv>
def augment_img(img):
batch_input = []
batch_input += [img]
batch_input += [img[::-1, :, :]]
batch_input += [img[:, ::-1, :]]
batch_input += [np.rot90(img)]
temp_img = np.zeros_like(img)
 temp_img[:28, :, :] = img[4:, :, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[:, :28, :] = img[:, 4:, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[4:, :, :] = img[:28, :, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[:, 4:, :] = img[:, :28, :]
batch_input += [temp_img]
batch_input += [cv2.resize(img[2:30, 2:30, :],(32, 32)) ]
batch_input += [scipy.ndimage.interpolation.rotate(img, 10, reshape=False)]
batch_input += [scipy.ndimage.interpolation.rotate(img, 5, reshape=False)]
return batch_input
XGB.fit(x_train,x_test)
 model9pred = XGB.predict(y_train)
 submission9 = pd.DataFrame(columns = ['PassengerId','Survived'])
 submission9['PassengerId'] = result['PassengerId']
submission9['Survived'] = model9pred
submission9.to_csv('XGBoosting(No HT ).csv',index = False )<compute_train_metric>
def image_generator(batch_size = 8, all_data=True, shuffle=True, train=True, indexes=None):
while True:
if indexes is None:
if train:
indexes = positive_examples.index.tolist()
neg_indexes = negative_examples.index.tolist()
if shuffle:
np.random.shuffle(indexes)
 np.random.shuffle(neg_indexes)
 N = int(len(indexes)/(batch_size/2))
neg_N = int(len(neg_indexes)/(batch_size/2))
j = 0
for i in range(N):
current_indexes = indexes[i*(batch_size//2):(i+1)*(batch_size//2)]
current_neg_indexes = neg_indexes[j*(batch_size//2):(j+1)*(batch_size//2)]
j =(j + 1)% neg_N
batch_input = []
batch_output = []
for ind in range(len(current_indexes)) :
index = current_indexes[ind]
neg_index = current_neg_indexes[ind]
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input.extend(augment_img(img))
for _ in range(11):
batch_output += [train_data.has_cactus[index]]
neg_img = mpimg.imread('.. /input/train/train/' + train_data.id[neg_index])
 batch_input.extend(augment_img(neg_img))
for _ in range(11):
batch_output += [train_data.has_cactus[neg_index]]
batch_input = np.array(batch_input)
 batch_output = np.array(batch_output)
 yield(batch_input, batch_output.reshape(-1, 1))
KNN = KNeighborsClassifier(n_neighbors = 11)
 estimator.append(( 'KNN',KNeighborsClassifier(n_neighbors = 11)))
 cv = cross_val_score(KNN,x_train,x_test,cv=10)
 accuracy10 = cv.mean()
accuracy.append(accuracy10)
 print(cv)
 print(cv.mean() )<train_model>
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(64,(5, 5), input_shape=(32, 32, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(64,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(128,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(128,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(256,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(256,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(512,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Flatten())
 model.add(keras.layers.Dense(100))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Dense(1, activation='sigmoid'))
KNN.fit(x_train,x_test)
 KNN.score(y_train,y_test )<save_to_csv>
opt = keras.optimizers.SGD(lr=0.0001, momentum=0.9, nesterov=True)
 model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'] )
KNN.fit(x_train,x_test)
 model10pred = KNN.predict(y_train)
 submission10 = pd.DataFrame(columns = ['PassengerId','Survived'])
 submission10['PassengerId'] = result['PassengerId']
submission10['Survived'] = model10pred
submission10.to_csv('KNN(No HT ).csv',index = False )<create_dataframe>
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )
Models = ['Logistic Regression','Linear SVM','Polynomial SVM','Decision Tree','Gaussian NB','Multinomial NB','Random Forest Classifier','Gradient Boost Classifier','XG Boosting','K-Nearest Neighbors']
total = list(zip(Models,accuracy))
output1 = pd.DataFrame(total, columns = ['Models','Accuracy'])
<compute_train_metric>
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img[::-1, :, :]]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )
vot_soft = VotingClassifier(estimators = estimator, voting ='soft')
 vot_soft.fit(x_train, x_test)
 y_pred = vot_soft.predict(y_train)
 vot_soft.score(y_train,y_test )<save_to_csv>
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img[:, ::-1, :]]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )
modelpred1 = vot_soft.predict(y_train)
 sub1 = pd.DataFrame(columns = ['PassengerId','Survived'])
 sub1['PassengerId'] = result['PassengerId']
sub1['Survived'] = modelpred1
sub1.to_csv('SoftVoting(NO HT ).csv',index = False )<find_best_model_class>
test_files = os.listdir('.. /input/test/test/' )
vot_hard = VotingClassifier(estimators = estimator, voting ='hard')
 vot_hard.fit(x_train, x_test)
 y_pred = vot_hard.predict(y_train)
 vot_hard.score(y_train,y_test )<save_to_csv>
batch = 40
all_out = []
for i in range(int(4000/batch)) :
images = []
for j in range(batch):
img = mpimg.imread('.. /input/test/test/'+test_files[i*batch + j])
 images += [img]
out = model.predict(np.array(images))
all_out += [out]
modelpred2 = vot_hard.predict(y_train)
 sub2 = pd.DataFrame(columns = ['PassengerId','Survived'])
 sub2['PassengerId'] = result['PassengerId']
sub2['Survived'] = modelpred2
sub2.to_csv('HardVoting(NO HT ).csv',index = False )<define_variables>
sub_file = pd.DataFrame(data = {'id': test_files, 'has_cactus': all_out.reshape(-1 ).tolist() } )
Accuracy = []
Estimator = []<train_on_grid>
sub_file.to_csv('sample_submission.csv', index=False )