Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<define_variables>
warnings.filterwarnings(action='ignore', category=DataConversionWarning)
 warnings.filterwarnings(action='ignore', category=DeprecationWarning)
 warnings.filterwarnings(action='ignore', category=FutureWarning )
test_fnames = [path/'test'/test_id for test_id in test_ids]<define_variables>
train = pd.read_csv(".. /input/train.csv")
 compe = pd.read_csv(".. /input/test.csv")
 sample_sub = pd.read_csv(".. /input/sample_submission.csv")
 data = train.append(compe, sort=False )
src.add_test(test_fnames, label='0');<set_options>
types = pd.DataFrame(data.dtypes ).rename(columns={0: 'type'} ).sort_values(by=['type'],ascending=False)
 types
src.test.x.create_func = open_4_channel
src.test.x.open = open_4_channel<define_variables>
def check_missing(df):
null_val = df.isnull().sum()
percent = 100 * df.isnull().sum() /len(df)
 missing_table = pd.concat([null_val, percent], axis=1)
 col = missing_table.rename(columns = {0 : 'Num', 1 : 'Rate'})
 return col
print("Data
cols = check_missing(data)
 types.join(cols ).sort_values(by="Rate", ascending=False )
protein_stats =([0.08069, 0.05258, 0.05487, 0.08282], [0.13704, 0.10145, 0.15313, 0.13814] )<feature_engineering>
data.drop(['PoolQC', 'MiscFeature', 'Alley', 'Fence', 'FireplaceQu'], axis=1, inplace = True )
trn_tfms,_ = get_transforms(do_flip=True, flip_vert=True, max_rotate=30., max_zoom=1,
max_lighting=0.05, max_warp=0.)<categorify>
categorical_variables_string = \
['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']
for v in categorical_variables_string:
data[v] = data[v].fillna(data[v].mode() [0])
 data = pd.get_dummies(data, columns=[v], drop_first=True)
data =(src.transform(( trn_tfms, _), size=224)
.databunch(num_workers=0 ).normalize(protein_stats))<define_variables>
categorical_variables_int = \
['OverallQual', 'OverallCond', 'MoSold']
data.show_batch(rows=3, figsize=(12,9))<choose_model_class>
numerical_variavles = \
['MSSubClass', 'LotFrontage', 'LotArea', 'YearBuilt',
'YearRemodAdd', 'MasVnrArea', 'BsmtFinSF1', 'BsmtFinSF2',
'BsmtUnfSF', 'TotalBsmtSF', '1stFlrSF', '2ndFlrSF',
'LowQualFinSF', 'GrLivArea', 'BsmtFullBath', 'BsmtHalfBath',
'FullBath', 'HalfBath', 'BedroomAbvGr', 'KitchenAbvGr',
'TotRmsAbvGrd', 'Fireplaces', 'GarageYrBlt', 'GarageCars',
'GarageArea', 'WoodDeckSF', 'OpenPorchSF', 'EnclosedPorch',
'3SsnPorch', 'ScreenPorch', 'PoolArea', 'MiscVal', 'YrSold']
ss = StandardScaler()
for v in numerical_variavles:
data[v] = data[v].fillna(data[v].mean())
 data[v] = ss.fit_transform(data[[v]] )
def resnet50(pretrained):
return Resnet4Channel(encoder_depth=50 )<split>
train = data[:1460]
test = data[1460:]
def _resnet_split(m): return(m[0][6],m[1] )<compute_test_metric>
possible_features = train.columns.copy().drop('SalePrice' ).drop('Id')
 selector = SelectKBest(f_regression, len(possible_features))
selector.fit(train[possible_features], train['SalePrice'])
 scores = -np.log10(selector.pvalues_)
 indices = np.argsort(scores)[::-1]
print('Feature importances:')
 for i in range(len(scores)) :
print('%.2f %s' %(scores[indices[i]], possible_features[indices[i]]))
f1_score = partial(fbeta, thresh=0.2, beta=1 )<choose_model_class>
fparams = \
['OverallQual', 'GrLivArea', 'GarageCars', 'GarageArea', 'TotalBsmtSF',
'1stFlrSF', 'ExterQual_TA', 'FullBath', 'TotRmsAbvGrd', 'YearBuilt',
'KitchenQual_TA', 'GarageFinish_Unf', 'YearRemodAdd', 'BsmtQual_TA',
'Foundation_PConc', 'MasVnrArea', 'GarageYrBlt', 'Fireplaces',
'ExterQual_Gd', 'BsmtFinType1_GLQ', 'Neighborhood_NridgHt', 'BsmtFinSF1',
'MasVnrType_None', 'SaleType_New', 'GarageType_Detchd', 'SaleCondition_Partial',
'Foundation_CBlock', 'LotFrontage', 'MasVnrType_Stone', 'Neighborhood_NoRidge',
'WoodDeckSF', 'KitchenQual_Gd', 'BsmtExposure_No', '2ndFlrSF', 'OpenPorchSF',
'HeatingQC_TA', 'BsmtExposure_Gd', 'Exterior2nd_VinylSd', 'Exterior1st_VinylSd',
'MSZoning_RM', 'HalfBath', 'LotShape_Reg', 'LotArea', 'CentralAir_Y', 'MSZoning_RL',
'HouseStyle_2Story', 'SaleType_WD', 'Electrical_SBrkr', 'RoofStyle_Hip', 'GarageType_BuiltIn',
'BsmtQual_Gd', 'GarageType_Attchd', 'PavedDrive_Y', 'BsmtFullBath', 'RoofStyle_Gable',
'Neighborhood_StoneBr', 'BsmtUnfSF', 'MasVnrType_BrkFace', 'Neighborhood_OldTown',
'Neighborhood_NAmes', 'Neighborhood_Edwards', 'GarageFinish_RFn', 'RoofMatl_WdShngl',
'BedroomAbvGr', 'Exterior1st_MetalSd', 'Neighborhood_IDOTRR', 'Exterior2nd_MetalSd',
'Exterior2nd_Wd Sdng', 'Exterior1st_Wd Sdng', 'KitchenQual_Fa', 'SaleCondition_Normal',
'Neighborhood_BrkSide', 'LotConfig_CulDSac', 'Neighborhood_Somerst', 'ExterCond_Fa',
'GarageCond_TA', 'KitchenAbvGr', 'BsmtFinType1_Rec', 'HeatingQC_Gd', 'HeatingQC_Fa',
'Exterior1st_CemntBd', 'GarageQual_Fa', 'BsmtFinType1_Unf', 'BsmtFinType1_BLQ',
'GarageCond_Fa', 'BsmtQual_Fa', 'EnclosedPorch', 'Neighborhood_Sawyer', 'Exterior2nd_CmentBd',
'Electrical_FuseF', 'Neighborhood_Timber', 'LotShape_IR2', 'LandContour_HLS', 'Foundation_Slab',
'Condition1_Feedr', 'Functional_Typ', 'ExterQual_Fa', 'BldgType_Duplex', 'Condition1_Norm',
'Neighborhood_MeadowV', 'ScreenPorch', 'ExterCond_TA', 'RoofMatl_CompShg', 'Neighborhood_BrDale',
'BldgType_Twnhs', 'BldgType_2fmCon', 'GarageQual_TA', 'Exterior1st_HdBoard', 'HouseStyle_SFoyer',
'Heating_GasA', 'PoolArea', 'Heating_Grav', 'MSZoning_FV', 'BsmtCond_Gd', 'PavedDrive_P',
'HouseStyle_1.5Unf', 'BsmtFinType1_LwQ', 'MSSubClass', 'LotConfig_Inside', 'OverallCond',
'Exterior2nd_ImStucc', 'Neighborhood_CollgCr', 'Functional_Min2', 'Neighborhood_Crawfor',
'GarageType_CarPort', 'Functional_Maj2', 'Exterior2nd_HdBoard']
train_target = train["SalePrice"].values
train_features = train[fparams].values
test_features = test[fparams].values
learn = create_cnn(
 data,
resnet50,
cut=-2,
split_on=_resnet_split,
loss_func=F.binary_cross_entropy_with_logits,
path=path,
metrics=[f1_score],
model_dir="/tmp/models/"
)<find_best_params>
parameters = {
'loss': ['ls','huber'],
'n_estimators' : [1000],
'learning_rate': [0.02,0.03,0.04,0.05],
'max_depth': [2,4,6,8],
'max_features': ['sqrt'],
"min_samples_split": [2,3,4,8,16],
'min_samples_leaf': [2,3,4,8,16]
}
cv = GridSearchCV(GradientBoostingRegressor() , parameters, cv=8, scoring='neg_mean_absolute_error')
 cv.fit(train_features, train_target)
 print("GridSearch score: "+str(cv.best_score_))
print("GridSearch params: ")
 print(cv.best_params_ )
learn.lr_find()<define_search_space>
prediction = cv.best_estimator_.predict(test_features)
 pred = pd.DataFrame(pd.read_csv(".. /input/test.csv")['Id'])
 pred['SalePrice'] = prediction
pred.to_csv(".. /working/submission.csv", index = False )