Kaggle competition: Dog Breed Identification
<SOS> metric: MulticlassLoss Kaggle data source: dog-breed-identification<predict_on_test>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
y_pred = random_forest_tuned.predict(X_val)
 accuracy_random_forest = accuracy_score(y_val, y_pred)
 print("Accuracy:", accuracy_random_forest )<find_best_params>
from fastai.imports import *
from fastai.torch_imports import *
from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *
import numpy as np
import pandas as pd
import os
svc = SVC(probability=True, random_state=SEED)
 svc.get_params()<train_on_grid>
print(torch.cuda.is_available() , torch.backends.cudnn.enabled )
params = {
'C': scipy.stats.expon(scale=78),
'class_weight':['balanced', None],
'gamma': scipy.stats.expon(scale=.1),
'kernel':['rbf', 'linear']
}
svc_tuned = random_search(X_train, y_train, estimator=svc, params=params )<compute_test_metric>
comp_name = "dog_breed"
input_path = ".. /input/"
wd = "/kaggle/working/"
y_pred = svc_tuned.predict(X_val)
 accuracy_svc = accuracy_score(y_val, y_pred)
 print("Accuracy:", accuracy_svc )<find_best_params>
def create_symlnk(src_dir, src_name, dst_name, dst_dir=wd, target_is_dir=False):

 if not os.path.exists(dst_dir + dst_name):
os.symlink(src=src_dir + src_name, dst = dst_dir + src_name, target_is_directory=target_is_dir )
xgb = XGBClassifier(random_state=SEED, verbosity=0)
 xgb.get_params()<train_on_grid>
create_symlnk(input_path, "train", "train", target_is_dir=True)
 create_symlnk(input_path, "test", "test", target_is_dir=True)
 create_symlnk(input_path, "labels.csv", "labels.csv" )
params = {
'colsample_bytree': list(np.arange(0.6, 1.0, step=0.05)) ,
'gamma': list(np.arange(0.1, 15, step=0.2)) ,
'learning_rate': [0.01, 0.05, 0.1, 0.15, 0.21],
'max_depth': list(range(2, 12)) ,
'min_child_weight': list(range(1, 12)) ,
'n_estimators': [10, 100, 500, 1000],
'reg_alpha': [10**i for i in range(-5, 1)],
'subsample': list(np.arange(0.6, 1.0, step=0.05))
}
xgb_tuned = random_search(X_train, y_train, estimator=xgb, params=params )<predict_on_test>
label_df = pd.read_csv(f"{wd}labels.csv" )
y_pred = xgb_tuned.predict(X_val)
 accuracy_xgboost = accuracy_score(y_val, y_pred)
 print("Accuracy:", accuracy_xgboost )<find_best_params>
val_idxs = get_cv_idxs(label_df.shape[0] )
decision_tree = DecisionTreeClassifier(random_state=SEED)
 decision_tree.get_params()<train_on_grid>
arch = resnet101
sz = 224
bs = 64
params = {
'criterion': ["gini", "entropy"],
'max_depth': list(range(1, 32)) ,
'max_features': list(range(1, X_train.shape[1]+1)) ,
'min_samples_leaf': list(range(1, 9)) ,
'min_samples_split': list(np.arange(0.1, 1.1, step=0.1))
}
decision_tree_tuned = random_search(
 X_train, y_train, estimator=decision_tree, params=params )<predict_on_test>
tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
 data = ImageClassifierData.from_csv(path=wd, folder="train", csv_fname=f"{wd}labels.csv", tfms=tfms, val_idxs=val_idxs, suffix=".jpg", test_name="test" )
y_pred = decision_tree_tuned.predict(X_val)
 accuracy_decision_tree = accuracy_score(y_val, y_pred)
 print("Accuracy:", accuracy_decision_tree )<find_best_params>
[print(len(e)) for e in [data.trn_ds, data.val_ds, data.test_ds]]
knn = KNeighborsClassifier()
knn.get_params()<train_on_grid>
def get_data(sz=sz, bs=bs, data=data):

 return data if sz > 300 else data.resize(340, new_path=wd )
params = {
'leaf_size': list(range(20, 60)) ,
'n_neighbors': list(range(3, 30)) ,
'p': [1, 2]
}
knn_tuned = random_search(X_train, y_train, estimator=knn, params=params )<predict_on_test>
data2 = get_data()
y_pred = knn_tuned.predict(X_val)
 accuracy_knn = accuracy_score(y_val, y_pred)
 print("Accuracy:", accuracy_knn )<find_best_params>
learn = ConvLearner.pretrained(arch, data2, precompute=True )
logistic_regression = LogisticRegression(random_state=SEED)
 logistic_regression.get_params()<train_on_grid>
lrf = learn.lr_find()
params = {
'C': scipy.stats.loguniform(1e-4, 100),
'penalty': ['l1', 'l2', 'elasticnet'],
'solver': ['newton-cg', 'lbfgs', 'liblinear']
}
logistic_regression_tuned = random_search(
 X_train, y_train, estimator=logistic_regression, params=params )<compute_train_metric>
learn.fit(1e-1, 2 )
y_pred = logistic_regression_tuned.predict(X_val)
 accuracy_logistic_regression = accuracy_score(y_val, y_pred)
 print("Accuracy:", accuracy_logistic_regression )<set_options>
learn.precompute=False
learn.fit(1e-1, 5, cycle_len=1 )
naive_bayes = GaussianNB()
naive_bayes.get_params()<train_model>
learn.set_data(get_data(299, bs))
params = {
'var_smoothing': [np.exp(-i)for i in range(1, 15)]
}
naive_bayes_tuned = random_search(
 X_train, y_train, estimator=naive_bayes, params=params, n_iter=15-1 )<compute_train_metric>
learn.fit(1e-1, 3, cycle_len=1 )
y_pred = naive_bayes_tuned.predict(X_val)
 accuracy_naive_bayes = accuracy_score(y_val, y_pred)
 print("Accuracy:", accuracy_naive_bayes )<train_model>
log_preds, y = learn.TTA()
probs = np.mean(np.exp(log_preds), 0)
 accuracy_np(probs, y), log_loss(y, probs )
voting = VotingClassifier(
 estimators=[('rf', random_forest_tuned), 
('xgb', xgb_tuned), 
('knn', knn_tuned), 
('svc', svc_tuned), 
('lr', logistic_regression_tuned), 
('dt', decision_tree_tuned), 
('nb', naive_bayes_tuned)],
voting='soft',
n_jobs=-1)
 voting = voting.fit(X_train, y_train )<compute_test_metric>
log_preds_test, y_test = learn.TTA(is_test=True)
 probs_test = np.mean(np.exp(log_preds_test), 0 )
y_pred = voting.predict(X_val)
 accuracy_voting = accuracy_score(y_val, y_pred)
 print("Accuracy:", accuracy_voting )<save_to_csv>
df = pd.DataFrame(probs_test)
 df.columns = data.classes
model = voting
predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test['PassengerId'], 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("The results successfully saved!" )<set_options>
df.insert(0, "id", [e[5:-4] for e in data.test_ds.fnames] )
%matplotlib inline
sns.set_style('darkgrid' )<load_from_csv>
df.to_csv(f"sub_{comp_name}_{str(arch.__name__)}.csv", index=False )