Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<set_options>
warnings.filterwarnings('ignore')
%matplotlib inline 
<load_from_csv>
MAIN_DIR = '.. /input/'
TRAIN_DIR = MAIN_DIR + "train/train/"
TEST_DIR = MAIN_DIR + "test/test/"
IMG_ROWS = 32
IMG_COLS = 32
CHANNELS = 3
IMG_SHAPE =(IMG_ROWS, IMG_COLS, CHANNELS)
 sns.set(font_scale=1.3 )
titanic_raw_train = pd.read_csv('.. //input/titanic/train.csv')
 titanic_raw_test = pd.read_csv('.. //input/titanic/test.csv')
 titanic_raw_train.info()
titanic_raw_test.info()<concatenate>
def plot_pictures(nb_rows=6, nb_cols=6, figsize=(14, 14)) :
fig, ax = plt.subplots(nb_rows, nb_cols, figsize=figsize, gridspec_kw=None)
 fig.subplots_adjust(wspace=0.4, hspace=0.4)
 for i in range(0, nb_rows):
for j in range(0, nb_cols):
data = pd.read_csv(MAIN_DIR + "train.csv")
 index = np.random.randint(0, data.shape[0])
 file = data.loc[index]['id']
img_ = cv2.imread(TRAIN_DIR + file)
 ax[i, j].grid(False)
 ax[i, j].axis('off')
 ax[i, j].imshow(img_)
 ax[i, j].set_title("Label : " + str(data.loc[index]['has_cactus']))
train = titanic_raw_train.copy()
test = titanic_raw_test.copy()
train_test = pd.concat([train,test] )<groupby>
df_train = pd.read_csv(MAIN_DIR + "train.csv" )
train[['Survived','Pclass']].groupby('Pclass' ).mean()<feature_engineering>
df_train['has_cactus'].value_counts()
train_test['Title']=train_test.Name.str.extract('([A-Za-z]+)\.' )<feature_engineering>
def load_data(dataframe=None, batch_size=16, mode='categorical'):
gen = ImageDataGenerator(
 zoom_range=0.1,
validation_split=0.1,
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest')
 df = pd.read_csv(MAIN_DIR + 'train.csv')
 df['has_cactus'] = df['has_cactus'].apply(str)
 data_train = gen.flow_from_dataframe(df,
directory=TRAIN_DIR,
x_col='id',
y_col='has_cactus',
has_ext=True,
target_size=(32, 32),
class_mode=mode,
batch_size=batch_size,
shuffle=True,
subset='training')
 data_test = gen.flow_from_dataframe(df,
directory=TRAIN_DIR,
x_col='id',
y_col='has_cactus',
has_ext=True,
target_size=(32, 32),
class_mode=mode,
batch_size=batch_size,
shuffle=True,
subset='validation')
 return data_train, data_test
train_test.loc[train_test['Title'] == 'Mrs','Is_Married'] = 1<feature_engineering>
def create_model() :
model = Sequential()
model.add(Conv2D(16,(3, 3), input_shape=(32, 32, 3), padding='same', use_bias=False, name="first_conv"))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3), padding='same', use_bias=False, name="second_conv"))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Conv2D(128,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Conv2D(256,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(256,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Conv2D(512,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(512,(3, 3), padding='same', use_bias=False))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=1e-4), metrics=['accuracy'])
 return model
train_test['Family_name'] = train_test.Name.str.extract('(\w+),', expand=False)
 train_test.Family_name<merge>
def train(train_generator, val_generator):
model = create_model()
cbs = [ReduceLROnPlateau(monitor='loss', factor=0.5, patience=1, min_lr=1e-7, verbose=0),
EarlyStopping(monitor='val_loss', min_delta=0.0001,
patience=10, verbose=1, mode='min', restore_best_weights=True)]
history = model.fit_generator(train_generator,
steps_per_epoch=(train_generator.n//train_generator.batch_size),
epochs=100,
validation_data=val_generator,
validation_steps=len(val_generator),
shuffle=True,
callbacks=cbs,
verbose=1)
 return model, history
m = train_test[['Family_name', 'Survived']].groupby('Family_name' ).mean()
c = train_test[['Family_name', 'PassengerId']].groupby('Family_name' ).count()
m = m.rename(columns={'Survived': 'Family_survived'})
 c = c.rename(columns={'PassengerId': 'FamilyMemberCount'})
 m = m.where(m.join(c ).FamilyMemberCount > 1, other=-1 ).fillna(-1 ).join(c)
 m.Family_survived = m.Family_survived.astype('int64')
 train_test = train_test.join(m, on='Family_name' )<count_missing_values>
train_generator, val_generator = load_data(batch_size=32)
 model, history = train(train_generator, val_generator )
train_test.Age.isnull().sum()<groupby>
def predict(model, sample_submission):
pred = np.empty(( sample_submission.shape[0],))
for n in tqdm(range(sample_submission.shape[0])) :
data = np.array(Image.open(TEST_DIR + sample_submission.id[n]))
pred[n] = model.predict(data.reshape(( 1, 32, 32, 3)))[0][1]
sample_submission['has_cactus'] = pred
return sample_submission
train_test[['Age','Title']].groupby('Title' ).mean()<feature_engineering>
sample_submission = pd.read_csv(MAIN_DIR + 'sample_submission.csv')
 df_prediction = predict(model, sample_submission)
 df_prediction.to_csv('submission.csv', index=False )