<jupyter_start><jupyter_text>Quick Regression Example on using Xgboost and Skater<jupyter_code>%matplotlib inline
import matplotlib.pyplot
from sklearn.datasets import load_boston, load_breast_cancer
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.linear_model import LogisticRegression, LinearRegression

import matplotlib.pyplot as plt
# Reference for customizing matplotlib: https://matplotlib.org/users/style_sheets.html
plt.style.use('ggplot')
import pandas as pd
import numpy as np

# Load Bosting housing data
regressor_data = load_boston()
# Get information about the data
print(regressor_data.DESCR)
regressor_X = regressor_data.data
regressor_y = regressor_data.target
regressor_data.feature_names
from xgboost import XGBRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_validate
# XGRegressor example
xgb = XGBRegressor(objective='reg:linear', booster='gbtree', n_jobs= -1)
#XGB parameters for grid search
xgb_grid = {"max_depth" : [6],
"learning_rate" : [0.2],
"gamma" : [0],
"n_estimators" : [150],
"min_child_weight" : [1],
"base_score" : [0.5],
"subsample" : [1],
"max_delta_step" : [0],
"colsample_bytree" : [0.5],
"colsample_bylevel" : [0.4],
"reg_alpha" : [0],
"reg_lambda" : [60],
"scale_pos_weight" : [1]
}

clf = GridSearchCV(xgb, param_grid = xgb_grid, cv = 3, n_jobs = -1)
clf.fit(regressor_X, regressor_y)
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
y_hat = clf.predict(regressor_X)
print(mean_squared_error(regressor_y, y_hat))
print(r2_score(regressor_y, y_hat))<jupyter_output>1.7361768728736837
0.9794339492894043<jupyter_text>Inference with Skater<jupyter_code>from skater.core.explanations import Interpretation
from skater.model import InMemoryModel

interpreter = Interpretation(regressor_X, feature_names=regressor_data.feature_names)
annotated_model = InMemoryModel(clf.predict, examples=regressor_X)
print("Number of classes: {}".format(annotated_model.n_classes))
print("Input shape: {}".format(annotated_model.input_shape))
print("Model Type: {}".format(annotated_model.model_type))
print("Output Shape: {}".format(annotated_model.output_shape))
print("Output Type: {}".format(annotated_model.output_type))
print("Returns Probabilities: {}".format(annotated_model.probability))
# There is some issue in the way xgboost handles the thread pool for concurrency and other multiprocessing python 
# libraries. For more information follow the discussion at the below mentioned links
# 1. https://github.com/datascienceinc/Skater/issues/212
# 2. https://github.com/dmlc/xgboost/issues/2163
# 3. http://scikit-learn.org/stable/faq.html#why-do-i-sometime-get-a-crash-freeze-with-n-jobs-1-under-osx-or-linux
# Workaround: The current workaround is to use n_jobs=

print("2-way partial dependence plots")
# Features can passed as a tuple for 2-way partial plot
pdp_features = [('DIS', 'RM')]
interpreter.partial_dependence.plot_partial_dependence(
    pdp_features, annotated_model, grid_resolution=30, n_jobs=1
)
print("1-way partial dependence plots")
# or as independent features for 1-way partial plots
pdp_features = ['DIS', 'RM']
interpreter.partial_dependence.plot_partial_dependence(
    pdp_features, annotated_model, grid_resolution=30, progressbar=False, n_jobs=1, with_variance=True
)<jupyter_output>1-way partial dependence plots
{'fraction_non_alphanumeric': 0.11005917159763313, 'fraction_numerical': 0.02396449704142012, 'mean_word_length': 7.166666666666667, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '513433', 'license': 'UPL-1.0', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1149, 'n_words': 262, 'path': 'examples/xgboost_regression_example.ipynb', 'repo_name': 'BouncyButton/Skater', 'size': '3380'}