Kaggle competition: Dog Breed Identification
def clf_performance(classifier, model_name):
print(model_name)
 print('Best Score: ' + str(classifier.best_score_))
print('Best Parameters: ' + str(classifier.best_params_))<train_on_grid>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
lr = LogisticRegression()
param_grid = {'max_iter' : [2000],
'penalty' : ['l1', 'l2'],
'C' : np.logspace(-4, 4, 20),
'solver' : ['liblinear']}
clf_lr = GridSearchCV(lr, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_lr = clf_lr.fit(X_train,y_train)
 clf_performance(best_clf_lr,'Logistic Regression' )<train_on_grid>
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 *
knn = KNeighborsClassifier()
param_grid = {'n_neighbors' : [3,5,7,9],
'weights' : ['uniform', 'distance'],
'algorithm' : ['auto', 'ball_tree','kd_tree'],
'p' : [1,2]}
clf_knn = GridSearchCV(knn, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_knn = clf_knn.fit(X_train,y_train)
 clf_performance(best_clf_knn,'KNN' )<define_search_space>
torch.cuda.set_device(0 )
svc = SVC(probability = True)
 param_grid = tuned_parameters = [{'kernel': ['rbf'], 'gamma': [.1,.5,1,2,5,10],
'C': [.1, 1, 10, 100, 1000]},
{'kernel': ['linear'], 'C': [.1, 1, 10, 100, 1000]},
{'kernel': ['poly'], 'degree' : [2,3,4,5], 'C': [.1, 1, 10, 100, 1000]}]
clf_svc = GridSearchCV(svc, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_svc = clf_svc.fit(X_train,y_train)
 clf_performance(best_clf_svc,'SVC' )<choose_model_class>
torch.backends.cudnn.enabled
rf = RandomForestClassifier(random_state = 1)
 param_grid = {'n_estimators': [400,450,500,550],
'criterion':['gini','entropy'],
'bootstrap': [True],
'max_depth': [15, 20, 25],
'max_features': ['auto','sqrt', 10],
'min_samples_leaf': [2,3],
'min_samples_split': [2,3]}
clf_rf = GridSearchCV(rf, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_rf = clf_rf.fit(X_train,y_train)
 clf_performance(best_clf_rf,'Random Forest' )<train_on_grid>
PATH = '.. /input/'
sz = 224
arch = resnet101
bs = 128
param_grid = {
'n_estimators': [450,500,550],
'colsample_bytree': [0.75,0.8,0.85],
'max_depth': [10],
'reg_alpha': [1],
'reg_lambda': [2, 5, 10],
'subsample': [0.55, 0.6,.65],
'learning_rate':[0.5],
'gamma':[.5,1,2],
'min_child_weight':[0.01],
'sampling_method': ['uniform']
}
clf_xgb = GridSearchCV(xgb, param_grid = param_grid, cv = 5, verbose = True, n_jobs = -1)
 best_clf_xgb = clf_xgb.fit(X_train,y_train)
 clf_performance(best_clf_xgb,'XGB' )<save_to_csv>
label_csv = f'{PATH}labels.csv'
n = len(list(open(label_csv)))- 1
val_idxs = get_cv_idxs(n )
test_result = pd.Series(best_clf_xgb.predict(test), name = "Survived" ).astype(int)
 results = pd.concat([test_data["PassengerId"], test_result],axis = 1)
 results.to_csv("titanic_submission1.csv", index = False)
<set_options>
def get_data(sz,bs):
tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
 data = ImageClassifierData.from_csv('.', 'train', label_csv, test_name='test',
val_idxs=val_idxs, suffix='.jpg', tfms=tfms, bs=bs)
 return data if sz > 300 else data.resize(340, 'tmp' )
SEED = 7
print("Setup complete." )<load_from_csv>
data = get_data(sz,bs )
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 datasets = [train, test]
train<feature_engineering>
learn = ConvLearner.pretrained(arch,data,precompute=True )
for ds in datasets:
def rand_ages() :
np.random.seed(SEED)
 return np.random.randint(low=ds['Age'].mean() - ds['Age'].std() ,
high=ds['Age'].mean() + ds['Age'].std() ,
size=ds['Age'].isnull().sum())
 ds.loc[ds['Age'].isnull() , 'Age'] = rand_ages()
ds['Age'] = pd.cut(
 ds['Age'], bins=[-np.inf, 14, 24, 64, np.inf], labels=range(4))
ds.loc[:, 'Age'] = ds['Age'].astype(int)
 ax = sns.countplot(data=train, x='Age', hue='Survived')
 ax.set_xlabel('Age category')
 plt.show()<categorify>
learning_rate = learn.lr_find()
learn.sched.plot()
def encode_freq_sorted(feature):
sorted_indices = feature.value_counts().index
sorted_dict = dict(zip(sorted_indices, range(len(sorted_indices))))
return feature.map(sorted_dict ).astype(int)
 for ds in datasets:
ds['Sex'] = encode_freq_sorted(ds['Sex'])
 ds['Embarked'] = encode_freq_sorted(ds['Embarked'])
 ds['Title'] = encode_freq_sorted(ds['Title'])
 train.head()<define_variables>
learn.fit(1e-2, 5 )
drop_features = ['Name', 'SibSp', 'Parch', 'Ticket', 'Cabin']<prepare_x_and_y>
from sklearn import metrics
drop_features.extend(['Pclass'])
 train = train.drop(columns=drop_features)
 test = test.drop(columns=drop_features)
 X = train.drop(columns=['PassengerId', 'Survived'])
 y = train['Survived']
X.head()<split>
log_preds, y = learn.TTA(is_test=True)
 probs = np.mean(np.exp(log_preds),0 )
X_train, X_val, y_train, y_val = train_test_split(X, y, train_size=0.75,
random_state=SEED)
 X_test = test.drop(columns=['PassengerId'] )<train_on_grid>
df = pd.DataFrame(probs)
 df.columns = data.classes
cross_valid = StratifiedKFold(n_splits=3, shuffle=True, random_state=SEED)
 def random_search(X, y, estimator, params, score="accuracy", cv=cross_valid,
n_iter=100, random_state=SEED, n_jobs=-1):

 print("
classifier = RandomizedSearchCV(estimator=estimator, param_distributions=params,
scoring=score, cv=cv, n_iter=n_iter, n_jobs=n_jobs,
random_state=random_state)
 classifier.fit(X, y)
 print("Best parameters by random search:
", classifier.best_params_)
 return classifier<find_best_params>
df.insert(0, 'id', [o[5:-4] for o in data.test_ds.fnames])
 df.head()
<init_hyperparams><EOS> 
df.to_csv('submission.csv', index=False )