Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
train_X = train[predictors]
train_Y = train[target].values
valid_X = valid[predictors]
valid_Y = valid[target].values<train_model>
import pandas as pd
rf_clf = clf.fit(train_X, train_Y )<predict_on_test>
import pandas as pd
preds = clf.predict(valid_X )<compute_test_metric>
from fastai import *
from fastai.vision import *
from fastai.datasets import *
from fastai.metrics import *
from fastai.vision.data import *
from fastai.vision.image import *
clf.score(train_X, train_Y)
 acc = round(clf.score(train_X, train_Y)* 100, 2)
 print("RandomForest accuracy(train set):", acc )<compute_test_metric>
path = untar_data(URLs.DOGS )
clf.score(valid_X, valid_Y)
 acc = round(clf.score(valid_X, valid_Y)* 100, 2)
 print("RandomForest accuracy(validation set):", acc )<compute_test_metric>
from fastai.imports import *
print(metrics.classification_report(valid_Y, preds, target_names=['Not Survived', 'Survived']))<predict_on_test>
torch.cuda.is_available()
test_X = test_df[predictors]
pred_Y = clf.predict(test_X )<save_to_csv>
torch.backends.cudnn.enabled
submission = pd.DataFrame({"PassengerId": test_df["PassengerId"],"Survived": pred_Y})
 submission.to_csv('submission.csv', index=False )<train_model>
sz=224
rf_clf = clf.fit(train_X, train_Y )<define_search_space>
arch=models.resnet34
parameters = {
'n_estimators':(50, 75,100),
'max_features':('auto', 'sqrt'),
'max_depth':(3,4,5),
'min_samples_split':(2,5,10),
'min_samples_leaf':(1,2,3)
 }<train_on_grid>
data = ImageDataBunch.from_folder(path,test='test1',
ds_tfms=get_transforms() ,
size=sz,
num_workers=0 )
%%time
gs_clf = GridSearchCV(rf_clf, parameters, n_jobs=-1, cv = 5, verbose = 5)
 gs_clf = gs_clf.fit(train_X, train_Y )<train_model>
learn = create_cnn(data, models.resnet34, metrics=accuracy )
print('Best scores:',gs_clf.best_score_)
 print('Best params:',gs_clf.best_params_ )<predict_on_test>
learn.lr_find()
preds = gs_clf.predict(valid_X )<compute_test_metric>
learn.fit(3 )
clf.score(valid_X, valid_Y)
 acc = round(clf.score(valid_X, valid_Y)* 100, 2)
 print("RandomForest accuracy(validation set):", acc )<compute_test_metric>
submission = pd.DataFrame(os.listdir(".. /input/test"),columns = ['FileName'])
 submission['id']= submission['FileName'].apply(lambda s: s[:len(s)-4])
 submission['label']=0.5
submission.head()
print(metrics.classification_report(valid_Y, preds, target_names=['Not Survived', 'Survived']))<predict_on_test>
test_path = PATH+'test/'
test_path
test_X = test_df[predictors]
pred_Y = gs_clf.predict(test_X )<save_to_csv>
for i in range(10):
img = open_image(test_path+submission['FileName'][i])
 result = learn.predict(img)
 print(submission['FileName'][i],result[0],result[1] )
submission = pd.DataFrame({"PassengerId": test_df["PassengerId"],"Survived": pred_Y})
 submission.to_csv('submission_hyperparam_optimization.csv', index=False )<choose_model_class>
for i in range(len(submission)) :
img = open_image(test_path+submission['FileName'][i])
 result = learn.predict(img)
 submission.loc[i,'label']=float(result[2][1] )
NUMBER_KFOLDS = 5
kf = KFold(n_splits = NUMBER_KFOLDS, random_state = RANDOM_STATE, shuffle = True )<find_best_model_class>
submission[['id','label']].to_csv('submission.csv',index=False )