Kaggle competition: Aerial Cactus Identification
X_train, X_test, y_train, y_test = train_test_split(X_full, y_full, test_size=.25, random_state=0 )<normalization>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
split_scaler = preprocessing.StandardScaler().fit(X_train)
 X_train_standardized = split_scaler.transform(X_train)
 X_test_standardized = split_scaler.transform(X_test )<drop_column>
from fastai.vision import *
import fastai
if 'model_results' in globals() :
del(model_results )<train_on_grid>
path = Path('.. /input' )
def make_logit_model(X_train, y_train):
logitmodel = LogisticRegression(random_state=0,max_iter=500)
 gridsearch = GridSearchCV(logitmodel,param_grid={'C':np.logspace(-3,2,6), 'solver':['lbfgs','sag'],\
'tol':np.logspace(-3,1,5)}, cv=10,return_train_score=True,iid=True)\ 
.fit(X_train, y_train)
 logitmodel = gridsearch.best_estimator_
return logitmodel, gridsearch<choose_model_class>
train_df = pd.read_csv(path/'train.csv')
 train_df.head()
logitmodel, gridsearch = make_logit_model(X_train_standardized, y_train)
 pd.DataFrame(gridsearch.cv_results_)\ 
.loc[:,['params','mean_test_score','mean_train_score','rank_test_score']]\ 
.sort_values(by='rank_test_score' ).head(3)
 print('Setting logitmodel to {}'.format(gridsearch.best_params_))<predict_on_test>
test_df = pd.read_csv(path/'sample_submission.csv')
 print(test_df.shape)
 test_df.head()
logit_predictions = logitmodel.predict(X_test_standardized)
 probas_index_class1 = np.where(logitmodel.classes_==1)[0][0]
logit_probas = logitmodel.predict_proba(X_test_standardized)[:,probas_index_class1]
logit_accuracy = logitmodel.score(X_test_standardized, y_test)
 logit_crossvalscores = cross_val_score(logitmodel, X_train_standardized, y_train,cv=10)
<save_to_csv>
tfms = 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)
 train_data = ImageDataBunch.from_df(path/'train'/'train', train_df, ds_tfms=tfms, size=128 )
write_model_results('LogisticRegression', logit_accuracy, logit_crossvalscores, logit_predictions, y_test )<train_on_grid>
train_data.show_batch(rows=3, figsize=(5,6))
def make_adaboost_model(X_train, y_train):
adaboost = AdaBoostClassifier(n_estimators=50,learning_rate=1,random_state=0)
 gridsearch = GridSearchCV(adaboost,param_grid={'n_estimators':[10,60,200], 'learning_rate':np.linspace (.01,1,5), \
'random_state':[0]}, cv=10,return_train_score=True,iid=True)\ 
.fit(X_train, y_train)
 adaboost = gridsearch.best_estimator_
return adaboost, gridsearch<train_on_grid>
learn = cnn_learner(train_data, models.resnet50, metrics=[accuracy],model_dir="/tmp/model/" )
adaboost, gridsearch = make_adaboost_model(X_train_standardized, y_train)
 pd.DataFrame(gridsearch.cv_results_)\ 
.loc[:,['params','mean_test_score','mean_train_score','rank_test_score']]\ 
.sort_values(by='rank_test_score' ).head(3)
 print('Setting adaboost to {}'.format(gridsearch.best_params_))<compute_train_metric>
learn.lr_find()
adaboost_index_class1 = np.where(adaboost.classes_==1)[0][0]
adaboost_predictions = adaboost.predict(X_test_standardized)
 adaboost_probas = adaboost.predict_proba(X_test_standardized)[:,probas_index_class1]
adaboost_accuracy = adaboost.score(X_test_standardized,y_test)
 adaboost_crossvalscores = cross_val_score(adaboost, X_train_standardized, y_train,cv=10)
<save_to_csv>
lr =1.0e-2
learn.fit_one_cycle(7,slice(lr))
write_model_results('AdaBoost', adaboost_accuracy, adaboost_crossvalscores, adaboost_predictions, y_test )<train_on_grid>
learn.fit_one_cycle(5, slice(1e-4, lr/5))
def make_random_forest(X_train, y_train):
randomforest = RandomForestClassifier(n_estimators=100,random_state=0)
 gridsearch = GridSearchCV(randomforest,param_grid={'n_estimators':[60], 'max_depth':[2,3,7], \
'max_leaf_nodes':[100,300,500],'random_state':[0]}, cv=10,return_train_score=True, iid=True)\ 
.fit(X_train, y_train)
 randomforest = gridsearch.best_estimator_
return randomforest, gridsearch<find_best_params>
solution = pd.DataFrame(columns=test_df.columns)
 solution
randomforest, gridsearch = make_random_forest(X_train_standardized, y_train)
 pd.DataFrame(gridsearch.cv_results_)\ 
.loc[:,['params','mean_test_score','mean_train_score','rank_test_score']]\ 
.sort_values(by='rank_test_score' ).head(3)
 print('Setting randomforest to {}'.format(gridsearch.best_params_))<compute_train_metric>
for index,row in test_df.iterrows() :
img_name = row['id']
img = open_image(path/'test'/'test'/img_name)
 pred_class,pred_idx,outputs = learn.predict(img)
 solution.loc[len(solution)] = [img_name,outputs.numpy() [1]]
randomforest_index_class1 = np.where(randomforest.classes_==1)[0][0]
randomforest_predictions = randomforest.predict(X_test_standardized)
 randomforest_probas = randomforest.predict_proba(X_test_standardized)[:,probas_index_class1]
randomforest_accuracy = randomforest.score(X_test_standardized,y_test)
 randomforest_crossvalscores = cross_val_score(randomforest, X_train_standardized, y_train,cv=10 )<save_to_csv>
solution.to_csv('submission.csv', index=False )
write_model_results('RandomForest', randomforest_accuracy, randomforest_crossvalscores, randomforest_predictions,
y_test )<choose_model_class>
def create_download_link(df, title = "Download CSV file", filename = "submission.csv"):
csv = df.to_csv()
b64 = base64.b64encode(csv.encode())
 payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
 return HTML(html)
 df = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD'))
create_download_link(df)