Kaggle competition: House Prices - Advanced Regression Techniques
submission.to_csv("submission_v4_1.csv", index=False)
 submission_stack.to_csv("submission_v4_2.csv", index=False)
 submission_mix.to_csv("submission_v4_3.csv", index=False)
 print('Save submission', datetime.now() , )<set_options>
train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv' )
%reload_ext autoreload
%autoreload 2
%matplotlib inline 
<import_modules>
def check_missing(df):
missing_cols = df.columns[df.isna().any() ].tolist()
total_missing = df[missing_cols].isnull().sum().sort_values(ascending=False)
 percentage =(df[missing_cols].isnull().sum())/(len(df[missing_cols]))
percentage = percentage.sort_values(ascending=False)
 print(pd.concat([total_missing, percentage], axis=1, keys= ['
from fastai import *
from fastai.vision import *
from fastai.tabular import *<load_from_csv>
check_missing(train )
train = pd.read_csv('.. /input/train.csv')
 train.head()<load_from_csv>
check_missing(test )
test = pd.read_csv(".. /input/test.csv")
 test.isnull().sum()<data_type_conversions>
train_cleaned = train.copy()
test_cleaned = test.copy()
train_cleaned['PoolQC'].fillna('None', inplace = True)
 test_cleaned['PoolQC'].fillna('None', inplace = True)
 train_cleaned['MiscFeature'].fillna('None', inplace = True)
 test_cleaned['MiscFeature'].fillna('None', inplace = True)
 train_cleaned['Alley'].fillna('None', inplace = True)
 test_cleaned['Alley'].fillna('None', inplace = True)
 train_cleaned['Fence'].fillna('None', inplace = True)
 test_cleaned['Fence'].fillna('None', inplace = True)
 train_cleaned['FireplaceQu'].fillna('None', inplace = True)
 test_cleaned['FireplaceQu'].fillna('None', inplace = True)
 train_cleaned['GarageCond'].fillna('None', inplace = True)
 test_cleaned['GarageCond'].fillna('None', inplace = True)
 train_cleaned['GarageQual'].fillna('None', inplace = True)
 test_cleaned['GarageQual'].fillna('None', inplace = True)
 train_cleaned['GarageFinish'].fillna('None', inplace = True)
 test_cleaned['GarageFinish'].fillna('None', inplace = True)
 train_cleaned['GarageType'].fillna('None', inplace = True)
 test_cleaned['GarageType'].fillna('None', inplace = True)
 train_cleaned['GarageYrBlt'].fillna(0, inplace = True)
 test_cleaned['GarageYrBlt'].fillna(0, inplace = True)
 test_cleaned['GarageArea'].fillna(0, inplace = True)
 test_cleaned['GarageCars'].fillna(0, inplace = True)
 train_cleaned['BsmtFinType2'].fillna('None', inplace = True)
 test_cleaned['BsmtFinType2'].fillna('None', inplace = True)
 train_cleaned['BsmtExposure'].fillna('None', inplace = True)
 test_cleaned['BsmtExposure'].fillna('None', inplace = True)
 train_cleaned['BsmtFinType1'].fillna('None', inplace = True)
 test_cleaned['BsmtFinType1'].fillna('None', inplace = True)
 train_cleaned['BsmtCond'].fillna('None', inplace = True)
 test_cleaned['BsmtCond'].fillna('None', inplace = True)
 train_cleaned['BsmtQual'].fillna('None', inplace = True)
 test_cleaned['BsmtQual'].fillna('None', inplace = True)
 test_cleaned['BsmtHalfBath'].fillna(0, inplace = True)
 test_cleaned['BsmtFullBath'].fillna(0, inplace = True)
 test_cleaned['TotalBsmtSF'].fillna(0, inplace = True)
 test_cleaned['BsmtUnfSF'].fillna(0, inplace = True)
 test_cleaned['BsmtFinSF2'].fillna(0, inplace = True)
 test_cleaned['BsmtFinSF1'].fillna(0, inplace = True)
 train_cleaned['MasVnrType'].fillna('None', inplace = True)
 test_cleaned['MasVnrType'].fillna('None', inplace = True)
 train_cleaned['MasVnrArea'].fillna(0, inplace = True)
 test_cleaned['MasVnrArea'].fillna(0, inplace = True)
 train_cleaned['Electrical'].fillna('SBrkr', inplace=True)
 test_cleaned['KitchenQual'].fillna('TA', inplace=True)
 test_cleaned['Exterior1st'].fillna('VinylSd', inplace=True)
 test_cleaned['Exterior2nd'].fillna('VinylSd', inplace=True)
 test_cleaned['MSZoning'].fillna('RL', inplace=True)
 test_cleaned['Functional'].fillna('Typ', inplace=True)
 test_cleaned['Utilities'].fillna('AllPub', inplace=True)
 test_cleaned['SaleType'].fillna('WD', inplace=True)
 print(train_cleaned.groupby('Neighborhood' ).LotFrontage.median())
 train_cleaned['LotFrontage'] = train_cleaned.groupby('Neighborhood' ).LotFrontage.transform(lambda x: x.fillna(x.median()))
test_cleaned['LotFrontage'] = test_cleaned.groupby('Neighborhood' ).LotFrontage.transform(lambda x: x.fillna(x.median()))
print("_"*35)
 print(check_missing(train_cleaned))
print(check_missing(test_cleaned))
train["Embarked"] = train["Embarked"].fillna("C")
 test["Embarked"] = test["Embarked"].fillna("C")
 train['Fare'].fillna(train['Fare'].median() , inplace = True)
 test['Fare'].fillna(test['Fare'].median() , inplace = True)
 train.Cabin.fillna("N", inplace=True)
 test.Cabin.fillna("N", inplace=True )<feature_engineering>
train_ID = train_cleaned['Id']
test_ID = test_cleaned['Id']
train_cleaned = train_cleaned.drop(['Id'], axis=1)
 test_cleaned = test_cleaned.drop(['Id'], axis=1 )
train_title = [i.split(",")[1].split(".")[0].strip() for i in train["Name"]]
train["Title"] = pd.Series(train_title)
 train["Title"].head()
test_title = [i.split(",")[1].split(".")[0].strip() for i in test["Name"]]
test["Title"] = pd.Series(test_title)
 test["Title"].head()<define_variables>
train_cleaned['MSSubClass']=train_cleaned['MSSubClass'].apply(str)
 test_cleaned['MSSubClass']=test_cleaned['MSSubClass'].apply(str)
 train_cleaned['MoSold']=train_cleaned['MoSold'].astype(str)
 test_cleaned['MoSold']=test_cleaned['MoSold'].astype(str )
test_id = test['PassengerId'] 
<define_variables>
train_cleaned = train_cleaned.replace({
'LotShape': {'IR3': 1, 'IR2': 2, 'IR1': 3, 'Reg': 4},
'LandSlope': {'Sev': 1, 'Mod': 2, 'Gtl': 3},
'ExterQual': {'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'BsmtQual' : {'None':0, 'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'BsmtCond' : {'None':0, 'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'BsmtExposure': {'None':0, 'No' : 1, 'Mn' : 2, 'Av' : 3, 'Gd' : 4},
'BsmtFinType1': {'None':0, 'Unf': 1, 'LwQ': 2, 'Rec': 3, 'BLQ': 4, 'ALQ': 5, 'GLQ': 6},
'BsmtFinType2': {'None':0, 'Unf': 1, 'LwQ': 2, 'Rec': 3, 'BLQ': 4, 'ALQ': 5, 'GLQ': 6},
'HeatingQC' : {'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'KitchenQual' : {'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'Functional': {'Sal': 1, 'Sev': 2, 'Maj2': 3, 'Maj1': 4, 'Mod': 5, 'Min2': 6, 'Min1': 7, 'Typ': 8},
'FireplaceQu': {'None':0, 'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'GarageFinish': {'None':0, 'Unf': 1, 'RFn': 2, 'Fin': 3},
'GarageQual': {'None':0, 'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'BsmtCond' : {'None':0, 'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'PavedDrive' : {'N' : 0, 'P' : 1, 'Y' : 2},
'PoolQC' : {'None':0, 'Fa' : 1, 'TA' : 2, 'Gd' : 3, 'Ex' : 4},
})
 test_cleaned = test_cleaned.replace({
'LotShape': {'IR3': 1, 'IR2': 2, 'IR1': 3, 'Reg': 4},
'LandSlope': {'Sev': 1, 'Mod': 2, 'Gtl': 3},
'ExterQual': {'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'BsmtQual' : {'None':0, 'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'BsmtCond' : {'None':0, 'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'BsmtExposure': {'None':0, 'No' : 1, 'Mn' : 2, 'Av' : 3, 'Gd' : 4},
'BsmtFinType1': {'None':0, 'Unf': 1, 'LwQ': 2, 'Rec': 3, 'BLQ': 4, 'ALQ': 5, 'GLQ': 6},
'BsmtFinType2': {'None':0, 'Unf': 1, 'LwQ': 2, 'Rec': 3, 'BLQ': 4, 'ALQ': 5, 'GLQ': 6},
'HeatingQC' : {'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'KitchenQual' : {'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'Functional': {'Sal': 1, 'Sev': 2, 'Maj2': 3, 'Maj1': 4, 'Mod': 5, 'Min2': 6, 'Min1': 7, 'Typ': 8},
'FireplaceQu': {'None':0, 'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'GarageFinish': {'None':0, 'Unf': 1, 'RFn': 2, 'Fin': 3},
'GarageQual': {'None':0, 'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4},
'BsmtCond' : {'None':0, 'Po' : 1, 'Fa' : 2, 'TA' : 3, 'Gd' : 4, 'Ex' : 5},
'PavedDrive' : {'N' : 0, 'P' : 1, 'Y' : 2},
'PoolQC' : {'None':0, 'Fa' : 1, 'TA' : 2, 'Gd' : 3, 'Ex' : 4},
} )
dep_var = 'Survived'
cat_names = ['Title', 'Sex', 'Ticket', 'Cabin', 'Embarked']
cont_names = [ 'Age', 'SibSp', 'Parch', 'Fare']
print("Categorical columns are : ", cat_names)
 print('Continuous numerical columns are :', cont_names)
 procs = [FillMissing, Categorify, Normalize] 
<categorify>
train_cleaned["TotalSF"] = train_cleaned["GrLivArea"] + train_cleaned["TotalBsmtSF"]
test_cleaned["TotalSF"] = test_cleaned["GrLivArea"] + test_cleaned["TotalBsmtSF"]
train_cleaned["FlrsSF"] = train_cleaned["1stFlrSF"] + train_cleaned["2ndFlrSF"]
test_cleaned["FlrsSF"] = test_cleaned["1stFlrSF"] + test_cleaned["2ndFlrSF"]
train_cleaned['QualScore'] =(train_cleaned['TotalSF'] / max(train_cleaned['TotalSF'])) * 10 + train_cleaned['OverallQual']
test_cleaned['QualScore'] =(test_cleaned['TotalSF'] / max(train_cleaned['TotalSF'])) * 10 + test_cleaned['OverallQual']
train_cleaned['BedBath'] = train_cleaned['BedroomAbvGr'] + train_cleaned['FullBath'] + 0.5*train_cleaned['HalfBath']
test_cleaned['BedBath'] = test_cleaned['BedroomAbvGr'] + test_cleaned['FullBath'] + 0.5*test_cleaned['HalfBath']
test = TabularList.from_df(test, cat_names=cat_names, cont_names=cont_names, procs=procs)
 data =(TabularList.from_df(train, path='.', cat_names=cat_names, cont_names=cont_names, procs=procs)
.split_by_idx(list(range(0,200)))
.label_from_df(cols = dep_var)
.add_test(test, label=0)
.databunch() )<define_variables>
corr = train_cleaned.corr()
corr.sort_values(["SalePrice"], ascending = False, inplace = True)
 print(corr.SalePrice )
data.show_batch(rows=10)
<choose_model_class>
s =(train_cleaned.dtypes == 'object')
 object_cols = list(s[s].index)
 object_cols
object_nunique = list(map(lambda col: train_cleaned[col].nunique() , object_cols))
d = dict(zip(object_cols, object_nunique))
sorted(d.items() , key=lambda x: x[1] )
learn = tabular_learner(data, layers=[200,100], metrics=accuracy, emb_drop=0.1)
<find_best_params>
cols_to_onehot = [col for col in object_cols if train_cleaned[col].nunique() <10]
cols_to_label = [col for col in object_cols if train_cleaned[col].nunique() >=10]
print(f"The following features will be one-hot encoded:
{cols_to_onehot}")
 print("-"*120)
 print(f"The following features will be label-encoded:
{cols_to_label}" )
learn.lr_find() 
<train_model>
alldata = pd.concat([train_cleaned.iloc[:,:-1], test_cleaned])
 label_encoder = LabelEncoder()
for col in cols_to_label:
label_encoder.fit(alldata[col])
 train_cleaned[col] = label_encoder.transform(train_cleaned[col])
 test_cleaned[col] = label_encoder.transform(test_cleaned[col])
 OH_encode = OneHotEncoder(handle_unknown='ignore', sparse= False)
 OH_encode.fit(alldata[cols_to_onehot])
 OH_train = pd.DataFrame(OH_encode.transform(train_cleaned[cols_to_onehot]))
OH_test = pd.DataFrame(OH_encode.transform(test_cleaned[cols_to_onehot]))
OH_train.index = train_cleaned.index
OH_test.index = test_cleaned.index
train_encoded = pd.concat([train_cleaned, OH_train], axis=1)
 test_encoded = pd.concat([test_cleaned, OH_test], axis=1)
 train_encoded = train_encoded.drop(cols_to_onehot, axis=1)
 test_encoded = test_encoded.drop(cols_to_onehot, axis=1 )
learn.fit(15, slice(1e-01))<predict_on_test>
preds, targets = learn.get_preds()
predictions = np.argmax(preds, axis = 1)
 pd.crosstab(predictions, targets)
<predict_on_test>
my_model = lgb.LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.0473, n_estimators=576,
max_bin = 95, bagging_fraction = 0.8195,
bagging_freq = 5, feature_fraction = 0.1742,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf =9, min_sum_hessian_in_leaf = 11.58)
predictions, *_ = learn.get_preds(DatasetType.Test)
 labels = np.argmax(predictions, 1 )<save_to_csv>
X = train_encoded.drop(['SalePrice'], axis=1)
 y = train_encoded.SalePrice
model_pipeline = Pipeline(steps=[ 
('model', my_model)
 ])
 scores = np.sqrt(-1*cross_val_score(model_pipeline, X, y, cv=5, scoring = 'neg_mean_squared_error'))
print(f"RMSE scores:
{scores}")
 print("-"*60)
 print(f"Average RMSE score:
{scores.mean() }" )
submission = pd.DataFrame({'PassengerId': test_id, 'Survived': labels})
 submission.to_csv('submission.csv', index=False)
 submission.head() 
<import_modules>
my_model.fit(X, y)
 preds_test = np.expm1(my_model.predict(test_encoded))
output = pd.DataFrame({'Id': test_ID,
'SalePrice': preds_test})
 output.to_csv('submission_ML.csv', index=False )
import datetime
import imageio
import numpy as np
import pandas as pd
import pickle
from sklearn.model_selection import train_test_split
from fastai.vision import *
from fastai.metrics import accuracy, error_rate
from fastai.widgets import DatasetFormatter, PredictionsCorrector<import_modules>
def random_seed(seed_value):
random.seed(seed_value)
 np.random.seed(seed_value)
 torch.manual_seed(seed_value)
 if torch.cuda.is_available() :
torch.cuda.manual_seed(seed_value)
 torch.cuda.manual_seed_all(seed_value)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
random_seed(0)
 dep_var = 'SalePrice'
procs = [Categorify, Normalize]
dataset_cleaned = train_cleaned.drop(['SalePrice'], axis=1)
 cont_names = list(dataset_cleaned.select_dtypes(exclude = ['object', 'bool'] ).columns)
 cat_names = list(dataset_cleaned.select_dtypes(include = ['object', 'bool'] ).columns)
 test = TabularList.from_df(test_cleaned, cont_names=cont_names, cat_names=cat_names, procs=procs)
 data =(TabularList.from_df(train_cleaned, path='.', cont_names=cont_names, cat_names=cat_names, procs=procs)
.split_by_rand_pct(valid_pct = 0.2)
.label_from_df(cols = dep_var, label_cls = FloatList, log = False)
.add_test(test)
.databunch(bs = 128, num_workers=0))
data.show_batch(rows=5, ds_type=DatasetType.Valid )
print(fastai.__version__ )<define_variables>
random_seed(0)
 learn.fit_one_cycle(40, max_lr = 1e-01 )
np.random.seed(42 )<load_from_csv>
preds, _ = learn.get_preds(DatasetType.Test)
 prices = [np.expm1(p[0].data.item())for p in preds]
submission = pd.DataFrame({'Id': test_ID,
'SalePrice': prices})
 submission.to_csv('submission_fastai.csv', index=False )
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )<prepare_x_and_y>
scaler = MinMaxScaler()
X = train_encoded.drop(['SalePrice'], axis=1 ).values
y = train_encoded.SalePrice.values
X_scaled = scaler.fit_transform(X)
 test_scaled = scaler.transform(test_encoded.values)
 tf.random.set_seed(0)
 model = Sequential()
model.add(Dense(193, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
 model.fit(X_scaled, y,
batch_size=64,
epochs=400,
validation_split = 0.2,
verbose = 0)
 losses = pd.DataFrame(model.history.history)
 losses.plot()
preds_test = np.expm1(model.predict(test_scaled))
output = pd.DataFrame({'Id': test_ID,
'SalePrice': preds_test.flatten() })
 output.to_csv('submission_TensorFlow.csv', index=False )
<split><EOS> 
%f using %s" %(grid_result.best_score_, grid_result.best_params_))
%f(%f)with: %r" %(mean, stdev, param))