Kaggle competition: Aerial Cactus Identification
dbscan = DBSCAN(eps=1400, min_samples=20 ).fit(train_pca)
 core_samples_mask = np.zeros_like(dbscan.labels_, dtype=bool)
 core_samples_mask[dbscan.core_sample_indices_] = True
labels=dbscan.labels_
n_clusters_ = len(set(labels)) -(1 if -1 in labels else 0 )<create_dataframe>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
labels=pd.DataFrame(labels,columns=['Classes'])
 print(labels[labels['Classes']==-1] )<concatenate>
from fastai.imports import *
import fastai
train=pd.concat([train,labels],axis=1 )<filter>
print(fastai.__version__ )
train[train.Classes==-1]<drop_column>
from fastai.utils import *
from fastai.vision import *
from fastai.callbacks import *
from pathlib import Path
import PIL
from torch.utils import model_zoo
train.drop([197,810,1170,1182,1298,1386,1423],axis=0,inplace=True )<drop_column>
PATH = ".. /input/aerial-cactus-identification/train/"
TMP_PATH = "/tmp/tmp"
MODEL_PATH = "/tmp/model/"
sz=224
train_df = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 test_df = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv' )
"Looking at the SalePrice and SalePrice_Log1p visualizations"
train = train[train.GarageArea * train.GarageCars < 3700]
train = train[(train.FullBath +(train.HalfBath*0.5)+ train.BsmtFullBath +(train.BsmtHalfBath*0.5)) <5]<count_missing_values>
torch.cuda.is_available()
train.isnull().sum()<count_missing_values>
torch.backends.cudnn.enabled
train.isnull().sum()<count_missing_values>
target_count = train_df.has_cactus.value_counts()
print('Class 0:', target_count[0])
 print('Class 1:', target_count[1])
 print('Proportion:', round(target_count[0] / target_count[1], 2), ': 1')
test.isnull().sum()<count_missing_values>
count_class_0, count_class_1 = train_df.has_cactus.value_counts()
df_class_0 = train_df[train_df['has_cactus'] == 0]
df_class_1 = train_df[train_df['has_cactus'] == 1]
test.isnull().sum()<drop_column>
X = train_df[:1]
y = train_df['has_cactus']
del test['SalePrice']<prepare_x_and_y>
fnames = np.array([f'train/{f}' for f in sorted(os.listdir(f'{PATH}train')) ])
 labels = np.array([(0 if 'cat' in fname else 1)for fname in fnames])
 fnames[0]
X=train.drop(['SalePrice','SalePrice_Log1p','Classes'],axis=1)
 y=train.SalePrice_Log1p<train_model>
arch = ".. /input/resnet34/"
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>
tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=20, max_lighting=0.3, max_warp=0.2, max_zoom=1.2 )
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>
test_images = ImageList.from_df(test_df, path='.. /input/aerial-cactus-identification/test', folder='test')
 src =(ImageList.from_df(train_df, path='.. /input/aerial-cactus-identification/train', folder='train')
.split_by_rand_pct(0.2)
.label_from_df() 
.add_test(test_images))
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>
data =(src.transform(tfms,
size=32,
resize_method=ResizeMethod.PAD,
padding_mode='reflection')
.databunch(bs=sz)
.normalize(imagenet_stats))
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>
data.classes, data.c
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>
data.show_batch(rows=4, figsize=(9,9))
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>
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
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>
client = bigquery.Client()
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>
lr = 1e-2
learn.fit_one_cycle(10, lr )
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.save('Model-1' )
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>
learn = cnn_learner(data,
models.resnet34,
metrics=[accuracy, AUROC() ],
callback_fns=[partial(SaveModelCallback)],
path = '.')
 learn = learn.load('Model-1' )
print('Count columns of test: ',len(test.columns))
print('Count columns of X: ',len(X.columns))<predict_on_test>
lr = 1e-6
learn.fit_one_cycle(10, lr )
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>
probability, classification = learn.get_preds(ds_type=DatasetType.Test)
 test_df.has_cactus = probability.numpy() [:, 0]
test_df.head()
<feature_engineering><EOS> 
test_df.to_csv("submission.csv", index=False )