text_plain_output_1.png
from sklearn.compose import ColumnTransformer

from sklearn.ensemble import RandomForestRegressor

from sklearn.impute import SimpleImputer

from sklearn.linear_model import LinearRegression

from sklearn.model_selection import KFold

from sklearn.model_selection import cross_validate

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import OneHotEncoder

from sklearn.preprocessing import OrdinalEncoder

from xgboost import XGBRegressor

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

df_train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
df_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')

(df_train.shape, df_test.shape)

numerical_features = [cols for cols in df_train if df_train[cols].dtype in ['int', 'float']]
categorical_features = [cols for cols in df_train if df_train[cols].dtype == 'object']

numerical_missing_columns = list(df_train[numerical_features].columns[df_train[numerical_features].isna().any()])

(df_train[numerical_missing_columns].isnull().sum() / df_train.shape[0] * 100).sort_values(ascending=False)

data_frames = [df_train, df_test]
for df in data_frames:
    df.drop('GarageYrBlt', axis=1, inplace=True)
numerical_features.remove('GarageYrBlt')

from sklearn.impute import SimpleImputer
imput_mean = SimpleImputer(missing_values=np.NaN, strategy='mean')
imput_with_zero = SimpleImputer(strategy='constant', fill_value=0)
df_train['LotFrontage'] = imput_mean.fit_transform(df_train[['LotFrontage']]).astype('int')
df_test['LotFrontage'] = imput_mean.transform(df_test[['LotFrontage']]).astype('int')
df_train['MasVnrArea'] = imput_with_zero.fit_transform(df_train[['MasVnrArea']]).astype('int')
df_test['MasVnrArea'] = imput_mean.transform(df_test[['MasVnrArea']]).astype('int')

categorical_features_to_investigate = [col for col in categorical_features if df_train[col].nunique() <= 6]
categorical_features_to_delete = [col for col in categorical_features if col not in categorical_features_to_investigate]
data_frames = [df_train, df_test]
for df in data_frames:
    df.drop(categorical_features_to_delete, axis=1, inplace=True)

categorical_missing_columns = list(df_train[categorical_features_to_investigate].columns[df_train[categorical_features_to_investigate].isnull().any()])
(df_train[categorical_missing_columns].isnull().sum() / df_train.shape[0] * 100).sort_values(ascending=False)

ordinal_categories = ['Street', 'LotShape', 'LandContour', 'LandSlope', 'ExterQual', 'CentralAir', 'PavedDrive']
nominal_categories = ['MSZoning', 'Utilities', 'LotConfig', 'BldgType', 'RoofStyle', 'KitchenQual', 'Foundation', 'Heating', 'SaleCondition', 'HeatingQC', 'ExterCond']

X = df_train.drop(['SalePrice'], axis=1)
y = df_train['SalePrice']

ordinal_encoder = OrdinalEncoder()
one_hot = OneHotEncoder(handle_unknown='ignore')
imput_mean = SimpleImputer(strategy='mean')
imput_with_zero = SimpleImputer(strategy='constant', fill_value=0)
preprocessor = ColumnTransformer(transformers=[('ordinal', ordinal_encoder, ordinal_categories), ('one hot', one_hot, nominal_categories), ('mean', imput_mean, ['LotFrontage']), ('zero', imput_with_zero, ['MasVnrArea'])])

from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from sklearn.linear_model import LinearRegression
model_random_regressor = Pipeline(steps=[('preprocessor', preprocessor), ('model', RandomForestRegressor(random_state=0))])
model_xgb = Pipeline(steps=[('preprocessor', preprocessor), ('model', XGBRegressor(random_state=0))])
model_linear = Pipeline(steps=[('preprocessor', preprocessor), ('model', LinearRegression())])

kfold = KFold(n_splits=5, shuffle=True, random_state=0)
rf_model_result = cross_validate(model_random_regressor, X, y, cv=kfold, scoring='neg_root_mean_squared_error')
xgb_result = cross_validate(model_xgb, X, y, cv=kfold, scoring='neg_root_mean_squared_error')
linear_result = cross_validate(model_linear, X, y, cv=kfold, scoring='neg_root_mean_squared_error')

print(f"The RMSE of Random Forest model was: {-rf_model_result['test_score'].mean()}")
print(f"The RMSE of XGB model was: {-xgb_result['test_score'].mean()}")
print(f"The RMSE of linear model was: {-linear_result['test_score'].mean()}")