def test_register_model_403_error(get_project, get_repository): """Verify HTTP 403 is converted to a user-friendly error. Depending on environment configuration, this can happen when attempting to find a repository. See: https://github.com/sassoftware/python-sasctl/issues/39 """ from six.moves.urllib.error import HTTPError from sasctl.exceptions import AuthorizationError from sasctl.tasks import register_model get_project.return_value = {'name': 'Project Name'} get_repository.side_effect = HTTPError(None, 403, None, None, None) # HTTP 403 error when getting repository should throw a user-friendly # AuthorizationError with pytest.raises(AuthorizationError): register_model(None, 'model name', 'project name') # All other errors should be bubbled up get_repository.side_effect = HTTPError(None, 404, None, None, None) with pytest.raises(HTTPError): register_model(None, 'model name', 'project name')
def test_model_import(self, astore): model = register_model(astore, self.model_name, self.project_name, force=True) assert self.model_name == model.name
def test_model_import(self, iris_astore): """Import a model from an ASTORE""" model = register_model(iris_astore, self.MODEL_NAME, self.PROJECT_NAME, force=True) assert self.MODEL_NAME == model.name
def test_model_import(self, astore): """Import a model from an ASTORE""" model = register_model(astore, self.model_name, self.project_name, force=True) assert self.model_name == model.name
def test_register_astore(self, astore): from sasctl.tasks import register_model from sasctl import RestObj # Register model and ensure attributes are set correctly model = register_model(astore, ASTORE_MODEL_NAME, project=PROJECT_NAME, force=True) assert isinstance(model, RestObj) assert ASTORE_MODEL_NAME == model.name
def test_register_model(self, sklearn_linear_model): from sasctl.tasks import register_model from sasctl import RestObj sk_model, X, _ = sklearn_linear_model # Register model and ensure attributes are set correctly model = register_model(sk_model, self.MODEL_NAME, project=self.PROJECT_NAME, input=X, force=True) assert isinstance(model, RestObj) assert self.MODEL_NAME == model.name assert 'prediction' == model.function.lower() assert 'Linear regression' == model.algorithm assert 'Python' == model.trainCodeType assert 'ds2MultiType' == model.scoreCodeType assert len(model.inputVariables) == 13 assert len(model.outputVariables) == 1 # Don't compare to sys.version since cassettes used may have been # created by a different version assert re.match(r'Python \d\.\d', model.tool) # Ensure input & output metadata was set for col in X.columns: assert 1 == len([ v for v in model.inputVariables + model.outputVariables if v.get('name') == col ]) # Ensure model files were created from sasctl.services import model_repository as mr files = mr.get_model_contents(model) filenames = [f.name for f in files] assert 'model.pkl' in filenames assert 'dmcas_epscorecode.sas' in filenames assert 'dmcas_packagescorecode.sas' in filenames
def test_register_sklearn(self, sklearn_model): from sasctl.tasks import register_model from sasctl import RestObj sk_model, train_df = sklearn_model # Register model and ensure attributes are set correctly model = register_model(sk_model, SCIKIT_MODEL_NAME, project=PROJECT_NAME, input=train_df, force=True) assert isinstance(model, RestObj) assert SCIKIT_MODEL_NAME == model.name assert 'Classification' == model.function assert 'Logistic regression' == model.algorithm assert 'Python' == model.trainCodeType assert 'ds2MultiType' == model.scoreCodeType # Don't compare to sys.version since cassettes used may have been created by a different version assert re.match('Python \d\.\d', model.tool) # Ensure input & output metadata was set for col in train_df.columns: assert 1 == len([ v for v in model.inputVariables + model.outputVariables if v.get('name') == col ]) # Ensure model files were created from sasctl.services.model_repository import get_model_contents files = get_model_contents(model) filenames = [f.name for f in files] assert 'model.pkl' in filenames assert 'dmcas_espscorecode.sas' in filenames assert 'dmcas_packagescorecode.sas' in filenames
glm = conn.CASTable(name = 'glmmodel') Use the name specified in the tbl.glm statement in the preceeding example. gbm = conn.CASTable(name = 'gbtreemodel') Use the name specified in the tbl.decisiontree.gbtreetrain statement in the preceeding example. """ #Section d: Registering the model in the model repository #For more information, see the Authentication section of the sasctl User Guide. Session(host, authinfo='~/.authinfo', protocol='http') register_model( model= model_object, # Provide the model object that stores the developed model. name= 'model_name', # Provide a model name that is unique in model repository. project= 'project_name', # Provide the name of the project in which your model will be stored in model repository. force=True) # To create a project with project_name if it doesn't exist. """ For Example register_model(model = glm, name = 'glmmodel', project = 'continuous', force=True) register_model(model = gbm, name = 'gbtreemodel', project = 'binary', force=True) """
lr=0.001) # Export the model as an ASTORE and get a reference to the new ASTORE table s.deeplearn.dlexportmodel(modelTable=model.model_table, initWeights=model.model_weights, casout='astore_table') astore = s.CASTable('astore_table') # Connect to the SAS environment with Session('dsasspre', 'robinswu', 'password'): # Register the trained model by providing: # - the ASTORE containing the model # - a name for the model # - a name for the project # # NOTE: the force=True option will create the project if it does not exist. model = register_model(astore, 'Deep Learning', 'PGV', force=True) # Publish the model to SAS® Micro Analytic Service (MAS). Specifically to # the default MAS service "maslocal". module = publish_model(model, 'maslocal') # sasctl wraps the published module with Python methods corresponding to # the various steps defined in the module (like "predict"). response = module.score(SepalLength=5.1, SepalWidth=3.5, PetalLength=1.4, PetalWidth=0.2) s.terminate()
# encoding: utf-8 # # Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 import swat from sasctl import Session from sasctl.tasks import register_model, publish_model from sasctl.services import microanalytic_score as mas s = swat.CAS('hostname', 'username', 'password') s.loadactionset('decisionTree') tbl = s.CASTable('iris') tbl.decisiontree.gbtreetrain(target='Species', inputs=['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'], savestate='gradboost_astore') astore = s.CASTable('gradboost_astore') with Session(s): model = register_model(astore, 'Gradient Boosting', 'Iris') module = publish_model(model, 'maslocal') response = mas.execute_module_step(module, 'score', SepalLength=5.1, SepalWidth=3.5, PetalLength=1.4, PetalWidth=0.2)
cas.loadactionset('regression') # Upload the local CSV file to a CAS table tbl = cas.upload('data/boston_house_prices.csv').casTable # Model input features are everything except the target features = tbl.columns[tbl.columns != 'medv'] # Fit a linear regression model in CAS and output an ASTORE tbl.glm(target='medv', inputs=list(features), savestate='model_table') astore = cas.CASTable('model_table') # Use sasctl to connect to SAS Session('hostname', 'username', 'password') # Register the model in SAS Model Manager, creating the "Boston Housing" # project if it doesn't already exist model = register_model(astore, 'Linear Regression', 'Boston Housing', force=True) # Publish the model to a real-time scoring engine module = publish_model(model, 'maslocal') # Pass a row of data to MAS and receive the predicted result. first_row = tbl.head(1) result = module.score(first_row) print(result)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3) # Establish a session with SAS Viya Session('hostname', 'username', 'password') project = 'Boston Housing' model_name = 'Boston Regression' # Fit a linear regression model using sci-kit learn lm = LinearRegression() lm.fit(X_train, y_train) # Register the model in SAS Model Manager register_model( lm, model_name, input=X_train, # Use X to determine model inputs project=project, # Register in "Iris" project force=True) # Create project if it doesn't exist # Update project properties. Target variable must be set before performance # definitions can be created. project = mr.get_project(project) project['targetVariable'] = 'Price' project = mr.update_project(project) # Instruct the project to look for tables in the "Public" CAS library with # names starting with "boston_" and use these tables to track model # performance over time. mm.create_performance_definition(model_name, 'Public', 'boston') # Publish the model to the real-time scoring engine
cas.loadactionset('regression') # Upload the local CSV file to a CAS table tbl = cas.upload('/home/viya/data/Los_Angeles_houses_prices.csv').casTable # Model input features are everything except the target features = tbl.columns[tbl.columns != 'medv'] # Fit a linear regression model in CAS and output an ASTORE tbl.glm(target='medv', inputs=list(features), savestate='model_table') astore = cas.CASTable('model_table') # Use sasctl to connect to SAS Session('dsasspre.org', 'robinswu', 'password') # Register the model in SAS Model Manager, creating the "Boston Housing" # project if it doesn't already exist model = register_model(astore, 'Linear Regression', 'Los Angeles Housing', force=True) # Publish the model to a real-time scoring engine module = publish_model(model, 'maslocal') # Pass a row of data to MAS and receive the predicted result. first_row = tbl.head(1) result = module.score(first_row) print(result)
SimpleImputer(strategy='median'))]) # Preprocessing categorical variables categorical_transformer = Pipeline( steps=[('imputer', SimpleImputer(strategy='most_frequent') ), ('onehot', OneHotEncoder(handle_unknown='ignore'))]) # Create preprocessing step preprocessor = ColumnTransformer(transformers=[( 'num', numeric_transformer, dm_interval_input), ('cat', categorical_transformer, dm_class_input)]) # Define pipeline steps dm_model = Pipeline( steps=[('preprocessor', preprocessor), ('model', GradientBoostingClassifier())]) fit = dm_model.fit(X_train, y_train) # Score full data scored = dm_model.predict_proba(dm_inputdf) dm_scoreddf = pd.DataFrame( scored, columns=['P_' + dm_dec_target + level for level in dm_classtarget_level]) # Register model with Session('http://sasserver.demo.sas.com', 'username', 'password'): register_model(dm_model, 'OS Node sklearn', 'My MM Project Name', input=X_train)