Custom task creation notebook¶
In [1]:
Copied!
import datarobot as dr
from datarobot.models.execution_environment import ExecutionEnvironment
from datarobot_bp_workshop import Workshop, Visualize
from datarobot_bp_workshop.magic import *
import datarobot as dr
from datarobot.models.execution_environment import ExecutionEnvironment
from datarobot_bp_workshop import Workshop, Visualize
from datarobot_bp_workshop.magic import *
Connect to DataRobot¶
Read more about different options for connecting to DataRobot from the Python client.
In [2]:
Copied!
with open('../api.token', 'r') as f:
token = f.read()
dr.Client(token=token, endpoint='https://app.datarobot.com/api/v2')
with open('../api.token', 'r') as f:
token = f.read()
dr.Client(token=token, endpoint='https://app.datarobot.com/api/v2')
Initialize the Blueprint Workshop¶
In [3]:
Copied!
w = Workshop()
w = Workshop()
Get the Sklearn Drop-In Environment
In [4]:
Copied!
environments = ExecutionEnvironment.list()
environments = ExecutionEnvironment.list()
In [5]:
Copied!
scikit_env = environments[7]
scikit_env = environments[7]
In [6]:
Copied!
customr = w.create_custom_task(
environment_id=scikit_env.id,
name="My Custom Ridge Regressor w/ Imputation",
target_type="Regression",
description="Impute values and perform ridge regression."
)
# customr = w.get_custom_task('608e5bc8b66a4934d58d0d4e')
customr = w.create_custom_task(
environment_id=scikit_env.id,
name="My Custom Ridge Regressor w/ Imputation",
target_type="Regression",
description="Impute values and perform ridge regression."
)
# customr = w.get_custom_task('608e5bc8b66a4934d58d0d4e')
Iterate on a custom model¶
In [7]:
Copied!
%%update_custom {customr.id}
import pickle
import numpy as np
import pandas as pd
from typing import List, Optional
from sklearn.compose import make_column_selector, ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import Ridge
from sklearn.pipeline import Pipeline
def fit(
X: pd.DataFrame,
y: pd.Series,
output_dir: str,
class_order: Optional[List[str]] = None,
row_weights: Optional[np.ndarray] = None,
**kwargs,
):
numeric_transformer = ColumnTransformer(
transformers=[
(
"imputer",
SimpleImputer(strategy="median", add_indicator=True),
make_column_selector(dtype_include=np.number),
)
]
)
pipeline = Pipeline(steps=[("numeric", numeric_transformer), ("model", Ridge())])
pipeline.fit(X, y)
with open("{}/artifact.pkl".format(output_dir), "wb") as fp:
pickle.dump(pipeline, fp)
%%update_custom {customr.id}
import pickle
import numpy as np
import pandas as pd
from typing import List, Optional
from sklearn.compose import make_column_selector, ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import Ridge
from sklearn.pipeline import Pipeline
def fit(
X: pd.DataFrame,
y: pd.Series,
output_dir: str,
class_order: Optional[List[str]] = None,
row_weights: Optional[np.ndarray] = None,
**kwargs,
):
numeric_transformer = ColumnTransformer(
transformers=[
(
"imputer",
SimpleImputer(strategy="median", add_indicator=True),
make_column_selector(dtype_include=np.number),
)
]
)
pipeline = Pipeline(steps=[("numeric", numeric_transformer), ("model", Ridge())])
pipeline.fit(X, y)
with open("{}/artifact.pkl".format(output_dir), "wb") as fp:
pickle.dump(pipeline, fp)
Out[7]:
'608ef74c5dda651931052422'
In [10]:
Copied!
custom_ridge = w.CustomTask(customr.id)(w.TaskInputs.NUM)
custom_ridge_bp = w.BlueprintGraph(custom_ridge, name="My Inline Custom Blueprint")
custom_ridge = w.CustomTask(customr.id)(w.TaskInputs.NUM)
custom_ridge_bp = w.BlueprintGraph(custom_ridge, name="My Inline Custom Blueprint")
In [11]:
Copied!
custom_ridge_bp.show()
custom_ridge_bp.show()
In [12]:
Copied!
custom_ridge_bp.save()
custom_ridge_bp.save()
Out[12]:
Name: 'My Inline Custom Blueprint' Input Data: Numeric Tasks: My Custom Ridge Regressor w/ Imputation
In [13]:
Copied!
custom_ridge_bp.train('5eb9656901f6bb026828f14e')
custom_ridge_bp.train('5eb9656901f6bb026828f14e')
Out[13]:
Name: 'My Inline Custom Blueprint' Input Data: Numeric Tasks: My Custom Ridge Regressor w/ Imputation
Updated August 9, 2023
Was this page helpful?
Great! Let us know what you found helpful.
What can we do to improve the content?
Thanks for your feedback!