<jupyter_start><jupyter_text>Random Forest with Grid Search (XGBoost)<jupyter_code># restart your notebook if prompted on Colab
try:
    import verta
except ImportError:
    !pip install verta<jupyter_output><empty_output><jupyter_text>This example features:- **XGBoost**'s native API for cross-validating and training gradient-boosted trees- **scikit-learn**'s `ParameterGrid` utility for iterating over a hyperparameter grid- **verta**'s Python client integrated into the grid search loop- **verta**'s Python client retrieving the best run from the grid search to calculate and log full training accuracy<jupyter_code>HOST = "app.verta.ai"

PROJECT_NAME = "Wine Multiclassification"
EXPERIMENT_NAME = "Boosted Trees"
# import os
# os.environ['VERTA_EMAIL'] = 
# os.environ['VERTA_DEV_KEY'] =<jupyter_output><empty_output><jupyter_text>Imports<jupyter_code>import warnings
warnings.filterwarnings("ignore", category=FutureWarning)

import itertools
import multiprocessing
import time

import six

import numpy as np
import pandas as pd

import sklearn
from sklearn import datasets
from sklearn import model_selection

import xgboost as xgb<jupyter_output><empty_output><jupyter_text>---  Log Workflow  Prepare Data<jupyter_code>data = datasets.load_wine()

X = data['data']
y = data['target']

dtrain = xgb.DMatrix(X, label=y)
df = pd.DataFrame(np.hstack((X, y.reshape(-1, 1))),
                  columns=data['feature_names'] + ['species'])

df.head()<jupyter_output><empty_output><jupyter_text>Prepare Hyperparameters<jupyter_code>grid = model_selection.ParameterGrid({
    'eta': [0.5, 0.7],
    'max_depth': [1, 2, 3],
    'num_class': [10],
})<jupyter_output><empty_output><jupyter_text>Instantiate Client<jupyter_code>from verta import Client
from verta.utils import ModelAPI

client = Client(HOST)
proj = client.set_project(PROJECT_NAME)
expt = client.set_experiment(EXPERIMENT_NAME)<jupyter_output><empty_output><jupyter_text>Run Validation<jupyter_code>def run_experiment(hyperparams):
    run = client.set_experiment_run()
    
    # log hyperparameters
    run.log_hyperparameters(hyperparams)
    
    # run cross validation on hyperparameters
    cv_history = xgb.cv(hyperparams, dtrain,
                        nfold=5,
                        metrics=("merror", "mlogloss"))

    # log observations from each iteration
    for _, iteration in cv_history.iterrows():
        for obs, val in iteration.iteritems():
            run.log_observation(obs, val)
            
    # log error from final iteration
    final_val_error = iteration['test-merror-mean']
    run.log_metric("val_error", final_val_error)
    print("{} Mean error: {:.4f}".format(hyperparams, final_val_error))
    
with multiprocessing.Pool() as pool:
    pool.map(run_experiment, grid)<jupyter_output><empty_output><jupyter_text>---  Revisit Workflow  Retrieve Best Run<jupyter_code>best_run = expt.expt_runs.sort("metrics.val_error", descending=False)[0]
print("Validation Error: {:.4f}".format(best_run.get_metric("val_error")))

best_hyperparams = best_run.get_hyperparameters()
print("Hyperparameters: {}".format(best_hyperparams))<jupyter_output><empty_output><jupyter_text>Train on Full Dataset<jupyter_code>model = xgb.XGBClassifier(**best_hyperparams)
model.fit(X, y)<jupyter_output><empty_output><jupyter_text>Calculate and Log Accuracy on Full Training Set<jupyter_code>train_acc = model.score(X, y)
best_run.log_metric("train_acc", train_acc)
print("Training accuracy: {:.4f}".format(train_acc))<jupyter_output><empty_output><jupyter_text>Log Model for Deployment<jupyter_code># create deployment artifacts
model_api = ModelAPI(X, model.predict(X))
requirements = ["scikit-learn", "xgboost"]

best_run.log_model(model, model_api=model_api)
best_run.log_requirements(requirements)<jupyter_output><empty_output><jupyter_text>---  Make Live Predictions  Deploy Model Through Web App<jupyter_code>best_run<jupyter_output><empty_output><jupyter_text>Load Deployed Model<jupyter_code>from verta._demo_utils import DeployedModel

deployed_model = DeployedModel(HOST, best_run.id)<jupyter_output><empty_output><jupyter_text>Query Deployed Model<jupyter_code>for x in itertools.cycle(np.random.permutation(X).tolist()):
    print(deployed_model.predict([x]))
    time.sleep(.5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1143455985095482, 'fraction_numerical': 0.0039590125756870055, 'mean_word_length': 6.146422628951747, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, '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': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '30166', 'license': 'Apache-2.0', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1456, 'n_words': 284, 'path': 'client/workflows/examples/xgboost.ipynb', 'repo_name': 'CaptEmulation/modeldb', 'size': '4294'}