Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<categorify>
import numpy as np
import pandas as pd
from pathlib import Path
from fastai import *
from fastai.vision import *
import torch
cat_encoder = cat_pipeline.named_steps['encoder']
cat_encoder.categories_[:10]<categorify>
data_folder = Path(".. /input")
 train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/sample_submission.csv" )
cat_one_hot_attribs =[]
for cat in range(len(cat_encoder.categories_)) :
cat_one_hot_attribs = cat_one_hot_attribs + list(cat_encoder.categories_[cat] )<define_variables>
test_img = ImageList.from_df(test_df, path=data_folder/'test', folder='test' )
attributes = list(num_attributes)+ extra_attributes + cat_one_hot_attribs
attributes<import_modules>
src =(ImageList.from_df(train_df, path=data_folder/'train', folder='train')
.split_by_rand_pct(0.01)
.label_from_df() 
.add_test(test_img)
 )
from sklearn.metrics import mean_squared_error<import_modules>
train_img=src.databunch('.',bs=50 )
from sklearn.metrics import mean_squared_error<compute_train_metric>
train_img.show_batch()
lin_reg = LinearRegression()
lin_reg.fit(X_train_prepared, y_train)
 y_pred = lin_reg.predict(X_train_prepared)
 lin_mse = mean_squared_error(y_train, y_pred)
 lin_rmse = np.sqrt(lin_mse)
 lin_rmse<compute_train_metric>
tfms=get_transforms(flip_vert=True )
sgd_reg = SGDRegressor(penalty=None, random_state=42, max_iter=1000, tol=1e-6)
 sgd_reg.fit(X_train_prepared, y_train)
 y_pred = sgd_reg.predict(X_train_prepared)
 sgd_mse = mean_squared_error(y_train, y_pred)
 sgd_rmse = np.sqrt(sgd_mse)
 sgd_rmse<compute_train_metric>
train_img =(src.transform(tfms,size=128)
.databunch('.',bs=50)
 )
tree_reg = DecisionTreeRegressor(random_state=42)
 tree_reg.fit(X_train_prepared, y_train)
 y_pred = tree_reg.predict(X_train_prepared)
 tree_mse = mean_squared_error(y_train, y_pred)
 tree_rmse = np.sqrt(tree_mse)
 tree_rmse<compute_train_metric>
denselearner = cnn_learner(train_img, models.densenet161, metrics=[FBeta() ,error_rate, accuracy] )
forest_reg = RandomForestRegressor(random_state=42, n_estimators=100)
 forest_reg.fit(X_train_prepared, y_train)
 y_pred = forest_reg.predict(X_train_prepared)
 forest_mse = mean_squared_error(y_train, y_pred)
 forest_rmse = np.sqrt(forest_mse)
 forest_rmse<compute_train_metric>
denselearner.lr_find()
denselearner.recorder.plot(suggestion=True )
svm_reg = SVR(kernel="linear")
 svm_reg.fit(X_train_prepared, y_train)
 y_pred = svm_reg.predict(X_train_prepared)
 svm_mse = mean_squared_error(y_train, y_pred)
 svm_rmse = np.sqrt(svm_mse)
 svm_rmse<compute_test_metric>
lr = 7.5e-03
denselearner.fit_one_cycle(5, slice(lr))
def display_scores(scores):
print("Scores:", scores)
 print("Mean:", scores.mean())
 print("Standard deviation:", scores.std() )<compute_train_metric>
denselearner.unfreeze()
denselearner.lr_find()
denselearner.recorder.plot(suggestion=True )
sgd_scores = cross_val_score(sgd_reg, X_train_prepared, y_train, scoring="neg_mean_squared_error", cv=5)
 sgd_rmse_scores = np.sqrt(-sgd_scores)
 display_scores(sgd_rmse_scores )<compute_train_metric>
denselearner.fit_one_cycle(1, slice(1e-06))
tree_scores = cross_val_score(tree_reg, X_train_prepared, y_train, scoring="neg_mean_squared_error", cv=5)
 tree_rmse_scores = np.sqrt(-tree_scores)
 display_scores(tree_rmse_scores )<compute_train_metric>
reslearner = cnn_learner(train_img, models.resnet101, metrics=[FBeta() ,error_rate, accuracy] )
forest_scores = cross_val_score(forest_reg, X_train_prepared, y_train, scoring="neg_mean_squared_error", cv=5)
 forest_rmse_scores = np.sqrt(-forest_scores)
 display_scores(forest_rmse_scores )<compute_train_metric>
reslearner.lr_find()
svm_scores = cross_val_score(svm_reg, X_train_prepared, y_train, scoring="neg_mean_squared_error", cv=5)
 svm_rmse_scores = np.sqrt(-svm_scores)
 display_scores(svm_rmse_scores )<train_on_grid>
lr=9e-3
param_grid = [
{'n_estimators': [30, 50, 100],
'max_features': [30, 50, 100]},
{'bootstrap': [False],
'n_estimators': [30, 50, 100],
'max_features': [30, 50, 100]},
]
forest_reg = RandomForestRegressor(random_state=42)
 grid_search_forest = GridSearchCV(forest_reg, param_grid, cv=5, scoring='neg_mean_squared_error', return_train_score=True)
 grid_search_forest.fit(X_train_prepared, y_train )<find_best_params>
reslearner.fit_one_cycle(5,slice(lr))
grid_search_forest.best_params_<find_best_params>
reslearner.unfreeze()
reslearner.fit_one_cycle(2,slice(1e-6))
grid_search_forest.best_estimator_<compute_test_metric>
preds,_ = reslearner.get_preds(ds_type=DatasetType.Test )
cvres = grid_search_forest.cv_results_
for mean_score, params in zip(cvres["mean_test_score"], cvres["params"]):
print(np.sqrt(-mean_score), params )<train_on_grid>
test_df.has_cactus = preds.numpy() [:, 0]
<find_best_params><EOS> 
test_df.to_csv('submission.csv', index=False )