<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Logistic Regression (scikit-learn) Experiment Versioning & Registry
<a href="https
Step1:  This example features
Step2:  Phase 1
Step3:  Prepare data
Step4:  Prepare hyperparameters
Step5:  Train models
Step6:  Revisit Workflow
This section demonstrates querying and retrieving runs via the Client.
Retrieve best run
Step7:  Train on full dataset
Step8:  Calculate accuracy on full training set
Step9:  Phase 2
<ASSISTANT_TASK:>
Python Code:
# restart your notebook if prompted on Colab
try:
    import verta
except ImportError:
    !pip install verta
Explanation: Logistic Regression (scikit-learn) Experiment Versioning & Registry
<a href="https://colab.research.google.com/github/VertaAI/modeldb/blob/master/client/workflows/demos/census-experiment-versioning-registry.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
Basic Verta Setup
End of explanation
HOST = "app.verta.ai"
PROJECT_NAME = "Census Income Classification"
EXPERIMENT_NAME = "Logistic Regression"
WORKSPACE = "XXXXX"
import os
os.environ['VERTA_EMAIL'] = 'XXXXXXXXXX'
os.environ['VERTA_DEV_KEY'] = 'XXXXXXXXXXXXXXXXXXXX'
from __future__ import print_function
import warnings
from sklearn.exceptions import ConvergenceWarning
warnings.filterwarnings("ignore", category=ConvergenceWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
import itertools
import os
import time
import six
import numpy as np
import pandas as pd
import sklearn
from sklearn import model_selection
from sklearn import linear_model
from sklearn import metrics
try:
    import wget
except ImportError:
    !pip install wget  # you may need pip3
    import wget
Explanation: This example features:
- scikit-learn's LinearRegression model
- verta model versioning and experiment tracking
- verta model staging and registry
End of explanation
from verta import Client
from verta.utils import ModelAPI
client = Client(HOST)
proj = client.set_project(PROJECT_NAME, workspace=WORKSPACE, public_within_org=True)
expt = client.set_experiment(EXPERIMENT_NAME)
Explanation: Phase 1: Model Development
This section demonstrates logging model metadata and training artifacts to ModelDB.
Instantiate client
End of explanation
train_data_url = "http://s3.amazonaws.com/verta-starter/census-train.csv"
train_data_filename = wget.detect_filename(train_data_url)
if not os.path.isfile(train_data_filename):
    wget.download(train_data_url)
test_data_url = "http://s3.amazonaws.com/verta-starter/census-test.csv"
test_data_filename = wget.detect_filename(test_data_url)
if not os.path.isfile(test_data_filename):
    wget.download(test_data_url)
from verta.dataset import Path
dataset = client.set_dataset(name="Census Income Local")
dataset_version = dataset.create_version(Path(train_data_filename))
df_train = pd.read_csv(train_data_filename)
X_train = df_train.iloc[:,:-1]
y_train = df_train.iloc[:, -1]
df_train.head()
Explanation: Prepare data
End of explanation
hyperparam_candidates = {
    'C': [1e-6, 1e-4],
    'solver': ['lbfgs'],
    'max_iter': [15, 28],
}
hyperparam_sets = [dict(zip(hyperparam_candidates.keys(), values))
                   for values
                   in itertools.product(*hyperparam_candidates.values())]
Explanation: Prepare hyperparameters
End of explanation
def run_experiment(hyperparams):
    # create object to track experiment run
    run = client.set_experiment_run()
    
    # log attributes
    run.log_attributes({
        'library': "scikit-learn",
        'model_type': "logistic regression",
    })
    
    # create validation split
    (X_val_train, X_val_test,
     y_val_train, y_val_test) = model_selection.train_test_split(X_train, y_train,
                                                                 test_size=0.2,
                                                                 shuffle=True)
    # log hyperparameters
    run.log_hyperparameters(hyperparams)
    print(hyperparams, end=' ')
    
    # create and train model
    model = linear_model.LogisticRegression(**hyperparams)
    model.fit(X_train, y_train)
    
    # calculate and log validation accuracy
    val_acc = model.score(X_val_test, y_val_test)
    run.log_metric("val_acc", val_acc)
    print("Validation accuracy: {:.4f}".format(val_acc))
    
    # create deployment artifacts
    model_api = ModelAPI(X_train, model.predict(X_train))
    requirements = ["scikit-learn"]
    
    # save and log model
    run.log_model(model, model_api=model_api, custom_modules=[])
    run.log_requirements(requirements)
    
    # log training data
    run.log_dataset_version("census_data", dataset_version)  # log dataset metadata
    
    # log git information
    run.log_code(
        repo_url="git@github.com:VertaAI/modeldb.git",
        commit_hash="d412a0d9",
        autocapture=False,
    )
    
# NOTE: run_experiment() could also be defined in a module, and executed in parallel
for hyperparams in hyperparam_sets:
    run_experiment(hyperparams)
Explanation: Train models
End of explanation
best_run = expt.expt_runs.sort("metrics.val_acc", descending=True)[0]
print("Validation Accuracy: {:.4f}".format(best_run.get_metric("val_acc")))
best_hyperparams = best_run.get_hyperparameters()
print("Hyperparameters: {}".format(best_hyperparams))
Explanation: Revisit Workflow
This section demonstrates querying and retrieving runs via the Client.
Retrieve best run
End of explanation
model = linear_model.LogisticRegression(multi_class='auto', **best_hyperparams)
model.fit(X_train, y_train)
Explanation: Train on full dataset
End of explanation
train_acc = model.score(X_train, y_train)
print("Training accuracy: {:.4f}".format(train_acc))
Explanation: Calculate accuracy on full training set
End of explanation
registered_model = client.get_or_create_registered_model(name="census", workspace=WORKSPACE, public_within_org=True)
registered_model.create_version_from_run(best_run.id, name="v0")
Explanation: Phase 2: Staging
Register the best model
The best-performing model can be staged as a registered model, for use downstream.
End of explanation
<END_TASK>