Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<count_missing_values>
%matplotlib inline
test.isnull().sum()<drop_column>
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything()
train_set = dataframe.drop(['Name','Cabin', 'Ticket','PassengerId', ], axis = 1)
 test_set = test.drop(['Name','Cabin', 'Ticket', 'PassengerId', ], axis = 1)
<count_missing_values>
Path('models' ).mkdir(exist_ok=True)
 !cp '.. /input/resnet34/resnet34.pth' 'models/'
def load_url(*args, **kwargs):
model_dir = Path('models')
 filename = 'resnet34.pth'
if not(model_dir/filename ).is_file() : raise FileNotFoundError
return torch.load(model_dir/filename)
 model_zoo.load_url = load_url
test_set.isnull().sum()<data_type_conversions>
data_path = Path(".. /input/aerial-cactus-identification")
 train_df = pd.read_csv(data_path/'train.csv')
 test_df = pd.read_csv(data_path/'sample_submission.csv' )
train_set['Embarked'].fillna(train_set['Embarked'].mode() [0], inplace = True)
<prepare_x_and_y>
df1 = train_df[train_df.has_cactus==0].copy()
df2 = df1.copy()
train_df = train_df.append([df1, df2], ignore_index=True )
y = train_set.iloc[:, 0].values
X = train_set.iloc[:, train_set.columns != 'Survived'].values
print(X[0] )<categorify>
tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=20, max_lighting=0.3, max_warp=0.2, max_zoom=1.2 )
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder() , [6])], remainder='passthrough')
 X = np.array(ct.fit_transform(X))
print(X[2] )<categorify>
test_images = ImageList.from_df(test_df, path=data_path/'test', folder='test')
 src =(ImageList.from_df(train_df, path=data_path/'train', folder='train')
.split_by_rand_pct(0.2)
.label_from_df() 
.add_test(test_images))
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder() , [6])], remainder='passthrough')
 test_set = np.array(ct.fit_transform(test_set))
print(test_set[1])
<categorify>
data =(src.transform(tfms,
size=128,
resize_method=ResizeMethod.PAD,
padding_mode='reflection')
.databunch(bs=256)
.normalize(imagenet_stats))
le = LabelEncoder()
X[:, 4 ] = le.fit_transform(X[:,4])
 print(X[1])
<categorify>
data.classes, data.c
le = LabelEncoder()
test_set[:, 4] = le.fit_transform(test_set[:,4])
 print(test_set[2] )<split>
data.show_batch(rows=3, figsize=(9,9))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20 )<normalization>
learn = cnn_learner(data,
models.resnet34,
metrics=[accuracy, AUROC() ],
path = '.' )
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
 X_test =sc.transform(X_test )<train_on_grid>
lr = 1e-3
learn.fit_one_cycle(5, lr )
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>
learn.save('step-1' )
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>
learn = cnn_learner(data,
models.resnet34,
metrics=[accuracy, AUROC() ],
callback_fns=[partial(SaveModelCallback)],
wd=0.1,
ps=[0.9, 0.6, 0.4],
path = '.')
 learn = learn.load('step-1' )
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>
learn.freeze_to(1)
 learn.lr_find()
learn.recorder.plot()
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>
learn.fit_one_cycle(20, slice(4e-4, lr/5))
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>
unfrozen_validation = learn.validate()
print("Final model validation loss: {0}".format(unfrozen_validation[0]))
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>
probability, classification = learn.get_preds(ds_type=DatasetType.Test)
 test_df.has_cactus = probability.numpy() [:, 0]
test_df.head()
<train_model><EOS> 
test_df.to_csv("submission.csv", index=False )