Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<feature_engineering>
import numpy as np
import pandas as pd
from pathlib import Path
from fastai import *
from fastai.vision import *
from fastai.callbacks import *
import torch
for dataset in train_test_data:
dataset['Cabin'] = dataset['Cabin'].str[:1]<categorify>
data_folder = Path(".. /input")
cabin_mapping = {"A": 0, "B": 0.4, "C": 0.8, "D": 1.2, "E": 1.6, "F": 2, "G": 2.4, "T": 2.8}
for dataset in train_test_data:
dataset['Cabin'] = dataset['Cabin'].map(cabin_mapping )<categorify>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/sample_submission.csv" )
train["Cabin"].fillna(train.groupby("Pclass")["Cabin"].transform("median"), inplace=True)
 test["Cabin"].fillna(test.groupby("Pclass")["Cabin"].transform("median"), inplace=True )<feature_engineering>
callbacks = [partial(EarlyStoppingCallback, monitor='accuracy', min_delta=0.01, patience=3)]
train['Familysize'] = train["SibSp"] + train["Parch"] + 1
test['Familysize'] = test["SibSp"] + test["Parch"] + 1<categorify>
learn = cnn_learner(train_img, models.densenet161, metrics=[error_rate, accuracy], callback_fns=callbacks ).mixup().to_fp16()
family_mapping = {1: 0, 2: 0.4, 3: 0.8, 4: 1.2, 5: 1.6, 6: 2, 7: 2.4, 8: 2.8, 9: 3.2, 10: 3.6, 11: 4}
for dataset in train_test_data:
dataset['Familysize'] = dataset['Familysize'].map(family_mapping )<drop_column>
lr = 1e-02
learn.fit_one_cycle(3, slice(lr), callbacks=[SaveModelCallback(learn, every='improvement', monitor='quadratic_kappa', name='bestmodel')] )
features_drop = ['Ticket', 'SibSp', 'Parch']
train = train.drop(features_drop, axis=1)
 test = test.drop(features_drop, axis=1)
 train = train.drop(['PassengerId'], axis=1 )<prepare_x_and_y>
learn.fit_one_cycle(16, slice(1e-05, 1e-03), callbacks=[SaveModelCallback(learn, every='improvement', monitor='quadratic_kappa', name='bestmodel')] )
X = train.drop('Survived', axis=1)
 y = train['Survived']
X.shape, y.shape<import_modules>
learn.to_fp32()
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC<choose_model_class>
preds,_ = learn.TTA(ds_type=DatasetType.Test )
k_fold = KFold(n_splits=10, shuffle=True, random_state=0 )<compute_train_metric>
test_df.has_cactus = preds.numpy() [:, 0]
knn = KNeighborsClassifier(n_neighbors=13)
 scoring = 'accuracy'
score = cross_val_score(knn, X, y, cv=k_fold, n_jobs=1, scoring=scoring)
 print(score )<compute_test_metric>
test_df.to_csv('submission.csv', index=False )
round(np.mean(score)*100, 2 )<compute_train_metric>
pseudo_df = test_df.copy()
rf = RandomForestClassifier(n_estimators=13)
 scoring = 'accuracy'
score = cross_val_score(rf, X, y, cv=k_fold, n_jobs=1, scoring=scoring)
 print(score )<compute_test_metric>
pseudo_df.loc[pseudo_df['has_cactus'] > 0.99, 'has_cactus'] = 1
pseudo_df.loc[pseudo_df['has_cactus'] < 0.01, 'has_cactus'] = 0
round(np.mean(score)*100, 2 )<compute_train_metric>
pseudo_label_df = pseudo_df[pseudo_df['has_cactus'] > 0.99]
pseudo_label_df.append(pseudo_df[pseudo_df['has_cactus'] < 0.01])
 pseudo_label_df.shape
nb = GaussianNB()
scoring = 'accuracy'
score = cross_val_score(nb, X, y, cv=k_fold, n_jobs=1, scoring=scoring)
 print(score )<compute_test_metric>
pseudo_label_df['has_cactus'] = pseudo_label_df['has_cactus'].astype(np.int64 )
round(np.mean(score)*100, 2 )<compute_train_metric>
label_src = ImageList.from_df(pseudo_label_df, path = data_folder/'test', folder='test', cols='id' )
svm = SVC()
scoring = 'accuracy'
score = cross_val_score(svm, X, y, cv=k_fold, n_jobs=1, scoring=scoring)
 print(score )<compute_test_metric>
learn = cnn_learner(train_img, models.densenet161, metrics=[error_rate, accuracy], callback_fns=callbacks ).mixup().to_fp16()
round(np.mean(score)*100, 2 )<predict_on_test>
lr = 1e-02
learn.fit_one_cycle(3, slice(lr), callbacks=[SaveModelCallback(learn, every='improvement', monitor='quadratic_kappa', name='bestmodel')] )
clf = SVC()
clf.fit(X, y)
 test_data = test.drop("PassengerId", axis=1 ).copy()
prediction = clf.predict(test_data )<save_to_csv>
learn.unfreeze()
learn.fit_one_cycle(16, slice(1e-05, 1e-03), callbacks=[SaveModelCallback(learn, every='improvement', monitor='quadratic_kappa', name='bestmodel')] )
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": prediction
})
 submission.to_csv('submission.csv', index=False )<set_options>
learn.to_fp32()
warnings.filterwarnings('ignore')
 print(os.listdir(".. /input"))
pd.set_option('display.float_format', lambda x: '{:.3f}'.format(x))
%matplotlib inline<load_from_csv>
preds,_ = learn.TTA(ds_type=DatasetType.Test)
 test_df.has_cactus = preds.numpy() [:, 0]
test_df.to_csv('submission.csv', index=False )