Kaggle competition: Planet: Understanding the Amazon from Space
<SOS> metric: MeanFScoreBeta Kaggle data source: planet:-understanding-the-amazon-from-space<concatenate>
import numpy as np
import pandas as pd
import os
train=house_data
test=house_data_test
train['train']=1
test['train']=0
combined=pd.concat([train,test] )<categorify>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
ohe_data_frame=pd.get_dummies(combined,
columns=['MSZoning', 'Street', 'LotShape', 'LandContour', 'Utilities',
'LotConfig', 'LandSlope', 'Neighborhood', 'Condition1', 'Condition2',
'BldgType', 'HouseStyle', 'RoofStyle', 'RoofMatl', 'Exterior1st',
'Exterior2nd', 'MasVnrType', 'ExterQual', 'ExterCond', 'Foundation',
'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2',
'Heating', 'HeatingQC', 'CentralAir', 'Electrical', 'KitchenQual',
'Functional', 'GarageType', 'GarageFinish', 'GarageQual', 'GarageCond',
'PavedDrive', 'SaleType', 'SaleCondition'],
)<drop_column>
from fastai.vision import *
train_df=ohe_data_frame[ohe_data_frame['train']==1]
test_df=ohe_data_frame[ohe_data_frame['train']==0]
train_df.drop(['train'],axis=1,inplace=True)
 test_df.drop(['train','SalePrice'],axis=1,inplace=True )<define_variables>
path = Path('/kaggle/input/planet-understanding-the-amazon-from-space')
 path.ls()
house_data=train_df
house_data_test=test_df<prepare_x_and_y>
df = pd.read_csv(path/'train_v2.csv')
 df.head()
X_train = house_data.drop('SalePrice', axis=1)
 Y_train = house_data['SalePrice']
X_test = house_data_test<define_search_space>
tfms = get_transforms(flip_vert=True, max_lighting=0.1, max_zoom=1.05, max_warp=0.)
<train_model>
np.random.seed(42)
 src =(ImageList.from_csv(path, 'train_v2.csv', folder='train-jpg', suffix='.jpg')
.split_by_rand_pct(0.2)
.label_from_df(label_delim=' '))
params = {'n_estimators': 3000, 'max_depth': 1, 'min_samples_leaf':15, 'min_samples_split':10,
'learning_rate': 0.05, 'loss': 'huber','max_features':'sqrt'}
gbr_model = GradientBoostingRegressor(**params)
 gbr_model.fit(X_train, Y_train )<compute_test_metric>
data =(src.transform(tfms, size=128)
.databunch().normalize(imagenet_stats))
gbr_model.score(X_train, Y_train )<predict_on_test>
data.show_batch(rows=3, figsize=(12,9))
y_grad_predict = gbr_model.predict(X_test)
 print(y_grad_predict )<save_to_csv>
arch = models.resnet50
my_submission = pd.DataFrame({'Id': test_parent.Id, 'SalePrice': y_grad_predict})
 print(my_submission)
 my_submission.to_csv('submission.csv', encoding='utf-8', index=False )<set_options>
acc_02 = partial(accuracy_thresh, thresh=0.2)
 f_score = partial(fbeta, thresh=0.2)
 learn = cnn_learner(data, arch, metrics=[acc_02, f_score],path='.. /working/' )
%matplotlib inline
rcParams['figure.figsize'] = 20,5
rcParams['xtick.labelsize'] = 9
rcParams['ytick.labelsize'] = 9
rcParams['axes.labelsize'] = 10 
<load_from_csv>
learn.lr_find()
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 df = pd.concat([train,test] )<count_values>
lr = 0.01
df.Survived.value_counts()<split>
learn.fit_one_cycle(5, slice(lr))
eda, df_test = train_test_split(train, test_size=0.25, random_state=42)
 eda.head()<data_type_conversions>
learn.save('stage-1-rn50' )
df_fill = df.copy()
df_fill['Fare'].fillna(df_fill['Fare'].median() , inplace = True)
 df_fill['Embarked'].fillna(df_fill['Embarked'].mode().iloc[0], inplace = True)
 df_fill.head()<drop_column>
learn.fit_one_cycle(5, slice(1e-5, lr/5))
df_index = df_fill.set_index('PassengerId')
 df_index.head()<categorify>
learn.save('stage-2-rn50' )
df_cut = df_split_name.copy()
df_cut['FareBin'] = pd.qcut(df_cut.Fare, 5)
 label = LabelEncoder()
df_cut['FareBin_Code'] = label.fit_transform(df_cut['FareBin'])
 df_cut.drop(['FareBin'], 1, inplace=True )<categorify>
data =(src.transform(tfms, size=256)
.databunch().normalize(imagenet_stats))
learn.data = data
data.train_ds[0][0].shape
df_cut['AgeBin'] = pd.qcut(df_cut.Age, 5)
 label = LabelEncoder()
df_cut['AgeBin_code'] = label.fit_transform(df_cut['AgeBin'])
 df_cut.drop(['Age','AgeBin'], 1, inplace=True)
 df_cut.head()<drop_column>
lr=1e-2/2
df_comb = df_cut.copy()
df_comb['Family_members_aboard'] = df_comb['SibSp'] + df_comb['Parch']
df_comb.drop(['SibSp','Parch'], axis=1, inplace=True)
 df_comb.head()<feature_engineering>
learn.fit_one_cycle(5, slice(lr))
df_extr_family = df_comb.copy()
df_extr_family.insert(2,'Surname',df_extr_family['Name'].str.extract('([A-Za-z]+)\,', expand=True)[0])
 DEFAULT_SURVIVAL_VALUE = 0.5
df_extr_family['Family_Survival'] = DEFAULT_SURVIVAL_VALUE
df_extr_family.reset_index(inplace=True)
 for surname, sur_group in df_extr_family[df_extr_family['Family_members_aboard'] > 0].groupby(['Surname','Fare']):
for ind, row in sur_group.iterrows() :
smax = sur_group.drop(ind ).Survived.max()
smin = sur_group.drop(ind ).Survived.min()
passID = row['PassengerId']
if smax == 1:
df_extr_family.loc[df_extr_family['PassengerId'] == passID, 'Family_Survival'] = 1
elif smin == 0:
df_extr_family.loc[df_extr_family['PassengerId'] == passID, 'Family_Survival'] = 0
df_extr_family[df_extr_family.Family_Survival != 0.5]<categorify>
learn.save('stage-1-256-rn50' )
df_enc = df_extr_family.copy()
label = LabelEncoder()
df_enc['Embarked_code'] = label.fit_transform(df_enc['Embarked'])
 label = LabelEncoder()
df_enc['Sex_code'] = label.fit_transform(df_enc['Sex'])
 df_enc.drop(['Sex', 'Embarked'], 1, inplace=True)
 df_enc.head()<drop_column>
learn.fit_one_cycle(5, slice(1e-5, lr/5))
def drop_cols(cols):
return df_enc.drop(cols, axis=1)
 attr_to_drop = ['Title', 'Surname', 'Name', 'Ticket', 'Cabin', 'Fare']
df_prepared = drop_cols(attr_to_drop)
 df_prepared.set_index('PassengerId',inplace=True)
 train_ready = df_prepared[:891]
submission = df_prepared[891:]
train_ready<prepare_x_and_y>
learn.save('stage-2-256-rn50',return_path=True )
X = train_ready.drop('Survived',1)
 y = train_ready['Survived']
X_submission = submission.drop('Survived',1 )<normalization>
test = ImageList.from_folder(path/'test-jpg-v2' ).add(ImageList.from_folder(path/'test-jpg-additional'))
len(test )
std_scaler = StandardScaler()
X = std_scaler.fit_transform(X)
 X_submission = std_scaler.transform(X_submission )<compute_train_metric>
learn = load_learner('.. /working/', test=test)
 preds, _ = learn.get_preds(ds_type=DatasetType.Test )
def compare_clf(classifiers):
rows = []
for clf in classifiers:
start = time.time()
score_arr = cross_val_score(clf,X,y,cv=5,scoring='roc_auc')
 end = time.time()
for i, score in enumerate(score_arr):
score_dict = {
'fold':i+1,
'Classifier':clf.__class__.__name__,
'Score':score,
'Time(sec)':end-start
}
rows.append(score_dict)
 return pd.DataFrame(rows)
 classifiers = [
SGDClassifier() ,
LogisticRegression() ,
LinearDiscriminantAnalysis() ,
GaussianNB() ,
DecisionTreeClassifier() ,
RandomForestClassifier() ,
SVC() ,
KNeighborsClassifier()
]<sort_values>
thresh = 0.2
labelled_preds = [' '.join([learn.data.classes[i] for i,p in enumerate(pred)if p > thresh])for pred in preds]
compare_clf(classifiers ).groupby('Classifier' ).agg({'mean','median','std'} ).drop('fold',1 ).sort_values(( 'Score','mean'),ascending=False )<train_on_grid>
fnames = [f.name[:-4] for f in learn.data.test_ds.items]
n_neighbors = [6,7,8,9,10,11,12,14,16,18,20,22]
algorithm = ['auto']
weights = ['uniform', 'distance']
leaf_size = list(range(1,50,5))
hyperparams = {'algorithm': algorithm, 'weights': weights, 'leaf_size': leaf_size,
'n_neighbors': n_neighbors}
gd=GridSearchCV(estimator = KNeighborsClassifier() , param_grid = hyperparams, verbose=True,
cv=5, scoring = "roc_auc")
 gd.fit(X, y)
 print(gd.best_score_)
 print(gd.best_estimator_ )<predict_on_test>
df = pd.DataFrame({'image_name':fnames, 'tags':labelled_preds}, columns=['image_name', 'tags'] )
<save_to_csv><EOS> 
df.to_csv('.. /working/submission.csv', index=False )