Kaggle competition: Aerial Cactus Identification
<categorify>
from glob import glob
import numpy as np
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras import layers
train_test.Sex.replace(['female', 'male'], [1,0],inplace=True )<categorify>
with ZipFile('.. /input/aerial-cactus-identification/test.zip')as test_obj :
test_obj.extractall()
with ZipFile('.. /input/aerial-cactus-identification/train.zip')as train_obj :
train_obj.extractall()
X = pd.get_dummies(train_test[['Pclass','Sex','Title','Family_type',
'Age_code','Fare_code','Embarked',
'Cabin_code','Ticket_Code_Remap','Family_survived']])[:891]
X_test = pd.get_dummies(train_test[['Pclass','Sex','Title','Family_type',
'Age_code','Fare_code','Embarked',
'Cabin_code','Ticket_Code_Remap','Family_survived']])[891:]
Y = train.Survived
X.shape<choose_model_class>
df = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 print(df.head())
 file_list = df['id']
has_cactus = df['has_cactus']
print(len(file_list), len(has_cactus))
CV = StratifiedKFold(n_splits=5, shuffle=True,random_state=42)
 n_trials = 500<train_on_grid>
test_df = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 print(test_df.head())
 test_fnames = test_df['id']
test_labels = test_df['has_cactus']
print(len(test_fnames), len(test_labels))
<find_best_model_class>
data_paths = glob('train/*.jpg')
 test_paths = glob('test/*.jpg')
 print(len(data_paths), len(test_paths))
<choose_model_class>
pa = glob('train/*.jpg')[0]
pa
g = tf.io.read_file(pa)
 im = tf.io.decode_image(g)
 print(im.shape)
 plt.imshow(im)
 plt.show()
LR_param = {'penalty': 'l2', 'C': 109.16587461586346}
LR_best= LogisticRegression(**LR_param )<choose_model_class>
input_shape =(32, 32, 3)
 batch_size = 32
DT_param ={'max_depth': 12, 'min_samples_split': 60, 'min_samples_leaf': 20}
DT_best= DecisionTreeClassifier(**DT_param )<choose_model_class>
data_dir = 'train'
data_paths = []
for fname, label in zip(file_list, has_cactus):
data_paths.append(( os.path.join(data_dir, fname), label))
KNN_param ={'n_neighbors': 5, 'weights': 'uniform', 'p': 1}
KNN_best= KNeighborsClassifier(**KNN_param )<choose_model_class>
def tmp_func(path_name):
return path_name
<choose_model_class>
def read_data(path_name):
img_path = path_name[0]
label = tf.strings.to_number(path_name[1], out_type=tf.int64)
 gfile = tf.io.read_file(img_path)
 image = tf.io.decode_image(gfile)
 return image, label
<choose_model_class>
a = tf.data.Dataset.from_tensor_slices(np.array(data_paths[:2]))
a = a.map(tmp_func)
 p = next(iter(a))
p[0], p[1]
<choose_model_class>
train_ratio = 0.8
train_paths = data_paths[:int(train_ratio*len(data_paths)) ]
test_paths = data_paths[int(train_ratio*len(data_paths)) :]
MLP_best= MLPClassifier(random_state=42,
solver='adam',
early_stopping=True,
activation='relu',
alpha= 0.0002746340910250398,
learning_rate='adaptive',
learning_rate_init=0.01,
batch_size=32,
hidden_layer_sizes=160 )<choose_model_class>
train_ds = tf.data.Dataset.from_tensor_slices(np.array(train_paths))
train_ds = train_ds.map(read_data)
 train_ds = train_ds.shuffle(len(train_paths))
train_ds = train_ds.batch(batch_size)
 train_ds = train_ds.repeat()
RF_param={'n_estimators': 50, 'max_depth': 5, 'min_samples_split': 40, 'min_samples_leaf': 20}
RF_best= RandomForestClassifier(random_state=42,
criterion='gini',
oob_score=True,
**RF_param )<choose_model_class>
valid_ds = tf.data.Dataset.from_tensor_slices(np.array(test_paths))
valid_ds = valid_ds.map(read_data)
 valid_ds = valid_ds.batch(batch_size)
 valid_ds = valid_ds.repeat()
ET_params = {'n_estimators': 250, 'max_depth': 5, 'min_samples_split': 20, 'min_samples_leaf': 20}
ET_best = ExtraTreesClassifier(random_state=42,
criterion='gini',
n_jobs=-1,
**ET_params )<choose_model_class>
inputs = layers.Input(input_shape)
 net = layers.Conv2D(32, 3, 1, 'SAME' )(inputs)
 net = layers.Activation('relu' )(net)
 net = layers.Conv2D(32, 3, 1, 'SAME' )(net)
 net = layers.Activation('relu' )(net)
 net = layers.MaxPooling2D(( 2, 2))(net)
 net = layers.Dropout(0.5 )(net)
 net = layers.Conv2D(64, 3, 1, 'SAME' )(net)
 net = layers.Activation('relu' )(net)
 net = layers.Conv2D(64, 3, 1, 'SAME' )(net)
 net = layers.Activation('relu' )(net)
 net = layers.MaxPooling2D(( 2, 2))(net)
 net = layers.Dropout(0.5 )(net)
 net = layers.Flatten()(net)
 net = layers.Dense(512 )(net)
 net = layers.Activation('relu' )(net)
 net = layers.Dropout(0.5 )(net)
 net = layers.Dense(1 )(net)
 net = layers.Activation('sigmoid' )(net)
 model = tf.keras.Model(inputs=inputs, outputs=net, name='cactus_cnn' )
SVMB_params = {'n_estimators': 300, 'base_estimator__C': 3.232901108594473, 'base_estimator__gamma': 0.07183110256410177}
SVMB_best = BaggingClassifier(base_estimator=SVC(kernel = 'rbf',probability=True,C=3.232901108594473,gamma=0.07183110256410177),
random_state=42,n_jobs=-1,n_estimators=350 )<choose_model_class>
model.compile(loss = tf.keras.losses.binary_crossentropy,
optimizer = tf.keras.optimizers.Adam() ,
metrics=['accuracy'] )
ADB_best= AdaBoostClassifier(base_estimator=DecisionTreeClassifier(criterion='gini',
max_depth=2),
learning_rate=0.03990900089241141 ,
n_estimators=160,
random_state=42 )<choose_model_class>
steps_per_epoch = len(train_paths)// batch_size
validation_steps = len(test_paths)// batch_size
GDB_param = {'n_estimators': 420, 'learning_rate': 0.08199591231901683,
'max_depth': 3, 'min_samples_split': 140, 'min_samples_leaf': 20}
GDB_best= GradientBoostingClassifier(**GDB_param,
subsample=0.8,
n_iter_no_change=10,
random_state=42 )<init_hyperparams>
hist = model.fit(train_ds,
validation_data=valid_ds,
validation_steps=validation_steps,
steps_per_epoch=steps_per_epoch,
epochs = 30 )
<choose_model_class>
test_dir = 'test'
eval_paths = []
for fname in test_fnames :
eval_paths.append(os.path.join(test_dir, fname))
print(eval_paths[:5] )
XGB_param = {'max_depth': 5, 'n_estimators': 500, 'booster': 'dart',
'min_child_weight': 45, 'learning_rate': 0.00028818174062883895,
'gamma': 0.0701754028803822, 'reg_alpha': 0.09673762960851098, 'reg_lambda': 0.020973617864068886,
'colsample_bytree': 0.6000000000000001, 'subsample': 1.0}
XGB_best= XGBClassifier(**XGB_param,
objective='binary:logistic',
n_jobs=-1,
random_state=42 )<choose_model_class>
def image_read(path):
g = tf.io.read_file(path)
 im = tf.io.decode_image(g)
 return im
votingC = VotingClassifier(estimators=[('XGB', XGB_best), 
('RF',RF_best), 
('ADB',ADB_best), 
('ET', ET_best), 
('GDB',GDB_best), 
('DT',DT_best)],
voting='hard', n_jobs=-1,verbose=True)
 votingC.fit(X,Y)
 cv_result = cross_val_score(votingC,X,Y, cv = 5,scoring = "accuracy")
 vot_acc = cv_result.mean()
vot_std = cv_result.std()
performance_df.loc['Voting'] = {'CV_Mean':vot_acc, 'Std':vot_std}
performance_df.sort_values(by=['CV_Mean'],ascending=False )<choose_model_class>
from tqdm import tqdm_notebook
stacking = StackingClassifier(estimators=[('XGB', XGB_best), 
('RF',RF_best), 
('ADB',ADB_best), 
('ET', ET_best), 
('GDB',GDB_best), 
('DT',DT_best)],
final_estimator=LogisticRegression() ,
cv=5,
n_jobs=-1)
 cv_result = cross_val_score(stacking,X,Y, cv = 5,scoring = "accuracy")
 stk_acc = cv_result.mean()
stk_std = cv_result.std()
performance_df.loc['Stacking'] = {'CV_Mean':stk_acc, 'Std':stk_std}
performance_df.sort_values(by=['CV_Mean'],ascending=False )<train_model>
test_images = []
for path in tqdm_notebook(eval_paths):
test_images.append(image_read(path))
X_tr, X_te, Y_tr, Y_te = train_test_split(X,Y)
 clf = RF_best.fit(X_tr,Y_tr)
 mis_df = X_te[np.logical_xor(Y_te,clf.predict(X_te)) ]
mis_df=train.loc[mis_df.index]
mis_df.describe()<filter>
test_ds = tf.data.Dataset.from_tensor_slices(eval_paths)
 test_ds = test_ds.map(image_read)
 test_ds = test_ds.batch(batch_size )
titanic_raw_train.loc[mis_df.index]<prepare_output>
pred = model.predict(test_ds )
<save_to_csv><EOS> 
submit_df = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 test_fnames = submit_df['id']
test_labels = pred
submit_file = pd.DataFrame({'id':test_fnames, 'has_cactus':test_labels}, columns = ['id', 'has_cactus'])
 submit_file
submit_file.to_csv('submission.csv', index=False )