Xgboost Classifier#
This example was adapted from an example by Bojan Tunguz. It demonstrates how to build a simple ML demo demonstrating how hyper-parameters affect the accuracy of the XGBoostClassifier.
import panel as pn
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score
from xgboost import XGBClassifier
pn.extension(sizing_mode="stretch_width")
Model#
train_accuracy is a plain function returning a number, so the indicator showing it is created once and only its value updates.
iris_df = load_iris(as_frame=True)
def train_accuracy(n_trees, max_depth, booster):
model = XGBClassifier(max_depth=max_depth, n_estimators=n_trees, booster=booster)
model.fit(iris_df.data, iris_df.target)
return round(accuracy_score(iris_df.target, model.predict(iris_df.data)) * 100, 1)
def format_params(n_trees, max_depth, booster):
return f'# <code>{n_trees=}, {max_depth=}, {booster=}</code>'
App#
n_trees = pn.ui.IntSlider(start=2, end=30, value=10, label="Number of trees")
max_depth = pn.ui.IntSlider(start=1, end=10, value=2, label="Maximum Depth")
booster = pn.ui.Select(options=['gbtree', 'gblinear', 'dart'], label="Booster")
accuracy = pn.ui.Number(
name="Test score",
value=pn.bind(train_accuracy, n_trees, max_depth, booster),
format="{value}%",
colors=[(97.5, "red"), (99.0, "orange"), (100, "green")],
align='center',
sizing_mode='fixed',
)
widgets = pn.ui.Column(booster, n_trees, max_depth)
main = pn.ui.Column(
pn.ui.Markdown("Simple example of training an XGBoost classification model on the small Iris dataset."),
pn.ui.DataFrame(iris_df.data.head()),
pn.ui.Markdown("Adjust the hyperparameters to re-run the XGBoost classifier. The training accuracy score will adjust accordingly:"),
accuracy,
pn.ui.Markdown(pn.bind(format_params, n_trees, max_depth, booster)),
)
page = pn.ui.Page(main=[main], sidebar=[widgets], title="XGBoost Example")
page
Served App#
if pn.state.served:
page.servable()