Kaggle competition: Aerial Cactus Identification
X = X_train[features]
Xt = X_test[features]<split>
from sklearn.model_selection import train_test_split
from keras import layers, models
from keras.utils import to_categorical
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from keras.optimizers import Adam, Adamax
from keras.regularizers import l2
import glob
import cv2
Xtrain1, Xtest1, ytrain1, ytest1 = train_test_split(X, Y_train, test_size=0.33 )<compute_train_metric>
path=".. /input/"
train_path=path+"train/train/"
test_path=path+"test/test/"
ridge = Ridge(alpha=5, solver='svd' ).fit(Xtrain1, ytrain1)
 val_ridge = np.expm1(ridge.predict(Xtest1))
rmlse(val_ridge, ytest1 )<train_on_grid>
files = sorted(glob.glob(train_path + '*.jpg'))
files[:3]
train_raw = np.array([cv2.imread(image)
 for image in files], dtype='int32' )
las_cv = LassoCV(alphas=(0.0001, 0.0005, 0.001, 0.01, 0.05, 0.1, 0.3, 1, 3, 5, 10)).fit(Xtrain1, ytrain1)
 val_las = np.expm1(las_cv.predict(Xtest1))
rmlse(val_las, ytest1 )<train_model>
test_files = sorted(glob.glob(test_path + '*.jpg'))
test_files[:3]
test_raw = np.array([cv2.imread(image)
 for image in test_files], dtype='int32' )
model_xgb = xgb.XGBRegressor(n_estimators=340, max_depth=2, learning_rate=0.2)
 model_xgb.fit(Xtrain1, ytrain1)
 xgb_preds = np.expm1(model_xgb.predict(Xtest1))
rmlse(xgb_preds, ytest1 )<train_model>
train_images = train_raw/255
test_images = test_raw/255
forest = RandomForestRegressor(min_samples_split =5,
min_weight_fraction_leaf = 0.0,
max_leaf_nodes = None,
max_depth = 100,
n_estimators = 300,
max_features = None)
 forest.fit(Xtrain1, ytrain1)
 Y_pred_RF = np.expm1(forest.predict(Xtest1))
rmlse(Y_pred_RF, ytest1 )<train_on_grid>
train_set = pd.read_csv(path + 'train.csv')
 train_labels = train_set['has_cactus']
train_labels = to_categorical(train_labels )
las_cv1 = LassoCV(alphas=(0.0001, 0.0005, 0.001, 0.01, 0.05, 0.1, 0.3, 1, 3, 5, 10))
las_cv1.fit(X, Y_train)
 lasso_preds = np.expm1(las_cv1.predict(Xt))<train_model>
model = models.Sequential()
model.add(layers.Conv2D(16,(3, 3), activation='relu', input_shape=(32, 32, 3), padding='same', kernel_initializer='he_uniform'))
model.add(layers.Conv2D(32,(3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.Conv2D(32,(3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Conv2D(64,(3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.Conv2D(64,(3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.Conv2D(64,(3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Conv2D(512,(3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.Conv2D(512,(3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.Conv2D(512,(3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Conv2D(32,(2, 2), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.Conv2D(32,(2, 2), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.Conv2D(32,(2, 2), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.BatchNormalization())
 model.add(layers.Conv2D(64,(2, 2), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.Conv2D(64,(2, 2), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.Conv2D(64,(2, 2), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Conv2D(128,(2, 2), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.Conv2D(128,(2, 2), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.Conv2D(128,(2, 2), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.Conv2D(128,(2, 2), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Dense(128, kernel_regularizer=l2(5e-4)))
 model.add(layers.Dense(64, kernel_regularizer=l2(5e-4)))
 model.add(layers.Dense(64, kernel_regularizer=l2(5e-4)))
 model.add(layers.Dense(32, kernel_regularizer=l2(5e-4)))
 model.add(layers.Flatten())
 model.add(layers.Dense(256))
model.add(layers.Dense(256))
model.add(layers.Dense(32))
model.add(layers.Dense(2, activation='softmax'))
model_xgb = xgb.XGBRegressor(n_estimators=340, max_depth=2, learning_rate=0.1)
 model_xgb.fit(X, Y_train)
 xgb_preds = np.expm1(model_xgb.predict(Xt))<prepare_output>
cbl = [
EarlyStopping(
 monitor='val_loss',
patience=10, 
),
ModelCheckpoint(
 filepath='best_model.h5',
monitor='val_loss',
save_best_only=True, 
),
ReduceLROnPlateau(
 monitor='val_loss',
factor=0.1,
patience=10 
)
 ]
preds = 0.7 * lasso_preds + 0.3 * xgb_preds<save_to_csv>
model.compile(optimizer=Adam() ,
loss='binary_crossentropy',
metrics=['accuracy', ],
)
submission = pd.DataFrame({
'Id': house_test['Id'].astype(int),
'SalePrice': preds
})
 submission.to_csv('home.csv', index=False )<train_on_grid>
%%time
history = model.fit(
 train_images,
train_labels,
validation_split=0.01,
epochs=200,
batch_size=32,
callbacks=cbl
)
model_lasso = LassoCV(alphas=(0.0001, 0.0005, 0.001, 0.01, 0.05, 0.1, 0.3, 1, 3, 5, 10, 100))
model_lasso.fit(X_train, Y_train)
 coef = pd.Series(model_lasso.coef_, index = X_train.columns)
 v1 = coef.loc[model_lasso.coef_ != 0].count()
print('So we have ' + str(v1)+ ' variables' )<feature_engineering>
predicted_data = model.predict(test_images)
 predicted_data
for i in features_f:
if i not in X_test.columns:
X_test[i] = 0
print(i)
 X = X_train[features_f]
Xt = X_test[features_f]<train_on_grid>
test_data = pd.read_csv(path + 'sample_submission.csv')
 test_data['has_cactus'] = predicted_data
<train_model><EOS> 
test_data.to_csv('sample_submission.csv', index=False )