Kaggle competition: Aerial Cactus Identification
X_train, X_val, y_train, y_val = train_test_split(X_train_TM, y_log, test_size = 0.3,random_state = 0)
 xgb_model.fit(X_train, y_train)
 y_val_pred_xgb = np.expm1(xgb_model.predict(X_val))
print('XGB:', np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_xgb)))
 gbr_model.fit(X_train, y_train)
 y_val_pred_gbr = np.expm1(gbr_model.predict(X_val))
gbr_score = np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_gbr))
print('GradientBoosting:', gbr_score)
 lgb_model.fit(X_train, y_train)
 y_val_pred_lgb = np.expm1(lgb_model.predict(X_val))
lgb_score = np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_lgb))
print('LightGBM:', lgb_score)
 X_train, X_val, y_train, y_val=train_test_split(X_train_LM, y_log, test_size = 0.3, random_state = 0)
 lasso_model.fit(X_train, y_train)
 y_val_pred_lasso = np.expm1(lasso_model.predict(X_val))
print('Lasso:', np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_lasso)))
 elastic_model.fit(X_train, y_train)
 y_val_pred_elastic = np.expm1(elastic_model.predict(X_val))
print("ElasticNet:",np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_elastic)))
 bridge_model.fit(X_train, y_train)
 y_val_pred_bridge = np.expm1(bridge_model.predict(X_val))
print('BRidge:',np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_bridge)))
 svr_model.fit(X_train, y_train)
 y_val_pred_svr = np.expm1(svr_model.predict(X_val))
print('SVR:',np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_svr)))
 X_train, X_val, y_train, y_val = train_test_split(X_train_LM2, y_log, test_size=0.3,random_state = 0)
 kridge_model.fit(X_train, y_train)
 y_val_pred_kridge = np.expm1(kridge_model.predict(X_val))
print('Kridge:', np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_kridge)) )<prepare_output>
def load_data(dataframe=None, batch_size=1024, mode='categorical'):
if dataframe is None:
dataframe = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 dataframe['has_cactus'] = dataframe['has_cactus'].apply(str)
 gen = ImageDataGenerator(rescale=1./255.,
validation_split=0.1, horizontal_flip=True, vertical_flip=True,rotation_range = 3,
width_shift_range = 0.1,height_shift_range = 0.1,shear_range = 0.1)
 trainGen = gen.flow_from_dataframe(dataframe, directory='.. /input/aerial-cactus-identification/train/train', x_col='id', y_col='has_cactus', has_ext=True, target_size=(32, 32),
class_mode=mode, batch_size=batch_size, shuffle=True, subset='training')
 testGen = gen.flow_from_dataframe(dataframe, directory='.. /input/aerial-cactus-identification/train/train', x_col='id', y_col='has_cactus', has_ext=True, target_size=(32, 32),
class_mode=mode, batch_size=batch_size, shuffle=True, subset='validation')
 return trainGen, testGen
d = {'y': np.expm1(y_val), 'XGB': y_val_pred_xgb, 'GBR': y_val_pred_gbr, 'LGB': y_val_pred_lgb,
'Lasso': y_val_pred_lasso, 'ElasticNet': y_val_pred_elastic, 'BRidge': y_val_pred_bridge,
'SVR': y_val_pred_svr, 'KRidge': y_val_pred_kridge}
prediction_val = pd.DataFrame(data = d)
 prediction_val_x = prediction_val[['ElasticNet','BRidge','GBR','SVR']]
prediction_val_y = prediction_val['y']
prediction_val.head(10 )<compute_train_metric>
def baseline_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(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(2, activation='softmax'))
return model
reg = LinearRegression()
reg.fit(prediction_val_x, prediction_val_y)
 stacked_prediction = reg.predict(prediction_val_x)
 c = prediction_val_x.columns.values
for i in range(0, len(reg.coef_)) :
print(np.round(reg.coef_[i], decimals = 3 ).astype(str)+ ' x ' + c[i])
 print('Score:' + np.sqrt(mean_squared_log_error(np.expm1(y_val), stacked_prediction)).astype(str))<train_model>
def train_baseline() :
batch_size = 1024
trainGen, valGen = load_data(batch_size=batch_size)
 model = baseline_model()
model.load_weights('.. /input/kernel658e346ac9/baseline.h5')
 opt = Adam(1e-4)
 model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
 cbs = [ReduceLROnPlateau(monitor='loss', factor=0.1, patience=1, min_lr=1e-8, verbose=1)]
model.fit_generator(trainGen, steps_per_epoch=1000, epochs=10, validation_data=valGen,
validation_steps=100, shuffle=True, callbacks=cbs)
 return model
def predict_baseline(model):
testdf = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 pred = np.empty(( testdf.shape[0],))
for n in tqdm(range(testdf.shape[0])) :
data = np.array(Image.open('.. /input/aerial-cactus-identification/test/test/'+testdf.id[n]))
data = data.astype(np.float32)/ 255.
 pred[n] = model.predict(data.reshape(( 1, 32, 32, 3)))[0][1]
testdf['has_cactus'] = pred
testdf.to_csv('sample_submission.csv', index=False )
<train_model><EOS> 
model = train_baseline()
model.save('baseline.h5')
 predict_baseline(model )