All Products
Search
Document Center

Platform For AI:PMML processor

Last Updated:Feb 29, 2024

Elastic Algorithm Service (EAS) provides the built-in Predictive Model Markup Language (PMML) processor to deploy PMML-formatted models as online services and run real-time inference tasks online. This topic describes how to deploy and call a PMML model service.

Background information

The PMML processor can be used to deploy algorithm models in the PMML format in EAS. Algorithm models exported from Machine Learning Designer can be directly deployed in EAS. However, algorithm models that are trained by using open source algorithm frameworks such as scikit-learn and XGBoost must be first converted into the PMML format before they can be deployed in EAS. For more information, see Model export.

For more information, visit PMML 4.4 - General Structure.

Missing value imputation

The PMML processor provides a default policy to impute missing values. If the missing value imputation (isMissing) policy is not specified for the feature columns in the PMML file, the following values are imputed by default.

Data type

Imputed value

BOOLEAN

false

DOUBLE

0.0

FLOAT

0.0

INT

0

STRING

""

Step 1: Deploy a model service

When you use the EASCMD client to deploy a PMML model service, set the processor parameter to pmml. The following code block shows an example.

{
  "name": "eas_lr_example",
  "processor": "pmml",
  "model_path": "http://examplebucket.oss-cn-shanghai.aliyuncs.com/models/lr.pmml",
  "metadata": {
    "instance": 1,
    "cpu": 1
  }
}

For more information about how to use the EASCMD client to deploy models, see Deploy model services by using EASCMD or DSW.

You can also deploy PMML model services in the Platform for AI (PAI) console. For more information, see Model service deployment by using the PAI console.

Step 2: Call the model service

You can perform the following operations to call the model service.

  1. After you deploy the PMML model service, obtain the endpoints of the service and the token used for service authentication. To obtain the information, perform the following operations: On the EAS-Online Model Services page, find the service that you want to call and click Call Inform in the Service Type column.

  2. Create a service request.

    Both the input and output of a PMML model service are JSON arrays. You can include multiple samples in a request. Each sample contains data that is presented by using key-value pairs. Example:

    [
        {
            "address": 12,
            "age": 22,
            "ed": 4,
            "marital": 3.0,
            "region": 2.0,
            "tenure": 4.0
        },
        {
            "address": 2,
            "age": 34,
            "ed": 6,
            "marital": 1.3,
            "region": 2.1,
            "tenure": 4.2
        }
    ]
    Note

    When you send the service request, you must delete the line breaks and space characters from the JSON file to speed up data transmission and improve service performance.

  3. Send a service request.

    The following methods are supported:

    Important

    If you run the curl command to send the request, the authentication token is specified in the HTTP header and is transmitted in plaintext over the Internet. If you use EAS SDK for Python to send the request, the token is used to sign the request, which improves security.

    • Run the curl command to send the request.

      PMML services support HTTP access. When you send a request, the authentication token can be directly specified in the HTTP header. Example:

      // Send the request. 
      curl -v 18284888792***.cn-shanghai.pai-eas.aliyuncs.com/api/predict/pmml_test \
            -H 'Authorization: YmE3NDkyMzdiMzNmMGM3ZmE4ZmNjZDk0M2NiMDA***' \
            -d '[{"region": 2.0, "marital":3.0,"tenure":4.0, "age":22,"address":12,"ed":4},{"region": 2.1, "marital":1.3,"tenure":4.2, "age":34,"address":2,"ed":6}]'

      Parameters:

      • 18284888792***.cn-shanghai.pai-eas.aliyuncs.com/api/predict/pmml_test: Replace the value with the endpoint that you obtained in Step 1.

      • Authorization: Configure the token that you obtained in Step 1 for service authentication.

      The following result is returned:

      [{"p_0":0.2856724025030001,"p_1":0.7143275974969999},{"p_0":0.18324957921381624,"p_1":0.8167504207861838}]
    • Use EAS SDK for Python to send the request. For more information, see SDK for Python.

      Sample code:

      #!/usr/bin/env python
      
      from eas_prediction import PredictClient
      from eas_prediction import StringRequest
      
      if __name__ == '__main__':
          # Replace the value with the endpoint and service name that you obtained in Step 1. 
          client = PredictClient('1828488879222***.cn-shanghai.pai-eas.aliyuncs.com', 'pmml_test')
          # Replace the value with the token that you obtained in Step 1 for service authentication. 
          client.set_token('YmE3NDkyMzdiMzNmMGM3ZmE4ZmNjZDk0M2NiMDA***')
          client.init()
      
      
          req = StringRequest('[{"region": 2.0, "marital":3.0,"tenure":4.0, "age":22,"address":12,"ed":4},{"region": 2.1, "marital":1.3,"tenure":4.2, "age":34,"address":2,"ed":6}]')
          for x in range(100):
              resp = client.predict(req)
              print(resp)
      

      For more information about how to use EAS SDKs for other languages, see SDKs.

References