<jupyter_start><jupyter_text>Install necessary packagesWe can install the necessary packages by either running `pip install --user ` or include everything in a `requirements.txt` file and run `pip install --user -r requirements.txt`.> NOTE: Do not forget to use the `--user` argument. It is necessary if you want to use Kale to transform this notebook into a Kubeflow pipeline<jupyter_code>!pip3 install --user -r requirements.txt<jupyter_output><empty_output><jupyter_text>ImportsIn this section we import the packages we need for this example. Make it a habbit to gather your imports in a single place. It will make your life easier if you are going to transform this notebook into a Kubeflow pipeline using Kale.<jupyter_code>import numpy as np
import xgboost as xgb

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score<jupyter_output><empty_output><jupyter_text>Project hyper-parametersIn this cell, we define the different hyper-parameters variables. Defining them in one place makes it easier to experiment with their values and also facilitates the execution of HP Tuning experiments using Kale and Katib.<jupyter_code>ETA = .3
MAX_DEPTH = 3
OBJECTIVE = "multi:softprob"
STEPS = 20<jupyter_output><empty_output><jupyter_text>Load and preprocess dataIn this section, we load and process the dataset to get it in a ready-to-use form by the model.<jupyter_code>x, y = datasets.load_iris(return_X_y=True)
x_trn, x_tst, y_trn, y_tst = train_test_split(x, y, test_size=.2)
D_trn = xgb.DMatrix(x_trn, label=y_trn)
D_tst = xgb.DMatrix(x_tst, label=y_tst)<jupyter_output><empty_output><jupyter_text>Define and train the modelWe are now ready to define our model. In this example, we use the Extreme Gradient Boosting algorithm inmplemented by [XGBoost](https://xgboost.ai/).<jupyter_code>param = {"eta": float(ETA),
         "max_depth": int(MAX_DEPTH),
         "objective": OBJECTIVE,
         "num_class": 3}

steps = int(STEPS)
model = xgb.train(param, D_trn, steps)<jupyter_output><empty_output><jupyter_text>Evaluate the modelFinally, we are ready to evaluate the model using the test set.<jupyter_code>preds = model.predict(D_tst)
max_preds = np.asarray([np.argmax(line) for line in preds])
precision = precision_score(y_tst, max_preds, average='macro')
recall = recall_score(y_tst, max_preds, average='macro')
f1 = f1_score(y_tst, max_preds, average='macro')
accuracy = accuracy_score(y_tst, max_preds)<jupyter_output><empty_output><jupyter_text>ServingWe can deploy the model as an inference server to KFServing, using the Kale `serve` API.<jupyter_code>from kale.common.serveutils import serve
kfserver = serve(model)
import json

data = {"instances": [[6.8, 2.8, 4.8, 1.4], [5.1, 3.5, 1.4, 0.2]]}
res = kfserver.predict(json.dumps(data))

print(res)<jupyter_output><empty_output><jupyter_text>Pipeline metricsIn the last cell of the Notebook, we print the pipeline metrics. These will be picked up by Kubeflow Pipelines, which will make them available through its UI.<jupyter_code>print(precision)
print(recall)
print(f1)
print(accuracy)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.090282131661442, 'fraction_numerical': 0.008463949843260187, 'mean_word_length': 6.091111111111111, 'pattern_counts': {'":': 5, '<': 33, '<?xml version=': 0, '>': 34, 'https://': 1, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '245136', 'license': 'Apache-2.0', 'n_tokens_mistral': 1087, 'n_tokens_neox': 1046, 'n_words': 340, 'path': 'examples/serving/xgboost/iris.ipynb', 'repo_name': 'kubeflow-kale/kale', 'size': '3190'}