Skip to content

Commit 555516c

Browse files
committed
switch to sklearn
1 parent 94e0e74 commit 555516c

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

docs/source/user_guide/model_registration/frameworks/genericmodel.rst

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -181,46 +181,31 @@ The example is illustrated using an AutoMLx model.
181181

182182
.. code-block:: python3
183183
184-
import automl
185-
import ads
186-
from automl import init
187-
from sklearn.datasets import fetch_openml
188-
from sklearn.model_selection import train_test_split
184+
import tempfile
185+
from ads import set_auth
189186
from ads.model import GenericModel
187+
from sklearn.datasets import load_iris
188+
from sklearn.linear_model import LogisticRegression
189+
from sklearn.model_selection import train_test_split
190190
191-
dataset = fetch_openml(name='adult', as_frame=True)
192-
df, y = dataset.data, dataset.target
193-
194-
# Several of the columns are incorrectly labeled as category type in the original dataset
195-
numeric_columns = ['age', 'capitalgain', 'capitalloss', 'hoursperweek']
196-
for col in df.columns:
197-
if col in numeric_columns:
198-
df[col] = df[col].astype(int)
199-
200-
201-
X_train, X_test, y_train, y_test = train_test_split(df,
202-
y.map({'>50K': 1, '<=50K': 0}).astype(int),
203-
train_size=0.7,
204-
random_state=0)
205-
206-
X_train.shape, X_test.shape
207-
208-
# create a AutoMLx model
209-
init(engine='local')
191+
set_auth(auth="resource_principal")
210192
211-
est = automl.Pipeline(task='classification')
212-
est.fit(X_train, y_train)
193+
# Load dataset and Prepare train and test split
194+
iris = load_iris()
195+
X, y = iris.data, iris.target
196+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
213197
214-
# Authentication
215-
ads.set_auth(auth="resource_principal")
198+
# Train a LogisticRegression model
199+
sklearn_estimator = LogisticRegression()
200+
sklearn_estimator.fit(X_train, y_train)
216201
217202
# Serialize your model. You can choose your own way to serialize your model.
218203
import cloudpickle
219204
with open("./model.pkl", "wb") as f:
220-
cloudpickle.dump(est, f)
205+
cloudpickle.dump(sklearn_estimator, f)
221206
222-
model = GenericModel(est, artifact_dir = "model_artifact_folder", serialize=False)
223-
model.prepare(inference_conda_env="automlx_p38_cpu_v1",force_overwrite=True, model_file_name="model.pkl", X_sample=X_test)
207+
model = GenericModel(sklearn_estimator, artifact_dir = "model_artifact_folder", serialize=False)
208+
model.prepare(inference_conda_env="generalml_p38_cpu_v1",force_overwrite=True, model_file_name="model.pkl", X_sample=X_test)
224209
225210
Now copy the model.pkl file and paste into the ``model_artifact_folder`` folder. And open the score.py in the ``model_artifact_folder`` folder to add implementation of the ``load_model`` function. You can also add your preprocessing steps in ``pre_inference`` function and postprocessing steps in ``post_inference`` function. Below is an example implementation of the score.py.
226211
Replace your score.py with the code below.
@@ -378,15 +363,15 @@ Save the score.py and now call ``.verify()`` to check if it works locally.
378363

379364
.. code-block:: python3
380365
381-
model.verify(X_test.iloc[:2], auto_serialize_data=True)
366+
model.verify(X_test[:2], auto_serialize_data=True)
382367
383368
After verify run successfully, you can save the model to model catalog, deploy and call predict to invoke the endpoint.
384369

385370
.. code-block:: python3
386371
387372
model_id = model.save(display_name='Demo AutoMLModel model')
388373
deploy = model.deploy(display_name='Demo AutoMLModel deployment')
389-
model.predict(X_test.iloc[:2].to_json())
374+
model.predict(X_test[:2].tolist())
390375
391376
You can also use the shortcut ``.prepare_save_deploy()`` instead of calling ``.prepare()``, ``.save()`` and ``.deploy()`` seperately.
392377

0 commit comments

Comments
 (0)