Kaggle competition: Aerial Cactus Identification
test_scaled = test[num_features_to_scale].copy()
test_scaled = scaler.transform(test_scaled)
 test_scaled = pd.DataFrame(test_scaled,columns=num_features_to_scale)
 test_scaled = pd.concat([test_scaled,test[cat_features],test[num_features]],axis=1)
 print(test_scaled.info() )<categorify>
from fastai import *
from fastai.vision import *
import torch
my_imputer = SimpleImputer(strategy='most_frequent')
 my_imputer.fit(pd.concat([train_scaled,test_scaled]))
data_with_imputed_values = pd.DataFrame(
 my_imputer.transform(train_scaled), columns=train_scaled.columns
).astype(train_scaled.dtypes.to_dict())
 test_data_with_imputed_values = pd.DataFrame(
 my_imputer.transform(test_scaled), columns=test_scaled.columns
).astype(test_scaled.dtypes.to_dict())
 print(data_with_imputed_values.info())
 print(test_data_with_imputed_values.info() )<feature_engineering>
data_folder = Path(".. /input" )
all_data = pd.concat([data_with_imputed_values,test_data_with_imputed_values])
 for column in all_data.select_dtypes(include=[np.object] ).columns:
data_with_imputed_values[column] = pd.Categorical(data_with_imputed_values[column], categories = all_data[column].unique())
 test_data_with_imputed_values[column] = pd.Categorical(test_data_with_imputed_values[column], categories = all_data[column].unique())
<categorify>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/sample_submission.csv" )
data_with_imputed_values = pd.get_dummies(data_with_imputed_values,drop_first=True)
 test_data_with_imputed_values = pd.get_dummies(test_data_with_imputed_values,drop_first=True )<define_search_space>
train_img.show_batch(rows=3, figsize=(7,6))
param_grid = {
'n_estimators' : [500,1200],
'max_depth': range(1,5,2),
'max_features' :('log2', 'sqrt'),
'class_weight':[{1: w} for w in [1,1.5]]
}
GridRF = GridSearchCV(RandomForestClassifier(random_state=15), param_grid)
 GridRF.fit(data_with_imputed_values, y_train)
 print(
 "
Best parameters
" + str(GridRF.best_params_))
rf = RandomForestClassifier(random_state=15,**GridRF.best_params_)
 rf.fit(data_with_imputed_values, y_train)
 Rfclf_fea = pd.DataFrame(rf.feature_importances_)
 Rfclf_fea["Feature"] = list(data_with_imputed_values.columns)
 Rfclf_fea.sort_values(by=0, ascending=False ).head(10 )<compute_test_metric>
print(train_img.classes)
 print("No.of classes : {}".format(train_img.c))
cross_val_score(rf, data_with_imputed_values, y_train,cv=5 )<categorify>
learner = cnn_learner(train_img, models.densenet161, metrics=[accuracy, error_rate] )
one = OneHotEncoder(handle_unknown='ignore')
 simple = SimpleImputer(strategy='constant', fill_value='missing')
 pre = one.fit_transform(simple.fit_transform(train[['Embarked', 'Sex', 'Pclass']]))<drop_column>
learner.fit_one_cycle(5, max_lr=slice(3e-02))
def drop_cols_with_str(df,str_v):
return df[df.columns.drop(list(df.filter(regex=str_v)))]
data_with_imputed_values = drop_cols_with_str(data_with_imputed_values,'Cabin_')
 test_data_with_imputed_values = drop_cols_with_str(test_data_with_imputed_values,'Cabin_')
<split>
learner.save('stage-1' )
X_t, X_v, y_t, y_v = train_test_split(data_with_imputed_values,y_train, stratify=y_train, test_size=0.2, random_state=2019)
 def runXGB(X_t, X_v, y_t, y_v, feature_names=None, seed_val=2017, num_rounds=200):
param = {}
param['objective'] = 'binary:logistic'
param['eta'] = 0.1
param['max_depth'] = 6
param['silent'] = 1
param['eval_metric'] = 'auc'
param['min_child_weight'] = 1
param['subsample'] = 0.5
param['seed'] = seed_val
num_rounds = num_rounds
plst = list(param.items())
 xgtrain = xgb.DMatrix(X_t, label=y_t)
 xgtest = xgb.DMatrix(X_v, label=y_v)
 watchlist = [(xgtrain,'train'),(xgtest, 'test')]
model = xgb.train(plst, xgtrain, num_rounds, watchlist, early_stopping_rounds=20)
 return model
model = runXGB(X_t, X_v, y_t, y_v)
<predict_on_test>
interpreter = ClassificationInterpretation.from_learner(learner )
preds = model.predict(xgb.DMatrix(data_with_imputed_values))<predict_on_test>
preds,_ = learner.get_preds(ds_type=DatasetType.Test)
 test_df.has_cactus = preds.numpy() [:, 0]
test_df.to_csv("submission.csv", index=False )