Kaggle competition: Aerial Cactus Identification
Test['cond*qual'] =(Test['OverallCond'] * Test['OverallQual'])/ 100.0
Test['home_age_when_sold'] = Test['YrSold'] - Test['YearBuilt']
Test['garage_age_when_sold'] = Test['YrSold'] - Test['GarageYrBlt']
Test['TotalSF'] = Test['TotalBsmtSF'] + Test['1stFlrSF'] + Test['2ndFlrSF']
Test['total_porch_area'] = Test['WoodDeckSF'] + Test['OpenPorchSF'] + Test['EnclosedPorch'] + Test['3SsnPorch'] + Test['ScreenPorch']
Test['Totalsqrfootage'] =(Test['BsmtFinSF1'] + Test['BsmtFinSF2'] + Test['1stFlrSF'] + Test['2ndFlrSF'])
 Test['Total_Bathrooms'] =(Test['FullBath'] +(0.5 * Test['HalfBath'])+ Test['BsmtFullBath'] +(0.5 * Test['BsmtHalfBath']))<define_variables>
train_df = pd.read_csv('.. /input/train.csv' )
Old_Cols=['OverallCond','OverallQual','YrSold','YearBuilt','YrSold','GarageYrBlt','TotalBsmtSF','1stFlrSF','2ndFlrSF','WoodDeckSF','OpenPorchSF','EnclosedPorch','3SsnPorch','ScreenPorch','BsmtFinSF1','BsmtFinSF2','1stFlrSF','2ndFlrSF','FullBath','HalfBath','BsmtFullBath','BsmtHalfBath']<define_variables>
train_data = []
train_tgt = []
for idx, row in train_df.iterrows() :
img = imread('.. /input/train/train/' + row['id'])
 train_data.append(img)
 train_tgt.append(row['has_cactus'])
 train_data = np.array(train_data)
 train_tgt = np.array(train_tgt )
Final_cols=[]
for i in DF.columns:
if i not in Old_Cols and i!='SalePrice':
Final_cols.append(i)
 PF=DF[Final_cols]<drop_column>
train_X, val_X, train_y, val_y = sklearn.model_selection.train_test_split(train_data, train_tgt, test_size=0.40 )
Final_cols=[]
for i in Test.columns:
if i not in Old_Cols and i!='SalePrice':
Final_cols.append(i)
 TF=Test[Final_cols]<prepare_x_and_y>
m0 = train_X[:,:,:,0].mean()
m1 = train_X[:,:,:,1].mean()
m2 = train_X[:,:,:,2].mean()
def augmented(data: list, tgt: list):
aug_data = []
aug_tgt = []
for i in range(data.shape[0]):
img = data[i, :, :, :]
img = img/255.
 aug_data.append(img)
 aug_data.append(rotate(img, 90.))
 aug_data.append(rotate(img, 180.))
 aug_data.append(rotate(img, 270.))
 aug_data.append(np.flip(img))
aug_data.append(np.flip(img, axis=0))
aug_data.append(np.flip(img, axis=1))
aug_tgt.append(tgt[i])
 aug_tgt.append(tgt[i])
 aug_tgt.append(tgt[i])
 aug_tgt.append(tgt[i])
 aug_tgt.append(tgt[i])
 aug_tgt.append(tgt[i])
 aug_tgt.append(tgt[i])
 return np.array(aug_data), np.array(aug_tgt)
 train_X, train_y = augmented(train_X, train_y)
 val_X, val_y = augmented(val_X, val_y )
y = DF.SalePrice<split>
train_X.mean(axis=0 ).mean(axis=0 ).mean(axis=0 )
X_train, X_test, y_train, y_test = train_test_split(PF, y, test_size=0.3 )<data_type_conversions>
val_X.mean(axis=0 ).mean(axis=0 ).mean(axis=0 )
def Change(x):
for col in x.select_dtypes(include=['object'] ).columns:
x[col] = x[col].astype('category')
 for col in x.select_dtypes(include=['category'] ).columns:
x[col] = x[col].cat.codes
return x<drop_column>
model = keras.Sequential([
keras.layers.InputLayer(input_shape=(32, 32, 3), batch_size=None, name='input'),
keras.layers.Conv2D(128, 1, 1, 'SAME', activation=keras.activations.relu, name='bottleneck1/conv1'),
keras.layers.Conv2D(16, 3, 1, 'SAME', activation=keras.activations.relu, name='bottleneck1/conv2'),
keras.layers.BatchNormalization(name='bottleneck1/bn1'),
keras.layers.Conv2D(128, 1, 1, 'SAME', activation=keras.activations.relu, name='bottleneck2/conv1'),
keras.layers.Conv2D(32, 3, 2, 'SAME', activation=keras.activations.relu, name='bottleneck2/conv2'),
keras.layers.BatchNormalization(name='bottleneck2/bn1'),
keras.layers.Conv2D(128, 1, 1, 'SAME', activation=keras.activations.relu, name='bottleneck3/conv1'),
keras.layers.Conv2D(64, 3, 2, 'SAME', activation=keras.activations.relu, name='bottleneck3/conv2'),
keras.layers.BatchNormalization(name='bottleneck3/bn1'),
keras.layers.Flatten(name='flatten'),
keras.layers.Dense(1, activation=keras.activations.sigmoid, name='prob')
 ], name='sequential0' )
X_train = Change(X_train)
 X_test = Change(X_test )<choose_model_class>
reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1,
patience=5, min_lr=1e-9, verbose=1)
 early_stop = keras.callbacks.EarlyStopping(monitor='loss', min_delta=1e-6, patience=5, verbose=True)
 model.compile(
 loss='binary_crossentropy',
optimizer=keras.optimizers.SGD(lr=0.0001, decay=0),
metrics=['accuracy'],
verbose=False
)
model = XGBRegressor(colsample_bytree=1,
gamma=0.5,
learning_rate=0.005,
max_depth=9,
min_child_weight=1.5,
n_estimators=5000,
reg_alpha=0.4,
reg_lambda=0.45,
subsample=0.6,
seed=42 )<train_model>
history = model.fit(
 x=train_X,
y=train_y,
validation_data=(val_X, val_y),
epochs=150, batch_size=50,
callbacks=[reduce_lr]
)
model.fit(X_train, y_train)
 model.score(X_test,y_test)*100<create_dataframe>
pred_y = model.predict(train_X )
SalePrice = pd.DataFrame(model.predict(Change(TF)))
 Id = pd.DataFrame(TF.Id)
 result = pd.concat([Id, SalePrice], axis=1)
 result.columns = ['Id', 'SalePrice']<save_to_csv>
print('validation accuracy: {}'.format(sklearn.metrics.roc_auc_score(train_y, pred_y)) )
result.to_csv('submission.csv',index=False )<load_from_csv>
output_df = pd.read_csv('.. /input/sample_submission.csv')
 test_X = []
for idx, row in output_df.iterrows() :
test_X.append(imread('.. /input/test/test/' + row['id'])/255.)
train = pd.read_csv(".. /input/train.csv")
 print(train.shape)
 train.head()<count_values>
test_y = model.predict(test_X )
z_train = Counter(train['label'])
 z_train<load_from_csv>
output_df['has_cactus'] = test_y
test= pd.read_csv(".. /input/test.csv")
 print(test.shape)
 test.head()<data_type_conversions>
output_df.to_csv('submission.csv', index=False )
x_train =(train.ix[:,1:].values ).astype('float32')
 y_train = train.ix[:,0].values.astype('int32')
 x_test = test.values.astype('float32' )<feature_engineering>
model.save_weights('weights.hdf' )