Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<train_model>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
class DataFrameSelector(BaseEstimator, TransformerMixin):
def __init__(self, attribute_names):
self.attribute_names = attribute_names
def fit(self, X, y=None):
return self
def transform(self, X):
return X[self.attribute_names]<create_dataframe>
from fastai.vision import *
from fastai import *
from fastai.metrics import error_rate
import pandas as pd
import torch
housing = train_data.copy()<drop_column>
path =".. /input/"
train_df=pd.read_csv(path+"train.csv")
 test_df=pd.read_csv(path+"sample_submission.csv")
housing.drop(['PoolQC','MiscFeature','Alley','Fence','FireplaceQu'],axis=1, inplace=True )<create_dataframe>
bs = 128
data = ImageDataBunch.from_csv(path=path, folder='train/train', csv_labels='train.csv', ds_tfms=get_transforms() , size=32, bs=bs ).normalize(imagenet_stats)
cat_pipeline = Pipeline([ 
("selector", DataFrameSelector(attribute_names=cat_attributes))
] )<train_model>
data.show_batch(rows=3, figsize=(7,6))
class CategoricalImputer(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
self.most_frequent_ = pd.Series([X[attr].value_counts().index[0] for attr in X], index=X.columns)
 return self
def transform(self, X, y=None):
return X.fillna(self.most_frequent_ )<create_dataframe>
data.classes, data.c, len(data.train_ds), len(data.valid_ds )
cat_pipeline = Pipeline([ 
("selector", DataFrameSelector(attribute_names=cat_attributes)) , 
('imputer', CategoricalImputer())
 ] )<feature_engineering>
learn = cnn_learner(data, models.resnet50, metrics=error_rate, model_dir="/tmp/model/" )
cat_pipeline.fit_transform(housing ).loc[some_incomplete_rows.index]<categorify>
learn.fit_one_cycle(6 )
cat_pipeline = Pipeline([ 
("selector", DataFrameSelector(attribute_names=cat_attributes)) , 
('imputer', CategoricalImputer()), 
("encoder", OneHotEncoder(sparse=False))
] )<categorify>
learn.fit_one_cycle(5, max_lr=slice(3e-5,3e-4))
cat_pipeline.fit_transform(housing )<create_dataframe>
interp = ClassificationInterpretation.from_learner(learn )
housing = train_data.copy()<drop_column>
a,b,c=learn.predict(open_image(".. /input/test/test/000940378805c44108d287872b2f04ce.jpg"))
print(c)
 print(c[1].numpy())
housing.drop(['Id','SalePrice', 'LotFrontage'],axis=1, inplace=True )<feature_engineering>
def pred(name):
a,b,c=learn.predict(open_image(".. /input/test/test/"+name))
return c[1].numpy()
transformer = ReduceVIF(thresh=15)
 num_attributes = list(transformer.fit_transform(housing.select_dtypes(include=['int64','float64'])).columns.values )<choose_model_class>
test_df["has_cactus"]=test_df["id"].apply(lambda x:pred(x))
<create_dataframe><EOS> 
test_df.to_csv('submission.csv',index=False )