Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<feature_engineering>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import keras
import os
import cv2
from PIL import Image
from IPython.display import FileLink
updated_test_dir['Age group']= updated_test_dir['Age'].apply(grouping_Age )<count_values>
dataset = pd.read_csv(".. /input/train.csv")
 dataset.head()
updated_train_dir['Age group'].value_counts()<drop_column>
grouped_dataset = dataset.groupby("has_cactus")
 grouped_dataset.count()
gender = {
'male': 1,
'female':0
}
updated_train_dir['Sex'] = updated_train_dir['Sex'].apply(lambda x: gender.get(x))
updated_train_dir.drop(['Age'], axis=1, inplace=True)
<feature_engineering>
dataset.count()
updated_test_dir['Sex'] = updated_test_dir['Sex'].apply(lambda x: gender.get(x)) 
<drop_column>
def datagen(dataset=dataset, path=".. /input/train/train/"):
x = np.ones(( 17500, 224, 224, 3), dtype=np.uint8)
 y = np.ones(17500)
 counter = 0
for rec in dataset.values:
img = cv2.imread(path + rec[0])
 img = cv2.resize(img,(32, 32))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 img = cv2.copyMakeBorder(img, 96, 96, 96, 96, cv2.BORDER_REFLECT)
 x[counter] = img
y[counter] = rec[1]
counter += 1
permutation = np.random.permutation(x.shape[0])
 x = x[permutation]
y = y[permutation]
return x, y
updated_test_dir.drop(['Age'], axis=1, inplace=True)
<drop_column>
densenet = keras.applications.densenet.DenseNet169(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000 )
updated_train_dir.drop(['Fare'], axis=1, inplace=True )<drop_column>
base_model = densenet.layers[-2].output
prediction = keras.layers.Dense(1, activation="sigmoid" )(base_model)
 densenet_model = keras.models.Model(inputs=densenet.input, outputs=prediction)
 densenet_model.compile(loss="binary_crossentropy", optimizer=keras.optimizers.Adam(lr=0.00001), metrics=["accuracy"])
 densenet_model.summary()
updated_test_dir.drop(['Fare'], axis=1, inplace=True )<categorify>
densenet_model.fit(X, Y, batch_size=16, epochs=5, verbose=1, validation_split=0.2 )
traindf = pd.get_dummies(updated_train_dir, columns = ["Embarked","Age group", "Pclass"],
prefix=["Em_type", "Age_group", "Pclass_"] )<categorify>
densenet_model.fit(X, Y, batch_size=16, epochs=5, verbose=1, validation_split=0.2 )
testdf = pd.get_dummies(updated_test_dir, columns = ["Embarked","Age group", "Pclass"],
prefix=["Em_type", "Age_group", "Pclass_"])
 testdf.head(2)
<prepare_x_and_y>
predictions = test_pred()
predictions = pd.DataFrame(predictions, columns=["id", "has_cactus"])
 predictions.head()
train_y = traindf['Survived']
traindf.drop(['Survived'], axis=1,inplace=True )<train_model>
predictions.to_csv("densetmodel_submissions.csv", index=False )