Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<categorify>
!unzip /kaggle/input/aerial-cactus-identification/train.zip
!unzip /kaggle/input/aerial-cactus-identification/test.zip
age_mapping = {'Baby': 1, 'Child': 2, 'Teenager': 3, 'Student': 4, 'Young Adult': 5, 'Adult': 6, 'Senior': 7}
train['AgeGroup'] = train['AgeGroup'].map(age_mapping)
 test['AgeGroup'] = test['AgeGroup'].map(age_mapping)
 train.head()
train = train.drop(['Age'], axis = 1)
 test = test.drop(['Age'], axis = 1 )<drop_column>
%matplotlib inline
train = train.drop(['Name'], axis = 1)
 test = test.drop(['Name'], axis = 1 )<categorify>
print(sys.version)
 print('tensorflow -> ', tf.__version__ )
sex_mapping = {"male": 0, "female": 1}
train['Sex'] = train['Sex'].map(sex_mapping)
 test['Sex'] = test['Sex'].map(sex_mapping)
 train.head()<categorify>
np.random.seed(12)
 tf.random.set_seed(12 )
embarked_mapping = {"S": 1, "C": 2, "Q": 3}
train['Embarked'] = train['Embarked'].map(embarked_mapping)
 test['Embarked'] = test['Embarked'].map(embarked_mapping)
 train.head()<split>
main_df = pd.read_csv('/kaggle/input/aerial-cactus-identification/train.csv')
 sub_df = pd.read_csv('/kaggle/input/aerial-cactus-identification/sample_submission.csv')
 train_dir = '/kaggle/working/train/'
test_dir = '/kaggle/working/test/'
predictors = train.drop(['Survived', 'PassengerId'], axis=1)
 target = train["Survived"]
x_train, x_val, y_train, y_val = train_test_split(predictors, target, test_size = 0.22, random_state = 0 )<predict_on_test>
print('shape: ', main_df.shape)
 print('===================================')
 print(main_df['has_cactus'].value_counts() )
gaussian = GaussianNB()
gaussian.fit(x_train, y_train)
 y_pred = gaussian.predict(x_val)
 acc_gaussian = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_gaussian )<compute_train_metric>
train_df, val_df = train_test_split(main_df, test_size=0.25, stratify=main_df['has_cactus'], shuffle=True, random_state=12)
 train_df = train_df.reset_index()
val_df = val_df.reset_index()
total_train = train_df.shape[0]
total_val = val_df.shape[0]
print('total_train: {}, total_val: {}'.format(total_train, total_val))
logreg = LogisticRegression()
logreg.fit(x_train, y_train)
 y_pred = logreg.predict(x_val)
 acc_logreg = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_logreg )<compute_train_metric>
img_width, img_height = 32, 32
target_size =(img_width, img_height)
 train_datagen = ImageDataGenerator(rescale=1./255)
 val_datagen = ImageDataGenerator(rescale=1./255)
 test_datagen = ImageDataGenerator(rescale=1./255)
 train_df['has_cactus'] = train_df['has_cactus'].astype(str)
 val_df['has_cactus'] = val_df['has_cactus'].astype(str )
svc = SVC()
svc.fit(x_train, y_train)
 y_pred = svc.predict(x_val)
 acc_svc = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_svc )<compute_train_metric>
batch_size = 32
x_col, y_col = 'id', 'has_cactus'
class_mode = 'binary'
train_gen = train_datagen.flow_from_dataframe(train_df,
train_dir,
x_col=x_col,
y_col=y_col,
class_mode=class_mode,
target_size=target_size,
batch_size=batch_size, 
)
 val_gen = val_datagen.flow_from_dataframe(val_df,
train_dir,
x_col=x_col,
y_col=y_col,
class_mode=class_mode,
target_size=target_size,
batch_size=batch_size,
)
linear_svc = LinearSVC()
linear_svc.fit(x_train, y_train)
 y_pred = linear_svc.predict(x_val)
 acc_linear_svc = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_linear_svc )<compute_train_metric>
input_shape =(img_width, img_height, 3)
 optimizer = optimizers.Adam(lr=1e-3 )
perceptron = Perceptron()
perceptron.fit(x_train, y_train)
 y_pred = perceptron.predict(x_val)
 acc_perceptron = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_perceptron )<train_model>
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3,3), padding='same', activation='relu', input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=(3,3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(128, kernel_size=(3,3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(GlobalMaxPooling2D())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', metrics=['acc'], optimizer=optimizer)
 model.summary()
decisiontree = DecisionTreeClassifier()
decisiontree.fit(x_train, y_train)
 y_pred = decisiontree.predict(x_val)
 acc_decisiontree = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_decisiontree )<train_model>
def step_decay(epoch):
initial_rate = 0.001
drop = 0.5
epochs_drop = 10.0
lrate = initial_rate * math.pow(drop, math.floor(( epoch)/ epochs_drop))
return lrate
randomforest = RandomForestClassifier()
randomforest.fit(x_train, y_train)
 y_pred = randomforest.predict(x_val)
 acc_randomforest = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_randomforest )<compute_train_metric>
lrate = LearningRateScheduler(step_decay)
 es = EarlyStopping(monitor='val_loss', min_delta=0, patience=5)
 callbacks = [lrate, es]
knn = KNeighborsClassifier()
knn.fit(x_train, y_train)
 y_pred = knn.predict(x_val)
 acc_knn = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_knn )<predict_on_test>
epochs = 30
history = model.fit(
 train_gen,
epochs=epochs,
steps_per_epoch=total_train//batch_size,
validation_data=val_gen,
validation_steps=total_val//batch_size,
callbacks=callbacks,
)
sgd = SGDClassifier()
sgd.fit(x_train, y_train)
 y_pred = sgd.predict(x_val)
 acc_sgd = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_sgd )<predict_on_test>
def predict(model, sub_df):
pred = np.empty(( sub_df.shape[0],))
for n in tqdm(range(sub_df.shape[0])) :
image = np.array(Image.open(test_dir + sub_df.id[n]))
pred[n] = model.predict(image.reshape(( 1, 32, 32, 3)) /255.0)[0]
sub_df['has_cactus'] = pred
return sub_df
gbk = GradientBoostingClassifier()
gbk.fit(x_train, y_train)
 y_pred = gbk.predict(x_val)
 acc_gbk = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_gbk )<create_dataframe>
predictions = predict(model, sub_df )
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest', 'Naive Bayes', 'Perceptron', 'Linear SVC',
'Decision Tree', 'Stochastic Gradient Descent', 'Gradient Boosting Classifier'],
'Score': [acc_svc, acc_knn, acc_logreg,
acc_randomforest, acc_gaussian, acc_perceptron,acc_linear_svc, acc_decisiontree,
acc_sgd, acc_gbk]})
 models.sort_values(by='Score', ascending=False )<save_to_csv>
!rm -r *
<import_modules><EOS> 
predictions.to_csv('submission.csv', header=True, index=False )