Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<feature_engineering>
resnet_weights_path = '.. /input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
print(os.listdir(".. /input"))
df_test.columns[df_test.isnull().any() ]
df_test_missing=df_test[df_test.columns[df_test.isnull().any() ]]
df_test_missing.apply(num_missing, axis=0)
 var1=['BsmtQual','Alley','Utilities','Exterior1st','Exterior2nd','MasVnrType','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2','FireplaceQu','GarageType','GarageYrBlt','GarageFinish','GarageQual','GarageCond','PoolQC','Fence','MiscFeature']
df_test[var1]=df_test[var1].fillna('NA')
 df_test['LotFrontage']=df_test['LotFrontage'].fillna(df_test['LotFrontage'].mean())
 df_test['MasVnrArea']=df_test['MasVnrArea'].fillna(df_test['MasVnrArea'].mean())
 df_test['BsmtFinSF1']=df_test['BsmtFinSF1'].fillna(df_test['BsmtFinSF1'].mean())
 df_test['BsmtFinSF2']=df_test['BsmtFinSF2'].fillna(df_test['BsmtFinSF2'].mean())
 df_test['BsmtUnfSF']=df_test['BsmtUnfSF'].fillna(df_test['BsmtUnfSF'].mean())
 df_test['TotalBsmtSF']=df_test['TotalBsmtSF'].fillna(df_test['TotalBsmtSF'].mean())
 df_test['GarageCars']=df_test['GarageCars'].fillna(df_test['GarageCars'].mean())
 df_test['GarageArea']=df_test['GarageArea'].fillna(df_test['GarageArea'].mean())
 df_test['GarageArea']=df_test['GarageArea'].fillna(df_test['GarageArea'].mean())
 df_test['MSZoning']=df_test['MSZoning'].fillna(mode('MSZoning'))
df_test['BsmtHalfBath']=df_test['BsmtHalfBath'].fillna(0)
 df_test['KitchenQual']=df_test['KitchenQual'].fillna('TA')
 df_test['Functional']=df_test['Functional'].fillna(mode('Functional'))
df_test['BsmtFullBath']=df_test['BsmtFullBath'].fillna(0)
 df_test['SaleType']=df_test['SaleType'].fillna(mode('SaleType')) 
<filter>
def prep_cnn_data(df, n_x, n_c, path):

 tensors = np.zeros(( df.shape[0], n_x, n_x, n_c))
for i in range(df.shape[0]):
pic = load_img(path+df.iloc[i]['id'])
 pic_array = img_to_array(pic)
 tensors[i,:] = pic_array
tensors = tensors / 255.
 return tensors
df_train=df_train[np.abs(df_train.SalePrice-df_train.SalePrice.mean())<=(6*df_train.SalePrice.std())]<feature_engineering>
train_df = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 test_df = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 train = prep_cnn_data(train_df, 32, 3, path='.. /input/aerial-cactus-identification/train/train/')
 train_y = train_df['has_cactus'].values
test = prep_cnn_data(test_df, 32, 3, path='.. /input/aerial-cactus-identification/test/test/' )
df_test['TT_SF']=df_test['1stFlrSF']+df_test['2ndFlrSF']+df_test['TotalBsmtSF']+df_test['GarageArea']+df_test['GrLivArea']
df_test['TT_bathrooms']=df_test['FullBath']+df_test['HalfBath']+df_test['BsmtFullBath']+df_test['HalfBath']
df_test['TT_rooms']=df_test['TotRmsAbvGrd']+df_test['BsmtFullBath']+df_test['BsmtHalfBath']
df_train['TT_SF']=df_train['1stFlrSF']+df_train['2ndFlrSF']+df_train['TotalBsmtSF']+df_train['GarageArea']+df_train['GrLivArea']
df_train['TT_bathrooms']=df_train['FullBath']+df_train['HalfBath']+df_train['BsmtFullBath']+df_train['HalfBath']
df_train['TT_rooms']=df_train['TotRmsAbvGrd']+df_train['BsmtFullBath']+df_train['BsmtHalfBath'] 
<drop_column>
datagen = ImageDataGenerator(
 zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
vertical_flip=True)
<feature_engineering>
del model
df_train['SalePrice'] = np.log(df_train['SalePrice'] )<feature_engineering>
model = Sequential()
model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(32,32,3)))
 model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.5))
model.add(Conv2D(filters = 32, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 32, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.5))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(1, activation = "sigmoid"))
numeric_vars = df_train.dtypes[df_train.dtypes != "object"].index
skewed_vars = df_train[numeric_vars].skew().sort_values(ascending=False)
 skewness = pd.DataFrame({'Skew':skewed_vars})
 skewness = skewness[abs(skewness['Skew'])> 0.7]
skewness
skewed_vars = skewness.index
df_train[skewed_vars] = np.log(df_train[skewed_vars]+0.001)
 df_test[skewed_vars] = np.log(df_test[skewed_vars]+0.001 )<categorify>
model.compile(optimizer = 'adam', loss = "binary_crossentropy", metrics=["accuracy"] )
df_train=pd.get_dummies(df_train)
 df_test=pd.get_dummies(df_test)
 final_train, final_test = df_train.align(df_test,join='inner',axis=1)
<train_model>
batch_size=256
epochs=200
x_train=train[3500:]
y_train=train_y[3500:]
x_val=train[:3500]
y_val=train_y[:3500]
red_lr= ReduceLROnPlateau(monitor='val_acc',patience=3,verbose=1,factor=0.7)
 History = model.fit_generator(datagen.flow(x_train,y_train,batch_size=batch_size),
epochs= epochs, steps_per_epoch=x_train.shape[0]//batch_size,
validation_data=(x_val,y_val),callbacks=[red_lr] )
<import_modules><EOS> 
pred=model.predict(test)
 test_csv = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 df=pd.DataFrame({'id':test_csv['id'] })
 df['has_cactus']=pred
df.to_csv("submission.csv",index=False )