-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathSklearn_Iris.py
149 lines (110 loc) · 4.45 KB
/
Sklearn_Iris.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Databricks notebook source
# MAGIC %md ## Sklearn Iris MLflow model
# MAGIC
# MAGIC Simple Iris Sklearn model.
# COMMAND ----------
# MAGIC %md ### Setup
# COMMAND ----------
# MAGIC %run ./Common
# COMMAND ----------
dbutils.widgets.text("1. Experiment name","")
experiment_name = dbutils.widgets.get("1. Experiment name")
dbutils.widgets.text("2. Registered model","")
model_name = dbutils.widgets.get("2. Registered model")
dbutils.widgets.dropdown("3. Model version stage","None", _model_version_stages)
model_version_stage = dbutils.widgets.get("3. Model version stage")
if model_version_stage=="None": model_version_stage = None
dbutils.widgets.text("4. Data path", "")
data_path = dbutils.widgets.get("4. Data path")
if data_path=="": data_path = None
dbutils.widgets.text("5. Max depth", "1")
max_depth = to_int(dbutils.widgets.get("5. Max depth"))
print("experiment_name:", experiment_name)
print("model_name:", model_name)
print("model_version_stage:", model_version_stage)
print("data_path:", data_path)
print("max_depth:", max_depth)
# COMMAND ----------
import mlflow
if experiment_name:
mlflow.set_experiment(experiment_name)
if model_name:
set_model_registry(model_name)
# COMMAND ----------
# MAGIC %md ### Get data
# COMMAND ----------
from sklearn import datasets
from sklearn.model_selection import train_test_split
def get_data(data_path=None):
if not data_path:
print("Loading default data")
dataset = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(dataset.data, dataset.target, test_size=0.3)
else:
print(f"Loading data from {data_path}")
import pandas as pd
df = pd.read_csv(mk_local_path(data_path))
train, test = train_test_split(df, test_size=0.30, random_state=42)
col_label = "species"
X_train = train.drop([col_label], axis=1)
X_test = test.drop([col_label], axis=1)
y_train = train[[col_label]]
y_test = test[[col_label]]
return X_train, X_test, y_train, y_test
# COMMAND ----------
X_train, X_test, y_train, y_test = get_data(data_path)
# COMMAND ----------
# MAGIC %md ### Train model
# COMMAND ----------
def _register_model(run,
model_name,
model_version_stage = None,
archive_existing_versions = False,
model_alias = None,
model_artifact = "model"
):
""" Register mode with specified stage and alias """
print(">> XX.1: model_name", model_name)
print(">> XX.1: client._registry_uri", client._registry_uri)
try:
model = client.create_registered_model(model_name)
except RestException as e:
model = client.get_registered_model(model_name)
source = f"{run.info.artifact_uri}/{model_artifact}"
vr = client.create_model_version(model_name, source, run.info.run_id)
if is_unity_catalog(model_name):
print(">> XX.2a")
if model_alias:
print(f"Setting model '{model_name}/{vr.version}' alias to '{model_alias}'")
client.set_registered_model_alias(model_name, model_alias, vr.version)
elif model_version_stage and model_version_stage != "None":
print(">> XX.2b")
print(f"Transitioning model '{model_name}/{vr.version}' to stage '{model_version_stage}'")
client.transition_model_version_stage(model_name, vr.version, model_version_stage, archive_existing_versions=False)
return vr
# COMMAND ----------
from sklearn.tree import DecisionTreeClassifier
from mlflow.models.signature import infer_signature
run_name=f"{now} - {mlflow.__version__}"
with mlflow.start_run(run_name=run_name) as run:
print("run_id:", run.info.run_id)
print("experiment_id:", run.info.experiment_id)
mlflow.set_tag("mlflow_version", mlflow.__version__)
mlflow.set_tag("data_path", data_path)
mlflow.log_param("max_depth",max_depth)
model = DecisionTreeClassifier(max_depth=max_depth)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
signature = infer_signature(X_train, predictions)
mlflow.sklearn.log_model(model, "model", signature=signature)
if model_name:
version = register_model(run, model_name, model_version_stage)
# COMMAND ----------
# MAGIC %md ### Display UI links
# COMMAND ----------
display_run_uri(run.info.experiment_id, run.info.run_id)
# COMMAND ----------
display_experiment_id_info(run.info.experiment_id)
# COMMAND ----------
if model_name:
display_registered_model_version_uri(model_name, version.version)