Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<feature_engineering>
import os
import numpy
from fastai.vision import *
import pandas as pd
vec = DictVectorizer(sparse=False, dtype=int)
 res = vec.fit_transform(train[['modCabin', 'Pclass', 'Age', 'SibSp', 'Parch','Embarked', 'Sex']].fillna('NA' ).to_dict(orient='records'))
res<prepare_output>
train_dir = path/'train'/'train'
test_dir = path/'test'/'test'
train_labels = path/'train.csv'
output = path/'sample_submission.csv'
df = pd.DataFrame(res, columns=vec.get_feature_names())
 df.head()<count_duplicates>
np.random.seed(26 )
corr_df = df.corr().abs().unstack().sort_values(ascending=False)
 corr_df[corr_df<1].drop_duplicates() [:10]<split>
data =(ImageList.from_csv(path=path, csv_name='train.csv', folder='train/train')
.split_by_rand_pct() 
.label_from_df(cols='has_cactus')
.add_test(ImageList.from_csv(path=path, csv_name='sample_submission.csv',
folder='test/test')) 
.transform(get_transforms() , size=224)
.databunch(bs=32, num_workers=0)
.normalize(imagenet_stats))
gbes = ensemble.GradientBoostingClassifier(n_estimators=400,
validation_fraction=0.2,
n_iter_no_change=5, tol=0.01,
random_state=0)
 X_train, X_test, y_train, y_test = train_test_split(train.Age, train.Survived, test_size=0.2,
random_state=0)
 gbes.fit(X_train.fillna(0 ).values.reshape(-1, 1),y_train)
 gbes.score(X_test.fillna(0 ).values.reshape(-1, 1),y_test )<import_modules>
data.classes, data.classes, data.train_ds, data.test_ds
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer<train_model>
from fastai.vision.learner import create_cnn, models
X_train, X_test, y_train, y_test = train_test_split(df, train.Survived, test_size=0.2,
random_state=0)
 gbes.fit(X_train,y_train)
 gbes.score(X_test,y_test )<compute_test_metric>
arch = cnn_learner(data, base_arch=models.resnet34, model_dir='/tmp/models',
metrics=accuracy )
print(cross_val_score(gbes, df, train.Survived, cv=3))<categorify>
arch.lr_find()
test_transf = vec.transform(test[['modCabin', 'Pclass', 'Age', 'SibSp', 'Parch','Embarked', 'Sex']].fillna('NA' ).to_dict(orient='records'))
df_test = pd.DataFrame(test_transf, columns=vec.get_feature_names())
 df_test.head()<save_to_csv>
epoch = 10
lr = 1e-3
arch.fit_one_cycle(epoch, slice(lr))
gbespred = gbes.predict(df_test)
 out_df = pd.DataFrame({"PassengerId":test["PassengerId"].values})
 out_df['Survived'] = gbespred
out_df.to_csv("submission.csv", index=False)
 print('out_df.to_csv')
 print(out_df.head())
 out_df.Survived.value_counts()<categorify>
arch.save('model-v1' )
preprocessing.KBinsDiscretizer(n_bins=[2,2,2,2], encode='ordinal' ).fit_transform(train.loc[:,['Age','Fare','SibSp','Parch']].dropna() )<count_values>
arch.load('model-v1' )
train.Embarked.value_counts()<feature_engineering>
arch.fit_one_cycle(3, max_lr=slice(lr/1000,lr/100))
train.loc[train.Embarked.isnull() ,'Embarked']='Q'<data_type_conversions>
prediction, y = arch.get_preds(ds_type=DatasetType.Test )
train['IsCabinNull'] = train.Cabin.isnull().astype(int)
 test['IsCabinNull'] = test.Cabin.isnull().astype(int )<feature_engineering>
test_data = pd.read_csv(output)
 test_data.has_cactus = prediction.numpy() [:, 0]
test_data.head(10 )
train.loc[train.Age.isnull() , 'Age'] = 28
test.loc[test.Age.isnull() , 'Age'] = 28<feature_engineering>
test_data.to_csv('submission.csv', index=False )