Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<count_values>
import fastai
from fastai.vision import *
from sklearn.model_selection import KFold
dataset['Title'].value_counts()<categorify>
fastai.__version__
def replace_titles(title):
if title in ['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona']:
return 'Rare'
elif title in ['Countess', 'Mme']:
return 'Mrs'
elif title in ['Mlle', 'Ms']:
return 'Miss'
elif title =='Dr':
if x['Sex']=='Male':
return 'Mr'
else:
return 'Mrs'
else:
return title
dataset['Title'] = dataset['Title'].apply(replace_titles )<categorify>
data_path = Path('.. /input/aerial-cactus-identification')
 df = pd.read_csv(data_path/'train.csv')
 df.head()
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
dataset['Title'] = dataset['Title'].map(title_mapping )<feature_engineering>
sub_csv = pd.read_csv(data_path/'sample_submission.csv')
 sub_csv.head()
dataset['Noble'] = dataset['Name'].apply(lambda x: 1 if re.search(r'\ (.*?\)', x)else 0 )<groupby>
def create_databunch(valid_idx):
test = ImageList.from_df(sub_csv, path=data_path/'test', folder='test')
 data =(ImageList.from_df(df, path=data_path/'train', folder='train')
.split_by_idx(valid_idx)
.label_from_df() 
.add_test(test)
.transform(get_transforms(flip_vert=True, max_rotate=20.0), size=128)
.databunch(path='.', bs=64)
.normalize(imagenet_stats)
)
 return data
dataset[:train_len][['Noble', 'Survived']].groupby(['Noble'] ).sum()<feature_engineering>
kf = KFold(n_splits=5, random_state=379)
 epochs = 6
lr = 1e-2
preds = []
for train_idx, valid_idx in kf.split(df):
data = create_databunch(valid_idx)
 learn = create_cnn(data, models.densenet201, metrics=[accuracy])
 learn.fit_one_cycle(epochs, slice(lr))
learn.unfreeze()
learn.fit_one_cycle(epochs, slice(lr/400, lr/4))
learn.fit_one_cycle(epochs, slice(lr/800, lr/8))
preds.append(learn.get_preds(ds_type=DatasetType.Test))
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1<feature_engineering>
ens = torch.cat([preds[i][0][:,1].view(-1, 1)for i in range(5)], dim=1)
 ens =(ens.mean(1)>0.5 ).long() ; ens[:10]
dataset.loc[ dataset['FamilySize'] == 1, 'FamilySizeBin'] = 0
dataset.loc[(dataset['FamilySize'] >= 2)&(dataset['FamilySize'] <= 3), 'FamilySizeBin'] = 1
dataset.loc[(dataset['FamilySize'] == 4), 'FamilySizeBin'] = 2
dataset.loc[(dataset['FamilySize'] >= 5)&(dataset['FamilySize'] <= 7), 'FamilySizeBin'] = 3
dataset.loc[(dataset['FamilySize'] >=8), 'FamilySizeBin'] = 4<data_type_conversions>
sub_csv['has_cactus'] = ens
dataset['FamilySizeBin'] = dataset['FamilySizeBin'].astype('int' )<feature_engineering>
sub_csv.to_csv('submission.csv', index=False )