Kaggle competition: Aerial Cactus Identification
scaler = RobustScaler(with_centering=True)
 X_scale = pd.DataFrame(scaler.fit_transform(X),
columns=X.columns)
 test_scale = pd.DataFrame(scaler.transform(test),
columns=test.columns )<compute_test_metric>
!pip install pytorchcv
def RMSE(actual, predicted):
return sqrt(mean_squared_error(actual, predicted))
SEED = 2007
common_args = {
'random_state': SEED,
} 
<compute_train_metric>
import numpy as np
import pandas as pd
from pytorchcv.model_provider import get_model as ptcv_get_model
from pathlib import Path
from fastai import *
from fastai.vision import *
import torch
cv = KFold(n_splits=10, shuffle=True, random_state=SEED)
 def cross_val(models, X=X, y=y):
r = dict()
for name, model in models.items() :
cv_results = np.sqrt(
 -cross_val_score(model, X, y,
cv=cv,
scoring='neg_mean_squared_error')
)
 r[name] = cv_results
print('Cross validation ready!')
 return r
def choose_best(results):
errors = dict()
for name, arr in results.items() :
errors[name] = arr.mean()
print(name, 'RMSE {0:.4f}, Std {1:.4f}'.format(
 errors[name], arr.std()))
best_model = [m for m, e in errors.items()
if e == min(errors.values())][0]
return best_model<save_to_csv>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/sample_submission.csv" )
def output(predicts, filename):
output = pd.DataFrame({'Id': test.index,
'SalePrice': predicts})
 output.to_csv(filename + '.csv', index=False)
 def train_predict_out(model, filename, X=X, y=y, test=test):
model.fit(X, y)
 predicts = un_norm(model.predict(test))
output(predicts, filename)
 return predicts<choose_model_class>
learn = cnn_learner(train_img, models.resnet152, metrics=[error_rate, accuracy] )
lin_models = {
'Lasso': LassoCV(cv=cv,
**common_args 
),
'LassoLars': LassoLarsCV(cv=cv 
),
}
r = cross_val(lin_models, X_scale)
 best_linear = choose_best(r)
 linear_model = lin_models[best_linear] 
<choose_model_class>
lr = 2e-02
learn.fit_one_cycle(4, slice(lr))
tree_models = {
'Random Forest': RandomForestRegressor(n_estimators=100,
max_depth=10,
max_features='sqrt',
warm_start=False,
**common_args),
'XGBoost': XGBRegressor(n_estimators=2000,
max_depth=3,
learning_rate=0.01,
verbosity=0,
objective='reg:squarederror',
**common_args),
}
results = cross_val(tree_models, X)
 best_treemodel = choose_best(results)
 tree_model = tree_models[best_treemodel]<predict_on_test>
preds,_ = learn.get_preds(ds_type=DatasetType.Test )
lin_predicts = train_predict_out(linear_model, 'linear',
X=X_scale, test=test_scale)
 tre_predicts = train_predict_out(tree_model, 'tree')
 blend_predicts =(lin_predicts + tre_predicts)/ 2
output(blend_predicts, 'blended' )<load_from_csv>
test_df.has_cactus = preds.numpy() [:, 0]
<count_missing_values><EOS> 
test_df.to_csv('samplesubmission.csv', index=False )