Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<count_missing_values>
base_dir = Path(".. /input")
 save_dir = Path('/kaggle/working')
 model_dir= Path('/tmp/models')
 base = base_dir
base.ls()
train.isnull().sum()<count_missing_values>
train_folder, train_path, sample_sub_path, test_path= base.ls()
train.isnull().sum()<count_missing_values>
train_folder.ls()
test.isnull().sum()<count_missing_values>
train_df = pd.read_csv(train_path); train_df.head()
test.isnull().sum()<drop_column>
np.random.seed(42)
 data_il = ImageList.from_df(train_df,train_folder)
 data_il = data_il.split_by_rand_pct()
data_il = data_il.label_from_df()
data_il = data_il.transform(get_transforms() ,size=32 )
del test['SalePrice']<prepare_x_and_y>
data = data_il.databunch().normalize(imagenet_stats )
X=train.drop(['SalePrice','SalePrice_Log1p','Classes'],axis=1)
 y=train.SalePrice_Log1p<train_model>
data.show_batch()
def overfit_reducer(df):
overfit = []
for i in df.columns:
counts = df[i].value_counts()
zeros = counts.iloc[0]
if zeros / len(df)* 100 > 99.9:
overfit.append(i)
 overfit = list(overfit)
 return overfit
overfitted_features = overfit_reducer(X )<normalization>
learn = cnn_learner(data, models.resnet50, model_dir='/temp/model',metrics=[accuracy] ).to_fp16()
std_scaler=StandardScaler()
rbst_scaler=RobustScaler()
power_transformer=PowerTransformer()
X_std=std_scaler.fit_transform(X)
 X_rbst=rbst_scaler.fit_transform(X)
 X_pwr=power_transformer.fit_transform(X)
 test_std=std_scaler.transform(test)
 test_rbst=rbst_scaler.transform(test)
 test_pwr=power_transformer.transform(test )<train_model>
learn.freeze()
learn.lr_find()
X_train,X_test,y_train,y_test=train_test_split(X_std,y,test_size=0.002,random_state=52)
 print('X_train Shape :',X_train.shape)
 print('X_test Shape :',X_test.shape)
 print('y_train Shape :',y_train.shape)
 print('y_test Shape :',y_test.shape )<train_model>
learn.fit_one_cycle(3, 1e-2 )
lgb_regressor=lgb.LGBMRegressor(objective='regression', num_leaves=5, learning_rate=0.035, n_estimators=2177, max_bin=50, bagging_fraction=0.65,bagging_freq=5, bagging_seed=7,
feature_fraction=0.201, feature_fraction_seed=7,n_jobs=-1)
 lgb_regressor.fit(X_train, y_train)
 y_head=lgb_regressor.predict(X_test)
 print('-'*10+'LGBM'+'-'*10)
 print('R square Accuracy: ',r2_score(y_test,y_head))
print('Mean Absolute Error Accuracy: ',mean_absolute_error(y_test,y_head))
print('Mean Squared Error Accuracy: ',mean_squared_error(y_test,y_head))<train_model>
learn.save(save_dir/'stage1-64-resnet50' )
gb_reg = GradientBoostingRegressor(n_estimators=1992, learning_rate=0.03005, max_depth=4, max_features='sqrt', min_samples_leaf=15, min_samples_split=14, loss='huber', random_state =42)
 gb_reg.fit(X_train, y_train)
 y_head=gb_reg.predict(X_test)
 print('-'*10+'GBR'+'-'*10)
 print('R square Accuracy: ',r2_score(y_test,y_head))
print('Mean Absolute Error Accuracy: ',mean_absolute_error(y_test,y_head))
print('Mean Squared Error Accuracy: ',mean_squared_error(y_test,y_head))<compute_train_metric>
learn.load(save_dir/'stage1-64-resnet50' )
kfolds = KFold(n_splits=10, shuffle=True, random_state=42)
 alphas=[1e-9,1e-8,1e-7,1e-6]
ridgecv_reg= make_pipeline(RidgeCV(alphas=alphas, cv=kfolds))
ridgecv_reg.fit(X_train, y_train)
 y_head=ridgecv_reg.predict(X_test)
 print('-'*10+'RidgeCV'+'-'*10)
 print('R square Accuracy: ',r2_score(y_test,y_head))
print('Mean Absolute Error Accuracy: ',mean_absolute_error(y_test,y_head))
print('Mean Squared Error Accuracy: ',mean_squared_error(y_test,y_head))<compute_train_metric>
learn.lr_find()
kfolds = KFold(n_splits=8, shuffle=True, random_state=42)
 lassocv_reg= make_pipeline(LassoCV(alphas=alphas, cv=kfolds))
lassocv_reg.fit(X_train, y_train)
 y_head=lassocv_reg.predict(X_test)
 print('-'*10+'LassoCV'+'-'*10)
 print('R square Accuracy: ',r2_score(y_test,y_head))
print('Mean Absolute Error Accuracy: ',mean_absolute_error(y_test,y_head))
print('Mean Squared Error Accuracy: ',mean_squared_error(y_test,y_head))<compute_train_metric>
learn.fit_one_cycle(2, slice(1e-8,1e-7/5))
kfolds = KFold(n_splits=8, shuffle=True, random_state=42)
 alphas=[0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006]
l1ratio=[0.87, 0.9,0.92, 0.95,0.97, 0.99, 1]
elasticv_reg= make_pipeline(ElasticNetCV(alphas=alphas, cv=kfolds, l1_ratio=l1ratio))
elasticv_reg.fit(X_train, y_train)
 y_head=elasticv_reg.predict(X_test)
 print('-'*10+'ElasticNetCV'+'-'*10)
 print('R square Accuracy: ',r2_score(y_test,y_head))
print('Mean Absolute Error Accuracy: ',mean_absolute_error(y_test,y_head))
print('Mean Squared Error Accuracy: ',mean_squared_error(y_test,y_head))<train_model>
learn.save(save_dir/'stage2-32-resnet34' )
estimators = [('lgbm', lgb_regressor), 
('gbr', gb_reg), 
('lasso', lassocv_reg), 
('ridge', ridgecv_reg), 
('elasticnet', elasticv_reg)]
stack_reg=StackingRegressor(estimators=estimators,final_estimator=ExtraTreesRegressor(n_estimators=50),n_jobs=-1)
 stack_reg.fit(X_train, y_train)
 y_head=stack_reg.predict(X_test)
 print('-'*10+'StackingRegressor'+'-'*10)
 print('R square Accuracy: ',r2_score(y_test,y_head))
print('Mean Absolute Error Accuracy: ',mean_absolute_error(y_test,y_head))
print('Mean Squared Error Accuracy: ',mean_squared_error(y_test,y_head))<concatenate>
learn.load(save_dir/'stage2-32-resnet34' )
y_head=pd.DataFrame(y_head,columns=['Predict'])
 y_test.reset_index(drop=True,inplace=True)
 y_test_y_head=pd.concat([y_test,y_head],axis=1)
 y_test_y_head.head()<count_values>
test_df = pd.read_csv(".. /input/sample_submission.csv")
print('Count columns of test: ',len(test.columns))
print('Count columns of X: ',len(X.columns))<predict_on_test>
test_img = ImageList.from_df(test_df, path=test_path, folder='' )
test_pred_lgb=lgb_regressor.predict(test_pwr)
 test_pred_gb=gb_reg.predict(test_pwr)
 test_pred_elastic=elasticv_reg.predict(test_pwr)
 test_pred_ridge=ridgecv_reg.predict(test_pwr)
 test_pred_lasso=lassocv_reg.predict(test_pwr)
 test_pred_stack=stack_reg.predict(test_pwr )<create_dataframe>
learn.data.add_test(test_img )
test_pred_lgb=pd.DataFrame(test_pred_lgb,columns=['SalePrice'])
 test_pred_gb=pd.DataFrame(test_pred_gb,columns=['SalePrice'])
 test_pred_elastic=pd.DataFrame(test_pred_elastic,columns=['SalePrice'])
 test_pred_ridge=pd.DataFrame(test_pred_ridge,columns=['SalePrice'])
 test_pred_lasso=pd.DataFrame(test_pred_lasso,columns=['SalePrice'])
 test_pred_stack=pd.DataFrame(test_pred_stack,columns=['SalePrice'] )<feature_engineering>
preds,y = learn.get_preds(ds_type=DatasetType.Test )
test_pred_lgb.SalePrice =np.floor(np.expm1(test_pred_lgb.SalePrice))
test_pred_gb.SalePrice =np.floor(np.expm1(test_pred_gb.SalePrice))
test_pred_elastic.SalePrice =np.floor(np.expm1(test_pred_elastic.SalePrice))
test_pred_ridge.SalePrice =np.floor(np.expm1(test_pred_ridge.SalePrice))
test_pred_lasso.SalePrice =np.floor(np.expm1(test_pred_lasso.SalePrice))
test_pred_stack.SalePrice =np.floor(np.expm1(test_pred_stack.SalePrice))<prepare_output>
test_df.has_cactus = preds.numpy() [:, 0]
final_pred=(test_pred_stack*0.1665)+(test_pred_lgb*0.678)+(test_pred_ridge*0.1665)
 final_pred.head()<prepare_output>
test_df.to_csv('submission.csv', index=False )