Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<feature_engineering>
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf
from tensorflow import keras
import os
from shutil import copyfile, move
from tqdm import tqdm
import h5py
test_ml[test_ml.isnull().any(axis=1)]
test_ml["Fare"] = test_ml["Fare"].fillna(test_ml.groupby('Pclass' ).mean() ['Fare'][3])
 test_ml.info()<data_type_conversions>
print(tf.__version__)
 print(tf.test.is_gpu_available() )
test_ml.loc[test_ml["Sex"] == "male", "Sex"] = 0
test_ml.loc[test_ml["Sex"] == "female", "Sex"] = 1
test_ml["Sex"] = test_ml["Sex"].astype(int)
 test_ml.loc[test_ml["Embarked"] == "S", "Embarked"] = 0
test_ml.loc[test_ml["Embarked"] == "C", "Embarked"] = 1
test_ml.loc[test_ml["Embarked"] == "Q", "Embarked"] = 2
test_ml["Embarked"] = test_ml["Embarked"].astype(int )<categorify>
training_df = pd.read_csv(".. /input/train.csv")
 training_df.head()
test_ml['age_categories'] = pd.cut(test_ml.Age, [0, 11, 18, 23, 27, 33, 40, 50, np.inf],
labels=[0, 1, 2, 3, 4, 5, 6,7], include_lowest=True, right=False ).astype(int)
 test_ml['fare_categories'] = pd.cut(test_ml.Fare, [0, 8, 14, 31, 99, 250, np.inf],
labels=[0, 1, 2, 3, 4, 5], include_lowest=True, right=False ).astype(int )<feature_engineering>
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import InputLayer, Input
from tensorflow.keras.layers import Conv2D, Dense, Flatten, Dropout, Activation
from tensorflow.keras.layers import BatchNormalization, Reshape, MaxPooling2D, GlobalAveragePooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
test_ml['family_members'] = test_ml.SibSp + test_ml.Parch<drop_column>
batch_size = 64
train_datagen = ImageDataGenerator(
 rescale=1./ 255,
horizontal_flip=True,
vertical_flip=True)
 train_data_dir = ".. /sorted_training"
train_generator = train_datagen.flow_from_directory(
 train_data_dir,
shuffle=True,
target_size=(32, 32),
batch_size=batch_size,
class_mode='binary')
 validation_datagen = ImageDataGenerator(rescale=1./ 255)
 validation_data_dir = ".. /sorted_validation"
validation_generator = validation_datagen.flow_from_directory(
 validation_data_dir,
target_size=(32, 32),
batch_size=batch_size,
class_mode='binary')
 input_shape =(32,32,3)
 num_classes = 2
X_test = test_final.drop('PassengerId', axis=1 )<prepare_x_and_y>
dropout_dense_layer = 0.6
model = Sequential()
model.add(Conv2D(32,(3, 3), input_shape=input_shape))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(64,(3, 3)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(128,(3, 3)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Flatten())
 model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dropout(dropout_dense_layer))
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(dropout_dense_layer))
model.add(Dense(1))
model.add(Activation('sigmoid'))
X_train = train_ml[['Pclass', 'Sex', 'Embarked', 'age_categories', 'fare_categories', 'family_members']]
Y_train = train_ml.Survived<find_best_model_class>
model.compile(loss=keras.losses.binary_crossentropy,
optimizer=keras.optimizers.Adam(lr=0.001),
metrics=['accuracy'] )
log_reg = LogisticRegression()
log_reg.fit(X_train, Y_train)
 log_reg_acc = round(cross_val_score(log_reg, X_train, Y_train, cv=10, scoring = "accuracy" ).mean() *100, 4)
 predictions = log_reg.predict(X_test )<find_best_model_class>
callbacks = [EarlyStopping(monitor='val_loss', patience=25),
ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True)]
knn = KNeighborsClassifier()
knn.fit(X_train, Y_train)
 knn_acc = round(cross_val_score(knn, X_train, Y_train, cv=10, scoring = "accuracy" ).mean() *100, 4)
 predictions = knn.predict(X_test )<find_best_model_class>
epochs = 100
history = model.fit_generator(train_generator,
validation_data=validation_generator,
epochs=epochs,
verbose=1,
shuffle=True,
callbacks=callbacks )
gaussian_nb = GaussianNB()
gaussian_nb.fit(X_train, Y_train)
 gaussian_nb_acc = round(cross_val_score(gaussian_nb, X_train, Y_train, cv=10, scoring = "accuracy" ).mean() *100, 4)
 predictions = gaussian_nb.predict(X_test )<find_best_model_class>
model.load_weights("best_model.h5" )
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
 decision_tree_acc = round(cross_val_score(decision_tree, X_train, Y_train, cv=10, scoring = "accuracy" ).mean() *100, 4)
 predictions = decision_tree.predict(X_test )<find_best_model_class>
test_folder = ".. /input/test/"
test_datagen = ImageDataGenerator(
 rescale=1./ 255)
 test_generator = test_datagen.flow_from_directory(
 directory=test_folder,
target_size=(32,32),
batch_size=1,
class_mode='binary',
shuffle=False
)
random_forest = RandomForestClassifier()
random_forest.fit(X_train, Y_train)
 random_forest_acc = round(cross_val_score(random_forest, X_train, Y_train, cv=10, scoring = "accuracy" ).mean() *100, 4)
 predictions = random_forest.predict(X_test )<create_dataframe>
pred=model.predict_generator(test_generator,verbose=1)
 pred_binary = [0 if value<0.50 else 1 for value in pred]
<create_dataframe><EOS> 
csv_file = open("sample_submission_cnn.csv","w")
 csv_file.write("id,has_cactus
")
 for filename, prediction in zip(test_generator.filenames,pred_binary):
name = filename.split("/")[1].replace(".tif","")
 csv_file.write(str(name)+","+str(prediction)+"
")
 csv_file.close()