text_plain_output_1.png
from pathlib import Path

from sklearn.metrics import mean_squared_error

from sklearn.model_selection import train_test_split, KFold

import lightgbm as lgbm

import numpy as np

import pandas as pd

path = Path('/kaggle/input/house-prices-advanced-regression-techniques/')
train_ = pd.read_csv(path.joinpath('train.csv'))
test_ = pd.read_csv(path.joinpath('test.csv'))

categorical_feature = []
for col in train_.columns.tolist():
    if train_[col].dtype == object:
        categorical_feature.append(col)

nan_train_cols = train_.columns[1:-1][train_.iloc[:, 1:-1].isnull().sum() > 0].tolist()
for col in nan_train_cols:
    if col in categorical_feature:
        train_.loc[train_[col].isna(), col] = train_.loc[~train_[col].isna(), col].mode().values[0]
    else:
        train_.loc[train_[col].isna(), col] = train_.loc[~train_[col].isna(), col].mean()

nan_test_cols = test_.columns[1:-1][test_.iloc[:, 1:-1].isnull().sum() > 0].tolist()
for col in nan_test_cols:
    if col in categorical_feature:
        test_.loc[test_[col].isna(), col] = test_.loc[~test_[col].isna(), col].mode().values[0]
    else:
        test_.loc[test_[col].isna(), col] = test_.loc[~test_[col].isna(), col].mean()

columns_drop = ['GarageCars', 'GarageYrBlt', 'GrLivArea', 'Id', 'YearRemodAdd']
train_.drop(columns=columns_drop, inplace=True)
test_.drop(columns=columns_drop, inplace=True)

all_ = pd.concat([train_, test_])
dumy = pd.get_dummies(all_[categorical_feature])
all_ = pd.concat([all_.loc[:, ~all_.columns.isin(categorical_feature)], dumy], axis=1)
train_data = all_.iloc[0:1460, :]
test_data = all_.iloc[1460:, :]

test_data.drop(columns=['SalePrice'], inplace=True)
feature_cols = train_data.columns.tolist()
feature_cols.remove('SalePrice')

train_data['SalePrice'] = np.log(train_data['SalePrice'])
train_data, validation_data = train_test_split(train_data, test_size=0.2, random_state=42)
train_data.reset_index(drop=True, inplace=True)
validation_data.reset_index(drop=True, inplace=True)

baseline = lgbm.LGBMRegressor()
baseline.fit(train_data[feature_cols], train_data['SalePrice'])
baseline_val_y = baseline.predict(validation_data[feature_cols])
base_line_score = np.sqrt(mean_squared_error(baseline_val_y, validation_data['SalePrice'].values))
print(f'The base line score is : {base_line_score}')