|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | +# |
| 4 | +# Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +import pandas as pd |
| 8 | +import sklearn.datasets |
| 9 | +from sklearn.tree import DecisionTreeRegressor |
| 10 | +from sklearn.linear_model import LinearRegression |
| 11 | +from sklearn.model_selection import train_test_split |
| 12 | + |
| 13 | +from sasctl import Session |
| 14 | +from sasctl.tasks import register_model, publish_model |
| 15 | +from sasctl.services import model_repository as mr |
| 16 | +from sasctl.services import model_management as mm |
| 17 | + |
| 18 | + |
| 19 | +data = sklearn.datasets.load_boston() |
| 20 | +X = pd.DataFrame(data.data, columns=data.feature_names) |
| 21 | +y = pd.DataFrame(data.target, columns=['Price']) |
| 22 | + |
| 23 | +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3) |
| 24 | + |
| 25 | +# Establish a session with SAS Viya |
| 26 | +Session('hostname', 'username', 'password') |
| 27 | + |
| 28 | +project = 'Boston Housing' |
| 29 | +model_name = 'Boston Regression' |
| 30 | + |
| 31 | +# Fit a linear regression model using sci-kit learn |
| 32 | +lm = LinearRegression() |
| 33 | +lm.fit(X_train, y_train) |
| 34 | + |
| 35 | +# Register the model in SAS Model Manager |
| 36 | +register_model(lm, |
| 37 | + model_name, |
| 38 | + input=X_train, # Use X to determine model inputs |
| 39 | + project=project, # Register in "Iris" project |
| 40 | + force=True) # Create project if it doesn't exist |
| 41 | + |
| 42 | +# Update project properties |
| 43 | +project = mr.get_project(project) |
| 44 | +project['function'] = 'prediction' |
| 45 | +project['targetLevel'] = 'interval' |
| 46 | +project['targetVariable'] = 'Price' |
| 47 | +project['predictionVariable'] = 'var1' |
| 48 | +project = mr.update_project(project) |
| 49 | + |
| 50 | +# Instruct the project to look for tables in the "Public" CAS library with |
| 51 | +# names starting with "boston_" and use these tables to track model |
| 52 | +# performance over time. |
| 53 | +mm.create_performance_definition(model_name, 'Public', 'boston') |
| 54 | + |
| 55 | +# Publish the model to the real-time scoring engine |
| 56 | +module_lm = publish_model(model_name, 'maslocal') |
| 57 | + |
| 58 | +# Select the first row of training data |
| 59 | +x = X.iloc[0, :] |
| 60 | + |
| 61 | +# Call the published module and score the record |
| 62 | +result = module_lm.score(**x) |
| 63 | +print(result) |
| 64 | + |
| 65 | +# Build a second model |
| 66 | +dt = DecisionTreeRegressor() |
| 67 | +dt.fit(X_train, y_train) |
| 68 | + |
| 69 | +# Register the second model in Model Manager |
| 70 | +model_dt = register_model(dt, 'Decision Tree', project, input=X) |
| 71 | + |
| 72 | +# Publish from Model Manager -> MAS |
| 73 | +module_dt = publish_model(model_dt, 'maslocal') |
| 74 | + |
| 75 | +# Use MAS to score some new data |
| 76 | +result = module_dt.score(**x) |
| 77 | +print(result) |
| 78 | + |
| 79 | + |
| 80 | + |
0 commit comments