Kaggle competition: Aerial Cactus Identification
import plotly.express as px
import plotly.graph_objects as go
import plotly.figure_factory as ff
from plotly.colors import n_colors
from plotly.subplots import make_subplots
from sklearn.preprocessing import MinMaxScaler<feature_engineering>
from tensorflow.keras import layers, optimizers, models, callbacks, applications
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from shutil import copy2
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 train_data["Age"] = train_data["Age"].fillna(train_data.describe() ["Age"]["mean"])
 test_data["Age"] = test_data["Age"].fillna(test_data.describe() ["Age"]["mean"])
 train_data['Age']=train_data['Age'].astype(np.float64)
 train_data["Fare"] = train_data["Fare"].fillna(train_data.describe() ["Fare"]["mean"])
 test_data["Fare"] = test_data["Fare"].fillna(test_data.describe() ["Fare"]["mean"])
 train_data['Fare']=train_data['Fare'].astype(int)
 train_data.head(5 )<filter>
train_dir = '.. /input/train/train/'
test_dir = '.. /input/test/test/'
df = pd.read_csv('.. /input/train.csv')
 df.head()
women = train_data[train_data.Sex == 'female']["Survived"]
men = train_data[train_data.Sex == 'male']["Survived"]
print("Survival rate for women is {:.2f} and for men is {:.2f}".format(( sum(women)/len(women)) *100,(sum(men)/len(men)) *100))<feature_engineering>
labels = df['has_cactus'].tolist()
img_paths = df['id'].tolist()
data = train_data
data['Died'] = 1 - data['Survived']<prepare_x_and_y>
def build_model() :
model_input = layers.Input(shape = [32, 32, 3])
 X = applications.densenet.DenseNet121(weights = 'imagenet', include_top = False, classes = 1 )(model_input)
 X = layers.Flatten()(X)
 model_output = layers.Dense(1, activation = 'sigmoid' )(X)
 return models.Model(model_input, model_output)
x_train = train_data.drop(['Survived', 'Died'], axis=1)
 y_train = train_data['Survived']
x_test = test_data
df_combined = x_train.append(x_test)
 df_combined.shape<feature_engineering>
model = build_model()
model.compile(loss = 'binary_crossentropy', optimizer = optimizers.Adam(lr = 0.0001), metrics = ['accuracy'])
 model.summary()
def family_size() :
global df_combined
df_combined['FamilySize'] = df_combined['Parch'] + df_combined['SibSp'] + 1
df_combined['Singleton'] = df_combined['FamilySize'].map(lambda s: 1 if s == 1 else 0)
 df_combined['SmallFamily'] = df_combined['FamilySize'].map(lambda s: 1 if 2 <= s <= 4 else 0)
 df_combined['LargeFamily'] = df_combined['FamilySize'].map(lambda s: 1 if 5 <= s <= 8 else 0)
 return df_combined<categorify>
def split_dataframe(csv_file):
df = pd.read_csv(csv_file)
 train_df = df.iloc[:14000, :]
train_df['has_cactus'] = train_df['has_cactus'].apply(str)
 val_df = df.iloc[14000:, :]
val_df['has_cactus'] = val_df['has_cactus'].apply(str)
 return train_df, val_df
train_df, val_df = split_dataframe('.. /input/train.csv' )
def embarked() :
global df_combined
df_combined['Embarked'].fillna('S', inplace=True)
 df_dummies = pd.get_dummies(df_combined['Embarked'], prefix='Embarked')
 df_combined = pd.concat([df_combined, df_dummies], axis=1)
 df_combined.drop('Embarked', axis=1, inplace=True)
 return df_combined<categorify>
train_df, val_df = split_dataframe('.. /input/train.csv')
 train_gen = ImageDataGenerator(
 rescale = 1./255,
validation_split = 0.15,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
vertical_flip = True,
rotation_range = 10 
)
 train_gen = train_gen.flow_from_dataframe(
 dataframe = train_df,
directory = train_dir,
x_col = 'id',
y_col = 'has_cactus',
target_size =(32, 32),
color_mode = 'rgb',
class_mode = 'binary',
batch_size = 128,
shuffle = True, 
)
 val_gen = ImageDataGenerator(rescale = 1./255, validation_split = 0.15)
 val_gen = val_gen.flow_from_dataframe(
 dataframe = val_df,
directory = train_dir,
x_col = 'id',
y_col = 'has_cactus',
target_size =(32, 32),
color_mode = 'rgb',
class_mode = 'binary',
batch_size = 128,
shuffle = True,
)
def cabin() :
global df_combined
df_combined['Cabin'].fillna('U', inplace=True)
 df_combined['Cabin'] = df_combined['Cabin'].map(lambda ca: ca[0])
 cabin_dummies = pd.get_dummies(df_combined['Cabin'], prefix='Cabin')
 df_combined = pd.concat([df_combined, cabin_dummies], axis=1)
 df_combined.drop('Cabin', inplace=True, axis=1)
 return df_combined<feature_engineering>
checkpoint = callbacks.ModelCheckpoint(
 'model.h5',
monitor = 'val_loss',
verbose = 0,
save_best_only = True,
save_weights_only = False,
mode = 'auto' 
)
 regulate_lr = callbacks.ReduceLROnPlateau(monitor = 'val_loss', min_lr = 1e-5, patience = 3)
 model.fit_generator(
 train_gen,
steps_per_epoch = 14000 // 128,
validation_data = val_gen,
validation_steps = 14000 // 128,
epochs = 60,
callbacks = [checkpoint, regulate_lr] 
)
df_combined['Age'] = df_combined['Age'].astype(int)
 df_combined.loc[df_combined['Age'] <= 11, 'Age'] = 0
df_combined.loc[(df_combined['Age'] > 11)&(df_combined['Age'] <= 18), 'Age'] = 1
df_combined.loc[(df_combined['Age'] > 18)&(df_combined['Age'] <= 22), 'Age'] = 2
df_combined.loc[(df_combined['Age'] > 22)&(df_combined['Age'] <= 27), 'Age'] = 3
df_combined.loc[(df_combined['Age'] > 27)&(df_combined['Age'] <= 33), 'Age'] = 4
df_combined.loc[(df_combined['Age'] > 33)&(df_combined['Age'] <= 46), 'Age'] = 5
df_combined.loc[(df_combined['Age'] > 46)&(df_combined['Age'] <= 66), 'Age'] = 6
df_combined.loc[df_combined['Age'] >= 66, 'Age'] = 7
df_combined['Age'].value_counts()<feature_engineering>
submit = pd.read_csv('.. /input/sample_submission.csv')
 test = []
for image in tqdm(submit['id']):
test.append(cv2.imread('.. /input/test/test/' + image))
test = np.array(test)
 print(test[0].shape)
df_combined.loc[df_combined['Fare'] <= 7, 'Fare'] = 0
df_combined.loc[(df_combined['Fare'] > 7)&(df_combined['Fare'] <= 14), 'Fare'] = 1
df_combined.loc[(df_combined['Fare'] > 14)&(df_combined['Fare'] <= 31), 'Fare'] = 2
df_combined.loc[(df_combined['Fare'] > 31)&(df_combined['Fare'] <= 99), 'Fare'] = 3
df_combined.loc[(df_combined['Fare'] > 99)&(df_combined['Fare'] <= 250), 'Fare'] = 4
df_combined.loc[df_combined['Fare'] >= 250, 'Fare'] = 5
df_combined['Fare'] = df_combined['Fare'].astype(int)
 df_combined['Fare'].value_counts()<categorify>
test = test / 255
preds = final_model.predict(test, verbose = 1)
 submit['has_cactus'] = preds
submit.head(10)
 submit.to_csv('sample_submission.csv', index = False )