Kaggle competition: Aerial Cactus Identification
full[full.Name.isin(['Connolly, Miss.Kate','Kelly, Mr.James'])].sort_values(by='Name' )<sort_values>
input_path = '.. /input/'
train_path = input_path + 'train/train/'
test_path = input_path + 'test/test/'
train_dir=".. /input/train/train"
test_dir=".. /input/test/test"
train_df=pd.read_csv('.. /input/train.csv')
 test_df=pd.read_csv('.. /input/sample_submission.csv')
 train_id = train_df['id']
labels = train_df['has_cactus']
test_id = test_df['id']
full.loc[:891,['TicketClass', 'Survived']]\ 
.groupby(['TicketClass'], as_index=False)\ 
.mean().sort_values(by='Survived',
ascending=False )<sort_values>
x_train, x_val, y_train, y_val = train_test_split(train_id, labels, test_size=0.2 )
full.loc[:891,['Sex', 'Survived']]\ 
.groupby(['Sex'], as_index=False ).mean() \ 
.sort_values(by='Survived',
ascending=False )<sort_values>
def get_images(ids, filepath):
arr = []
for img_id in ids:
img = plt.imread(filepath + img_id)
 arr.append(img)
 arr = np.array(arr ).astype('float32')
 arr = arr / 255
return arr
full.loc[:891,['Embarked', 'Survived']]\ 
.groupby(['Embarked'], as_index=False ).mean() \ 
.sort_values(by='Survived',
ascending=False )<drop_column>
x_train = get_images(ids=x_train, filepath=train_path)
 x_val = get_images(ids=x_val, filepath=train_path)
 test = get_images(ids=test_id, filepath=test_path)
 img_dim = x_train.shape[1:]
full.drop(['Cabin','Ticket','PassengerId'],
axis=1,
inplace=True )<drop_column>
batch_size = 64
epochs = 30
steps = x_train.shape[0] // batch_size
full.drop(['Name'], axis=1, inplace=True )<count_missing_values>
inputs = Input(shape=img_dim)
 densenet121 = DenseNet121(weights='imagenet', include_top=False )(inputs)
 flat1 = Flatten()(densenet121)
 dense1 = Dense(units=256, use_bias=True )(flat1)
 batchnorm1 = BatchNormalization()(dense1)
 act1 = Activation(activation='relu' )(batchnorm1)
 drop1 = Dropout(rate=0.5 )(act1)
 out = Dense(units=1, activation='sigmoid' )(drop1)
 model = Model(inputs=inputs, outputs=out)
 model.compile(optimizer='adam', loss='binary_crossentropy' )
full.isnull().sum()<feature_engineering>
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=3, verbose=2, mode='max')
 img_aug = ImageDataGenerator(rotation_range=20, vertical_flip=True, horizontal_flip=True)
 img_aug.fit(x_train)
 model.fit_generator(img_aug.flow(x_train, y_train, batch_size=batch_size),
steps_per_epoch=steps, epochs=epochs,
validation_data=(x_val, y_val), callbacks=[reduce_lr],
verbose=2 )
full.loc[full['Embarked'].isnull() ,'Embarked'] = \
full['Embarked'].dropna().mode() [0]<feature_engineering>
test_pred = model.predict(test, verbose=2 )
full.loc[full['TicketFare'].isnull() ,'TicketFare'] = \
full['TicketFare'].dropna().mean()<count_missing_values>
test_df['has_cactus'] = test_pred
test_df.to_csv('submission.csv', index=False )