Пример #1
0
def test(cas_session, iris_dataset):
    pytest.skip('Re-enable once MAS publish no longer hangs.')
    cas_session.loadactionset('decisiontree')

    tbl = cas_session.upload(iris_dataset).casTable
    features = list(tbl.columns[tbl.columns != TARGET])

    # Fit a linear regression model in CAS and output an ASTORE
    tbl.decisiontree.gbtreetrain(target=TARGET,
                                 inputs=features,
                                 savestate='model_table')
    astore = cas_session.CASTable('model_table')

    from sklearn.ensemble import GradientBoostingClassifier
    X = iris_dataset.drop(TARGET, axis=1)
    y = iris_dataset[TARGET]
    sk_model = GradientBoostingClassifier()
    sk_model.fit(X, y)

    sas_model = register_model(astore,
                               SAS_MODEL_NAME,
                               PROJECT_NAME,
                               force=True)
    sk_model = register_model(sk_model,
                              SCIKIT_MODEL_NAME,
                              PROJECT_NAME,
                              input=X)

    # Publish to MAS
    sas_module = publish_model(sas_model, 'maslocal', replace=True)
    sk_module = publish_model(sk_model, 'maslocal', replace=True)

    # Pass a row of data to MAS and receive the predicted result.
    first_row = tbl.head(1)
    result = sas_module.score(first_row)
    p1, p1, p2, species, warning = result

    result2 = sk_module.predict(first_row)
    assert result2 in ('setosa', 'virginica', 'versicolor')

    # SAS model may have CHAR variable that's padded with spaces.
    assert species.strip() == result2

    result3 = sk_module.predict_proba(first_row)
    assert round(sum(result3), 5) == 1
    def test_publish_mas(self, request):
        from sasctl import publish_model

        module = publish_model(self.MODEL_NAME, 'maslocal', replace=True)

        # Store module name so we can retrieve it in later tests
        request.config.cache.set('MAS_MODULE_NAME', module.name)

        assert module.scope.lower() == 'public'
        assert hasattr(module, 'predict')
    def test_publish_cas(self, request):
        from sasctl import publish_model

        module = publish_model(self.MODEL_NAME, self.CAS_DEST)

        # Store module name so we can retrieve it in later tests
        request.config.cache.set('CAS_MODULE_NAME', module.name)

        assert module.state == 'completed'
        assert module.destinationName == self.CAS_DEST
        assert module.publishType == 'casModel'
Пример #4
0
def test(cas_session, boston_dataset):
    cas_session.loadactionset('regression')

    tbl = cas_session.upload(boston_dataset).casTable
    features = tbl.columns[tbl.columns != 'Price']

    # Fit a linear regression model in CAS and output an ASTORE
    tbl.glm(target='Price', inputs=list(features), savestate='model_table')
    astore = cas_session.CASTable('model_table')

    from sklearn.linear_model import LinearRegression
    X = boston_dataset.drop('Price', axis=1)
    y = boston_dataset['Price']
    sk_model = LinearRegression()
    sk_model.fit(X, y)

    sas_model = register_model(astore,
                               SAS_MODEL_NAME,
                               PROJECT_NAME,
                               force=True)
    sk_model = register_model(sk_model,
                              SCIKIT_MODEL_NAME,
                              PROJECT_NAME,
                              input=X)

    # Publish to MAS
    sas_module = publish_model(sas_model, 'maslocal', replace=True)
    sk_module = publish_model(sk_model, 'maslocal', replace=True)

    # Pass a row of data to MAS and receive the predicted result.
    first_row = tbl.head(1)
    result = sas_module.score(first_row)
    assert isinstance(result, float)

    result2 = sk_module.predict(first_row)
    assert isinstance(result2, float)

    assert round(result, 5) == round(result2, 5)
Пример #5
0
                                           file=_file,
                                           name=filenames['file'][i],
                                           role=filenames['role'][i])

        print('uploaded ' + filenames['file'][i] + ' as ' +
              filenames['role'][i])
        _file.close()

#### Publish model

### need to raise exception because
### when there is no change on the actual items
### even if you delete the old container
### it just restores with the older ID
### and throws an error

try:

    module = publish_model(modelname, publishdestination, replace=True)

    r1 = module.predict(3, 4, 2.5, 0.2)
    r2 = module.predict_proba(3, 4, 2.5, 0.2)

    print(r1, r2)

    conn.terminate()
except:

    print('Something went wrong during publication')
    sys.exit(1)
Пример #6
0
        model_repository.add_model_content(model=modelname,
                                           file=_file,
                                           name=filenames['file'][i],
                                           role=filenames['role'][i])

        print('uploaded ' + filenames['file'][i] + ' as ' +
              filenames['role'][i])
        _file.close()

#### Publish model

### need to raise exception because
### when there is no change on the actual items
### even if you delete the old container
### it just restores with the older ID
### and throws an error

try:

    publish_model(modelname, publishdestination)

except Exception as e:

    result = str(e).find('The image already exists')

    if result != -1:
        print('The image already exists, probably no change for new version')
    else:
        print('Another error, check logs')
        sys.exit(1)
Пример #7
0
# Convert the local CSV file into a Pandas DataFrame
df = pd.read_csv('/home/viya/data/Los_Angeles_house_prices.csv')

# The model input data (X) is every column in the DataFrame except the target.
# The target (y) is equal to the median home value.
target = 'medv'
X = df.drop(target, axis=1)
y = df[target]

# Fit a sci-kit learn model
model = GradientBoostingRegressor()
model.fit(X, y)

# Establish a session with Viya
with Session('dsasspre.org', 'robinswu', 'password'):
    model_name = 'GB Regression'
    project_name = 'Los Angeles Housing'

    # Register the model in SAS Model Manager
    register_model(model, model_name, project_name, input=X, force=True)

    # Publish the model to the real-time scoring engine
    module = publish_model(model_name, 'maslocal', replace=True)

    # Select the first row of training data
    x = X.iloc[0, :]

    # Call the published module and score the record
    result = module.predict(x)
    print(result)
Пример #8
0
# Load the Iris data set and convert into a Pandas data frame.
raw = datasets.load_iris()
X = pd.DataFrame(raw.data, columns=['SepalLength', 'SepalWidth',
                                    'PetalLength', 'PetalWidth'])
y = pd.DataFrame(raw.target, columns=['Species'], dtype='category')
y.Species.cat.categories = raw.target_names

# Fit a sci-kit learn model
model = LogisticRegression()
model.fit(X, y)

# Establish a session with Viya
with Session('hostname', 'username', 'password'):
    model_name = 'Iris Regression'

    # Register the model in Model Manager
    register_model(model,
                   model_name,
                   input=X,         # Use X to determine model inputs
                   project='Iris',  # Register in "Iris" project
                   force=True)      # Create project if it doesn't exist

    # Publish the model to the real-time scoring engine
    module = publish_model(model_name, 'maslocal')

    # Select the first row of training data
    x = X.iloc[0, :]

    # Call the published module and score the record
    result = module.score(**x)
    print(result)