image_output_1.png
from sklearn.metrics import mean_squared_error

from sklearn.metrics import mean_squared_error, r2_score, make_scorer

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import RobustScaler

from xgboost import XGBRegressor

import numpy as np

import numpy as np

import pandas as pd

concat_california_array = np.concatenate((california.data, np.reshape(california.target, (california.target.shape[0], 1))), axis=1)
california_df = pd.DataFrame(concat_california_array, columns=california.feature_names + ['price'])

X = california.data
y = california.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

rs = RobustScaler()
X_train_rs = rs.fit_transform(X_train)
X_test_rs = rs.transform(X_test)

from xgboost import XGBRegressor
xg_regressor = XGBRegressor(max_depth=6, n_estimators=500, learning_rate=0.01, silent=True)
xg_regressor.fit(X_train_rs, y_train)
print('Feature importance:')
for name, score in zip(california['feature_names'], xg_regressor.feature_importances_):
    print(name, '{0:.2f} %'.format(score * 100))
print('\n' * 2, 'Scoring:')
print('MSE for test {0:.2f}'.format(mean_squared_error(y_test, xg_regressor.predict(X_test_rs))))
print('R-squared for test {0:.2f}'.format(xg_regressor.score(X_test_rs, y_test)))
print('MSE for train {0:.2f}'.format(mean_squared_error(y_train, xg_regressor.predict(X_train_rs))))
print('R-squared for train {0:.2f}'.format(xg_regressor.score(X_train_rs, y_train)))