Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<find_best_params>
%matplotlib inline
%reload_ext autoreload
%autoreload 2
InteractiveShell.ast_node_interactivity = "all"
random_forest.get_params()<define_search_space>
from fastai.vision import *
from pathlib import Path
n_estimators = [int(x)for x in np.linspace(start = 100, stop = 1000, num = 10)]
max_features = ['auto', 'sqrt', 'log2']
max_depth = [int(x)for x in np.linspace(10, 110, num = 11)]
max_depth.append(None)
 min_samples_split = [2, 5, 10]
min_samples_leaf = [1, 2, 4]
bootstrap = [True, False]
random_grid = {'n_estimators': n_estimators,
'max_features': max_features,
'max_depth': max_depth,
'min_samples_split': min_samples_split,
'min_samples_leaf': min_samples_leaf,
'bootstrap': bootstrap}
random_grid<train_on_grid>
train_df = pd.read_csv(root/"train.csv")
 test_df = pd.read_csv(root/"sample_submission.csv" )
random_forest_random = RandomizedSearchCV(estimator = random_forest, param_distributions = random_grid, n_iter = 100, verbose=2, cv = 3, random_state=42, n_jobs = -1)
 random_forest_random.fit(X_train, Y_train )<find_best_params>
test_set = ImageList.from_df(test_df, path=root/'test', cols='id', folder='test' )
random_forest_random.best_params_<train_model>
tsfm = get_transforms(do_flip=True, flip_vert=True, max_rotate=10.0, max_zoom=1.1, max_lighting=0.2, max_warp=0.2, p_affine=0.75, p_lighting=0.75 )
random_forest_2 = random_forest_random.best_estimator_
random_forest_2.fit(X_train, Y_train)
 random_forest_acc_2 = round(cross_val_score(random_forest_2, X_train, Y_train, cv=10, scoring = "accuracy" ).mean() *100, 4)
 model_score = model_score.append(pd.DataFrame({'Model': ['Random Forest 2'], 'Score': [random_forest_acc_2]}))
model_score.sort_values(by='Score', ascending=False ).reset_index().drop('index', axis=1 )<train_on_grid>
SZ=128
BS=64
param_grid = {
'bootstrap': [True],
'max_depth': [20, 40, 60, 80, 100],
'max_features': ['log2'],
'min_samples_leaf': [3, 4, 5],
'min_samples_split': [4, 5, 6],
'n_estimators': [100, 200, 300, 700, 1000]
}
random_forest_grid_search = GridSearchCV(estimator = random_forest, param_grid = param_grid, cv = 3, n_jobs = -1, verbose = 2)
 random_forest_grid_search.fit(X_train, Y_train )<find_best_params>
data.show_batch(rows=3, figsize=(6,6))
random_forest_grid_search.best_params_<train_model>
arch = models.densenet169
random_forest_3 = random_forest_grid_search.best_estimator_
random_forest_3.fit(X_train, Y_train)
 random_forest_acc_3 = round(cross_val_score(random_forest_3, X_train, Y_train, cv=10, scoring = "accuracy" ).mean() *100, 4)
 model_score = model_score.append(pd.DataFrame({'Model': ['Random Forest 3'], 'Score': [random_forest_acc_3]}))
model_score.sort_values(by='Score', ascending=False ).reset_index().drop('index', axis=1 )<compute_train_metric>
learn = cnn_learner(data, arch, metrics=[error_rate, accuracy] )
predictions = cross_val_predict(random_forest_3, X_train, Y_train, cv=3)
 confusion_matrix(Y_train, predictions )<compute_test_metric>
lr = 1e-03
learn.fit_one_cycle(5, slice(lr))
print("Precision:", precision_score(Y_train, predictions))
print("Recall:",recall_score(Y_train, predictions))<compute_train_metric>
preds,_ = learn.get_preds(ds_type=DatasetType.Test )
y_scores = random_forest_3.predict_proba(X_train)
 y_scores = y_scores[:,1]
r_a_score = roc_auc_score(Y_train, y_scores)
 print("ROC-AUC-Score:", r_a_score )<save_to_csv>
test_df.has_cactus = preds.numpy() [:, 0]
<load_from_csv><EOS> 
test_df.to_csv('submission.csv', index=False )