Kaggle competition: Aerial Cactus Identification
cname = 'TotalBsmtSF'
df_train.corr() [cname][abs(df_train.corr() [cname])> 0.65]<drop_column>
%%time
df_train = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 df_test = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 df_train.head(3 )
col = ['GarageArea','1stFlrSF', '2ndFlrSF','TotRmsAbvGrd', 'GarageYrBlt']
df_train.drop(col, axis=1, inplace=True)
 df_test.drop(col, axis=1, inplace=True )<data_type_conversions>
TRAIN_DATA_PATH ='.. /input/aerial-cactus-identification/train/train'
sample = TRAIN_DATA_PATH +'/'+ os.listdir(TRAIN_DATA_PATH)[0]
TEST_DATA_PATH = '.. /input/aerial-cactus-identification/test/test'
np.array(Image.open(sample)).shape
def fill_missing_data(df):
df_data = df.copy()
categoricals = []
for cname,dtype in df_data.dtypes.items() :
if dtype == 'object':
categoricals.append(cname)
 df_data[categoricals] = df_data[categoricals].fillna('None')
 for cname in df_data.columns:
if cname not in categoricals:
df_data[cname] = df_data[cname].fillna(0)
 return df_data<correct_missing_values>
print('target 0:1->',len(df_train[df_train.has_cactus==0])/len(df_train[df_train.has_cactus==1]))
df_train = fill_missing_data(df_train)
 df_test = fill_missing_data(df_test )<feature_engineering>
%%time
tmp = []
for i in range(len(df_train)) :
tmp.append(np.array(Image.open(TRAIN_DATA_PATH +'/'+ df_train.iloc[i]['id'])))
 X_train, y_train = np.array(tmp)/255, df_train['has_cactus']
tmp = []
for i in range(len(df_test)) :
tmp.append(np.array(Image.open(TEST_DATA_PATH +'/'+ df_test.iloc[i]['id'])))
 X_test = np.array(tmp)/255
del tmp
print(X_train.shape, y_train.shape)
 print(X_test.shape )
df_train['TotalPorchSF'] = df_train['OpenPorchSF'] + df_train['EnclosedPorch'] + df_train['3SsnPorch'] + df_train['ScreenPorch']
df_test['TotalPorchSF'] = df_test['OpenPorchSF'] + df_test['EnclosedPorch'] + df_test['3SsnPorch'] + df_test['ScreenPorch']<feature_engineering>
def build_model(input_shape):
model =Sequential()
model.add(Conv2D(64,(3,3),padding='same',input_shape=(input_shape)))
 model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(Conv2D(64,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(Conv2D(64,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(MaxPooling2D())
 model.add(Dropout(0.5))
model.add(Conv2D(128,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(Conv2D(128,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(Conv2D(128,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(MaxPooling2D())
 model.add(Dropout(0.5))
model.add(Conv2D(256,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(Conv2D(256,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(Conv2D(256,(3,3),padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(scale=False))
model.add(GlobalAveragePooling2D())
 model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile('adam',loss='binary_crossentropy',metrics=['accuracy'])
 return model
df_train['TotalBaths'] = df_train['BsmtFullBath'] + df_train['FullBath'] + 0.5*(df_train['BsmtHalfBath'] + df_train['HalfBath'])
 df_test['TotalBaths'] = df_test['BsmtFullBath'] + df_test['FullBath'] + 0.5*(df_test['BsmtHalfBath'] + df_test['HalfBath'] )<feature_engineering>
1.0 / len(df_train[df_train.has_cactus==0])* len(df_train[df_train.has_cactus==1] )
df_train['TotalAreaSF'] = df_train['TotalBsmtSF'] + df_train['GrLivArea']
df_test['TotalAreaSF'] = df_test['TotalBsmtSF'] + df_test['GrLivArea']<data_type_conversions>
%%time
histories = []
oof_pred = np.zeros(len(df_train))
sub_pred = np.zeros(len(df_test))
class_weights = {}
weights = [3.010082493125573,
1.0]
len(df_train[df_train.has_cactus==0])
 for i in range(2):
class_weights[i] = weights[i]
print('class_weights:',class_weights)
 checkpoint_name = '/checkpoint.file'
skf = StratifiedKFold(n_splits=10, shuffle=True, random_state=42)
 BATCH_SIZE = 64
EPOCHS = 128
for fold_id,(train_index, val_index)in enumerate(skf.split(X_train, y_train)) :
print(f'fold id: {fold_id}')
 X_tr, y_tr = X_train[train_index], y_train[train_index]
X_val, y_val = X_train[val_index], y_train[val_index]
callbacks=[
keras.callbacks.ModelCheckpoint(
 checkpoint_name, monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False),
keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.7, patience=5, verbose=1,min_delta=0.00005,),
keras.callbacks.EarlyStopping(monitor='val_loss', patience=15)
 ]
datagen = ImageDataGenerator(
 height_shift_range=0.1,
horizontal_flip=True 
)
 K.clear_session()
model = build_model(X_train.shape[1:])
 model.summary()
histories.append(
 model.fit_generator(
 datagen.flow(X_train, y_train, batch_size=BATCH_SIZE),
steps_per_epoch=int(np.ceil(len(X_train)/ BATCH_SIZE)) , validation_data=(X_val, y_val),
epochs=EPOCHS, class_weight=class_weights,
callbacks=callbacks, verbose=2)
)
 model = load_model(checkpoint_name)
 oof_pred[val_index] = model.predict(X_val ).flatten()
sub_pred += model.predict(X_test ).flatten() / skf.n_splits
print(roc_auc_score(y_val, oof_pred[val_index]))
plot_roc_auc(y_val, oof_pred[val_index])
 del callbacks
sub_pred = np.clip(sub_pred,0.0,1.0 )
df_train['Age'] = df_train['YrSold'].astype('int64')- df_train['YearBuilt']
df_test['Age'] = df_test['YrSold'].astype('int64')- df_test['YearBuilt']<drop_column>
print('Ambiguous image index:',np.where(( sub_pred<0.80)&(sub_pred>0.20)) [:32][0] )
<feature_engineering><EOS> 
submission = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 submission['has_cactus'] = sub_pred
submission.to_csv('submission.csv', index=False)
 submission.head(50 )