Kaggle competition: Aerial Cactus Identification
pred_t = model.predict(xgb.DMatrix(data_with_imputed_values))
train_copy['Prediction'] =(pred_t > 0.4 ).astype(int)
 tn, fp, fn, tp = confusion_matrix(train_copy['Survived'], train_copy['Prediction'] ).ravel() 
(tn, fp, fn, tp )<save_to_csv>
from fastai import *
from fastai.vision import *
import pandas as pd
import numpy as np
<train_on_grid>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/sample_submission.csv" )
def runXGBcv(X, y, feature_names=None, seed_val=2017, num_rounds=100):
param = {}
param['objective'] = 'binary:logistic'
param['eta'] = 0.05
param['max_depth'] = 6
param['silent'] = 1
param['eval_metric'] = 'auc'
param['min_child_weight'] = 3
param['subsample'] = 0.5
param['colsample_bytree'] = 0.5
param['seed'] = seed_val
num_rounds = num_rounds
plst = list(param.items())
 xgtrain = xgb.DMatrix(X, label=y)
 result = xgb.cv(plst, xgtrain, num_rounds, nfold=5,stratified=True,
folds=None, metrics=() , obj=None, feval=None, maximize=False,
early_stopping_rounds=20, fpreproc=None, as_pandas=True,
verbose_eval=None, show_stdv=True, seed=2019, callbacks=None, shuffle=True)
 return result
result = runXGBcv(data_with_imputed_values, y_train)
<choose_model_class>
test_img = ImageList.from_df(test_df,
path=data_folder/'test',
folder='test' 
)
 train_img =(
 ImageList.from_df(train_df,
path=data_folder/'train',
folder='train')
.split_by_rand_pct(0.2)
.label_from_df() 
.add_test(test_img)
.transform(get_transforms(flip_vert=True), size=128)
.databunch(path='.', bs=64)
.normalize(imagenet_stats)
 )
clf = xgb.XGBClassifier(
 max_depth = 7,
n_estimators=700,
learning_rate=0.1,
nthread=4,
subsample=1.0,
colsample_bytree=0.5,
min_child_weight = 3,
seed=1301)
 xgb_param = clf.get_xgb_params()
cvresult = xgb.cv(xgb_param, xgb.DMatrix(data_with_imputed_values,y_train),
num_boost_round=400, nfold=15, metrics=['auc'],
early_stopping_rounds=30,
stratified=True,
as_pandas = True, show_stdv= True, seed=1301)
 print('Best number of trees = {}'.format(cvresult.shape[0]))
clf.set_params(n_estimators=cvresult.shape[0])
 clf.fit(data_with_imputed_values, y_train, eval_metric='auc')
 print('Overall AUC:', roc_auc_score(y_train, clf.predict_proba(data_with_imputed_values)[:,1])) 
<train_model>
learn = cnn_learner(train_img,
models.resnet34,
metrics=[error_rate, accuracy]
)
bclf = BalancedRandomForestClassifier(n_estimators=50, random_state=5)
 X_t, X_v, y_t, y_v = train_test_split(data_with_imputed_values,y_train, stratify=y_train, test_size=0.1, random_state=0)
 bclf.fit(X_t, y_t)
 print(classification_report(y_v, bclf.predict(X_v)))
<save_to_csv>
learn.fit_one_cycle(4 )
bclf.fit(data_with_imputed_values, y_train)
 print('Predict based on features in the test set')
 pred_bclf = bclf.predict(test_data_with_imputed_values)
<split>
learn.save('model-1' )
X_t, X_v, y_t, y_v = train_test_split(data_with_imputed_values,y_train, stratify=y_train, test_size=0.1, random_state=0)
 dtrain = xgb.DMatrix(X_t, label=y_t)
 dtest = xgb.DMatrix(X_v, label=y_v)
 def xgb_evaluate(max_depth, gamma, colsample_bytree):
params = {'eval_metric': 'auc',
'max_depth': int(max_depth),
'subsample': 0.8,
'eta': 0.1,
'gamma': gamma,
'colsample_bytree': colsample_bytree}
cv_result = xgb.cv(params, dtrain, num_boost_round=100, nfold=3)
 return -1.0 * cv_result['test-auc-mean'].iloc[-1]
xgb_bo = BayesianOptimization(xgb_evaluate, {'max_depth':(3, 7),
'gamma':(0, 1),
'colsample_bytree':(0.3, 0.9)})
 xgb_bo.maximize(init_points=3, n_iter=5, acq='ei')
<find_best_params>
interp = ClassificationInterpretation.from_learner(learn )
params_xgb = xgb_bo.max['params']
params_xgb['max_depth'] = int(params_xgb['max_depth'])
 params_xgb<train_model>
learn.fit_one_cycle(1 )
model2 = xgb.train(params_xgb, dtrain, num_boost_round=250)
 y_test_pred = model2.predict(dtest)>0.5
y_train_pred = model2.predict(dtrain)>0.5
print(classification_report(y_v, y_test_pred))
print(classification_report(y_t, y_train_pred))
model3 = xgb.train(params_xgb, xgb.DMatrix(data_with_imputed_values, label=y_train), num_boost_round=250)
 xgbpred =(model3.predict(xgb.DMatrix(test_data_with_imputed_values)) > 0.5 ).astype(int)
 out_df = pd.DataFrame({"PassengerId":test["PassengerId"].values})
 out_df['Survived'] = xgbpred
print(out_df.head())
 out_df.Survived.value_counts()<choose_model_class>
learn.lr_find()
cb_model = CatBoostClassifier(
)
 cb_model.fit(X_t, y_t,
eval_set=(X_v,y_v),
use_best_model=True, 
)
 print(classification_report(y_v, cb_model.predict(X_v)))
 print(cb_model.get_params())
 cat_params = cb_model.get_params()
cb_model = CatBoostClassifier(**cat_params)
 cb_model.fit(data_with_imputed_values, y_train, 
)
 catpred = cb_model.predict(test_data_with_imputed_values)
<find_best_params>
learn.fit_one_cycle(4, max_lr=slice(1e-05))
clf.get_booster().get_score(importance_type='weight' )<predict_on_test>
preds,_ = learn.get_preds(ds_type=DatasetType.Test )
pred_t = clf.predict(data_with_imputed_values, ntree_limit=cvresult.shape[0])
 train_copy['Prediction'] = pred_t
tn, fp, fn, tp = confusion_matrix(train_copy['Survived'], train_copy['Prediction'] ).ravel() 
(tn, fp, fn, tp )<predict_on_test>
test_df.has_cactus = preds.numpy() [:, 0]
<count_values><EOS> 
test_df.to_csv('submission.csv', index=False )