text_plain_output_1.png
from sklearn.compose import ColumnTransformer

from sklearn.impute import SimpleImputer

from sklearn.impute import SimpleImputer

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import OneHotEncoder

import numpy as np

import pandas as pd

import xgboost as xgb

train = pd.read_csv('../input/home-data-for-ml-course/train.csv')
test = pd.read_csv('../input/home-data-for-ml-course/test.csv')
id = test['Id']

train.describe

train.dtypes.unique()

list(train.select_dtypes(include=['int64']).columns)

list(train.select_dtypes(include=['float64']).columns)

list(train.select_dtypes(include=['O']).columns)

train.drop(['Id', 'YrSold', 'MoSold'], inplace=True, axis=1)
test.drop(['Id', 'YrSold', 'MoSold'], inplace=True, axis=1)

train.isnull().mean().round(4).mul(100).sort_values(ascending=False)

train = train.loc[:, train.isnull().mean() < 0.4]
test = test.loc[:, test.isnull().mean() < 0.4]
train.shape

from sklearn.impute import SimpleImputer
numerical_transformer = SimpleImputer(missing_values=np.NaN, strategy='mean')
categorical_transformer = Pipeline(steps=[('imputer', SimpleImputer(strategy='most_frequent')), ('onehot', OneHotEncoder(handle_unknown='ignore'))])

categorical_cols = [cname for cname in X_train.columns if X_train[cname].nunique() < 10 and X_train[cname].dtype == 'object']
numerical_cols = [cname for cname in X_train.columns if X_train[cname].dtype in ['int64', 'float64']]
my_cols = categorical_cols + numerical_cols
X_train = X_train[my_cols].copy()
X_test = X_test[my_cols].copy()
test = test[my_cols].copy()
preprocessor = ColumnTransformer(transformers=[('num', numerical_transformer, numerical_cols), ('cat', categorical_transformer, categorical_cols)])
X_train = preprocessor.fit_transform(X_train)
X_test = preprocessor.fit_transform(X_test)
test = preprocessor.fit_transform(test)

dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)

params = {'max_depth': 6, 'min_child_weight': 1, 'eta': 0.3, 'subsample': 1, 'colsample_bytree': 1, 'objective': 'reg:linear'}

num_boost_round = 999

model = xgb.train(params, dtrain, num_boost_round=num_boost_round, evals=[(dtest, 'Test')], early_stopping_rounds=10)
print('Best MAE: {:.2f} with {} rounds'.format(model.best_score, model.best_iteration + 1))