IBM Support

How to score via deployed function on Watson Machine Learning

How To


This document introduces steps to scoring with model via function. Both model and function is used after deploy.


Cloud Pak for Data 2.5.0
Cloud Pak for Data 3.0.0


  1. Train a model. In this case, sklearn is used to create a model.
    import pandas as pd
    from sklearn.datasets import load_iris
    from sklearn.model_selection import train_test_split
    from sklearn.ensemble import RandomForestClassifier
    # data preparation
    # use sample dataset fron sklearn library
    iris = load_iris()
    df = pd.DataFrame(, columns=iris.feature_names)
    df['iris_type'] = iris.target_names[]
    X = df.drop('iris_type', axis=1)
    y = df['iris_type']
    X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=0)
    # modeling
    clf = RandomForestClassifier(max_depth=2, random_state=0, n_estimators=10)
    model =, y_train)
  2. Save model to deployment space. You can use "watson_machine_learning_client" which is a python library that allows you to work with Watson Machine Learning services.
    # iniitialize WML client
    from watson_machine_learning_client import WatsonMachineLearningAPIClient
    wml_credentials = {
        "url": "<URL>",
        "username": "<USERNAME>",
        "password" : "<PASSWORD>",
        "instance_id": "wml_local",
        "version" : "2.5.0"
    client = WatsonMachineLearningAPIClient(wml_credentials)
    # setting Deployment Space ID
    space_name = "<SPACENAME>"
    space_id = next(item for item in client.spaces.get_details()['resources'] if item['entity']["name"] == space_name)['metadata']['guid']
    # save model  to Deployment Space
        client.repository.ModelMetaNames.NAME: "iris_model1",
        client.repository.ModelMetaNames.RUNTIME_UID: "scikit-learn_0.20-py3.6",
        client.repository.ModelMetaNames.TYPE: "scikit-learn_0.20",
            "id":"iris model",
                {'name': 'sepal length (cm)', 'type': 'double'},
                {'name': 'sepal width (cm)', 'type': 'double'},
                {'name': 'petal length (cm)', 'type': 'double'},
                {'name': 'petal width (cm)', 'type': 'double'}
        client.repository.ModelMetaNames.OUTPUT_DATA_SCHEMA: {
            "id":"iris model",
            "fields": [
                {'name': 'iris_type', 'type': 'string','metadata': {'modeling_role': 'prediction'}}
    model_artifact = client.repository.store_model(model, meta_props=meta_props, training_data=X, training_target=y)
    # list models in Deployment Space
  3. Deploy the model with WML API.
    model_id = model_artifact['metadata']['guid']
    # create deployment
    meta_props = {
        client.deployments.ConfigurationMetaNames.NAME: "iris_model1_online",
        client.deployments.ConfigurationMetaNames.ONLINE: {}
    deployment_details = client.deployments.create(model_id, meta_props=meta_props)
    # deployment ID
    dep_id = deployment_details['metadata']['guid']
    # list deployment
  4. Score for test.
    # sample data for scoring (setosa)
    scoring_x = pd.DataFrame(
        data = [[5.1,3.5,1.4,0.2]],
        columns=['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']
    values = scoring_x.values.tolist()
    fields = scoring_x.columns.values.tolist()
    scoring_payload = {client.deployments.ScoringMetaNames.INPUT_DATA: [{'fields': fields, 'values': values}]}
    # scoring
    prediction = client.deployments.score(dep_id, scoring_payload)
  5. Create function for scoring. In following function, wml_credentials is defined again. This is needed to avoid authentication issue.
    # variables for function
    func_variables = {
        "space_id" : space_id,
        "dep_id" : dep_id
    # Function for scoring
    def iris_scoring_test(func_vars=func_variables):
        from watson_machine_learning_client import WatsonMachineLearningAPIClient
        wml_credentials = {
            "url": "<URL>",
            "username": "<USERNAME>",
            "password" : "<PASSWORD>",
            "instance_id": "wml_local",
            "version" : "2.5.0"
        client = WatsonMachineLearningAPIClient(wml_credentials)
        def score(scoring_payload):
                prediction = client.deployments.score(func_vars['dep_id'], scoring_payload)
                return prediction
            except Exception as e:
                return {'error': repr(e)}
        return score
  6. Store and deploy function to Deployment Space.

    # store function "iris_scoring_test"
    meta_props = {
        client.repository.FunctionMetaNames.NAME: 'iris_scoring_test_func',
        client.repository.FunctionMetaNames.RUNTIME_UID: "ai-function_0.1-py3.6",
        client.repository.FunctionMetaNames.SPACE_UID: space_id
    function_details_test = client.repository.store_function(meta_props=meta_props, function=iris_scoring_test)
    # list function
    # Deploy function "iris_scoring_test"
    function_id_test = function_details_test['metadata']['guid']
    meta_props = {
        client.deployments.ConfigurationMetaNames.NAME: "iris_scoring_test_func_online",
        client.deployments.ConfigurationMetaNames.ONLINE: {}
    function_deployment_details_test = client.deployments.create(function_id_test, meta_props=meta_props)
  7. Score via the deployed function.
    # scoring function "iris_scoring_test_func_online"
    func_dep_id = function_deployment_details_test['metadata']['guid']
    prediction = client.deployments.score(func_dep_id, scoring_payload)

Document Location


[{"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Product":{"code":"SSHGYS","label":"IBM Cloud Pak for Data"},"ARM Category":[{"code":"a8m50000000ClW9AAK","label":"Analyze->Models->Deployment"}],"ARM Case Number":"","Platform":[{"code":"PF064","label":"OpenShift 4.3"},{"code":"PF068","label":"OpenShift 3.11"}],"Version":"2.5.0;3.0.0","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Document Information

Modified date:
29 June 2020