Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<drop_column>
warnings.filterwarnings('ignore' )
train['BsmtQual'].fillna("No_Basement",inplace=True)
 test['BsmtQual'].fillna("No_Basement",inplace=True )<groupby>
train_directory = ".. /input/train/train/"
train_df = pd.read_csv('.. /input/train.csv')
 train_df.tail()
train.groupby('BsmtQual')['SalePrice'].agg(['count','min','max','mean'] ).sort_values(by='count' )<count_values>
X = []
Y = []
imges = train_df['id'].values
for img_id in tqdm_notebook(imges):
X.append(cv2.imread(train_directory + img_id))
Y.append(train_df[train_df['id'] == img_id]['has_cactus'].values[0])
 X = np.asarray(X)
 X = X.astype('float32')
 X /= 255
Y = np.asarray(Y )
train['BsmtCond'].value_counts(dropna=False )<drop_column>
from keras.layers import Conv2D, MaxPool2D, Dense, BatchNormalization, Activation, GlobalAveragePooling2D, Flatten, Dropout
from keras.models import Sequential, Model
from keras.regularizers import l2
from keras.optimizers import Adam
train['BsmtCond'].fillna("No_Basement",inplace=True)
 test['BsmtCond'].fillna("No_Basement",inplace=True )<drop_column>
def simple_model() :
model = Sequential()
model.add(Conv2D(32,(3, 3), input_shape=(32, 32, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(Conv2D(128,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(GlobalAveragePooling2D())
 model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer=Adam(lr=1e-5),
metrics=['accuracy'])
 return model
train['BsmtExposure'].fillna("No_Basement",inplace=True)
 test['BsmtExposure'].fillna("No_Basement",inplace=True )<drop_column>
datagen = ImageDataGenerator(featurewise_center=False,
",
samplewise_center=False,
",
featurewise_std_normalization=False,
",
samplewise_std_normalization=False,
",
zca_whitening=False,
",
zca_epsilon=1e-06,
",
rotation_range=45,
",
width_shift_range=0.2,
",
height_shift_range=0.2,
",
shear_range=0.2,
",
zoom_range=0.2,
",
channel_shift_range=0.,
",
fill_mode='nearest',
validation_split=0.1,
horizontal_flip=True,
vertical_flip=True)
 datagen.fit(X )
train['BsmtFinType1'].fillna("No_Basement",inplace=True)
 test['BsmtFinType1'].fillna("No_Basement",inplace=True )<correct_missing_values>
model = simple_model()
batch_size = 16
epoch = 1000
train = datagen.flow(X, Y, batch_size=batch_size, subset='training')
 validate = datagen.flow(X, Y, batch_size=batch_size, subset='validation')
 history = model.fit_generator(train,
validation_data=validate,
steps_per_epoch=len(X)/ batch_size,
epochs=epoch,
shuffle=True,
validation_steps=len(X)/batch_size )
test['BsmtFinSF1'].fillna(0,inplace=True )<drop_column>
test_directory = ".. /input/test/test/"
X_test = []
Test_imgs = []
for img_id in tqdm_notebook(os.listdir(test_directory)) :
X_test.append(cv2.imread(test_directory + img_id))
Test_imgs.append(img_id)
 X_test = np.asarray(X_test)
 X_test = X_test.astype('float32')
 X_test /= 255
train['BsmtFinType2'].fillna("No_Basement",inplace=True)
 test['BsmtFinType2'].fillna("No_Basement",inplace=True )<correct_missing_values>
test_predictions = model.predict(X_test )
test['BsmtFinSF2'].fillna(0,inplace=True )<correct_missing_values>
pred_df = pd.DataFrame(test_predictions, columns=['has_cactus'])
 pred_df['id'] = ''
cols = pred_df.columns.tolist()
cols = cols[-1:] + cols[:-1]
pred_df=pred_df[cols]
for i, img in enumerate(Test_imgs):
pred_df.set_value(i,'id',img)
 pred_df.to_csv('submission.csv',index=False )