Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<normalization>
import numpy as np
import pandas as pd
import os
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
 X_test =sc.transform(X_test )<train_on_grid>
from fastai.vision import *
rfc=RandomForestClassifier()
parameters= {'n_estimators':[ 100,200,300,400, 600],
'max_depth':[3,4,6,7],
'criterion':['entropy','gini']
}
rfc=GridSearchCV(rfc, param_grid=parameters, cv = 5)
 rfc.fit(X_train,y_train)
 print("The best value of leanring rate is: ",rfc.best_params_, )<train_model>
data_path = Path('/kaggle/input/aerial-cactus-identification')
 data_path.ls()
rf_model = RandomForestClassifier(criterion= 'gini', n_estimators = 100 ,max_depth = 6, random_state = 0)
 rf_model.fit(X_train, y_train)
 y_pred = rf_model.predict(X_test)
 Random_forest_acc= accuracy_score(y_test, y_pred)
 print('acc = ', Random_forest_acc)
<choose_model_class>
np.random.seed(4)
 data = ImageList.from_csv(data_path, 'train.csv', folder='train/train', cols='id')\ 
.split_by_rand_pct(0.1)\ 
.label_from_df(cols='has_cactus')\ 
.transform(get_transforms() , size=224)\ 
.databunch() \ 
.normalize(imagenet_stats )
model = LogisticRegression()
model.fit(X_train, y_train)
 y_pred = model.predict(X_test)
 LogisticReg_acc= accuracy_score(y_test, y_pred)
 print('acc = ', LogisticReg_acc )<train_model>
learner = cnn_learner(data, models.resnet34, metrics=error_rate)
 learner.model_dir = '/kaggle/working/'
model = SVC(kernel = 'rbf', random_state = 0)
 model.fit(X, y)
 y_pred = model.predict(X_test)
 SVC_acc = accuracy_score(y_test, y_pred)
 print('acc = ', SVC_acc )<compute_train_metric>
learner.freeze()
model = GaussianNB()
model.fit(X, y)
 y_pred = model.predict(X_test)
 Gaussian_acc = accuracy_score(y_test, y_pred)
 print('acc = ', Gaussian_acc )<train_model>
learner.fit_one_cycle(5 )
model = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
 model.fit(X_train, y_train)
 y_pred = model.predict(X_test)
 DT_acc = accuracy_score(y_test, y_pred)
 print('acc = ', DT_acc)
<train_on_grid>
learner.unfreeze()
learner.fit_one_cycle(3 )
gbc=GradientBoostingClassifier()
parameters= {'n_estimators':[ 50,100,200,300, ],
'max_depth':[3,4,6,7]
}
gbreg=GridSearchCV(gbc, param_grid=parameters, cv = 5)
 gbreg.fit(X_train,y_train)
 print("The best value of leanring rate is: ",gbreg.best_params_, )<train_model>
learner.export('/kaggle/working/export.pkl' )
model_gb = GradientBoostingClassifier(n_estimators = 100, max_depth =4, random_state = 42)
 model_gb.fit(X_train, y_train)
 y_pred = model_gb.predict(X_test)
 GB_acc = accuracy_score(y_test, y_pred)
 print('acc = ', GB_acc )<compute_train_metric>
test_imgs = ImageList.from_folder(data_path/'test' )
classifier = XGBClassifier()
classifier.fit(X_train, y_train)
 y_pred = classifier.predict(X_test)
 xgb_acc = accuracy_score(y_pred, y_test)
 print('acc=',xgb_acc )<train_model>
learn = load_learner('/kaggle/working/', test=test_imgs)
 preds, _ = learn.get_preds(ds_type=DatasetType.Test)
 preds[:5]
print('RF_acc=', Random_forest_acc)
 print('Logistic_acc=', LogisticReg_acc)
 print('SVC_acc=', SVC_acc)
 print('Gaussian_acc=', Gaussian_acc)
 print('DecisionTree_acc=', DT_acc)
 print('GradBoost_acc=', GB_acc)
 print('XGBoost_acc=', xgb_acc)
<train_model>
img_names = [os.path.basename(os.fspath(fname)) for fname in test_imgs.items]
img_names[:5]
<prepare_output><EOS> 
df = pd.DataFrame({'id': img_names, 'has_cactus': preds.numpy() [:,0]})
 df.to_csv('submission.csv', index=None)
 df.head()