RUN_MODEL and RUN_MODEL2: Running a Saved Python Model

How to:

Once you create a model using one of the machine learning functions by running it on a set of training and test data, you can save the model and run it against new data. The type of data source used to run the model can be any data source that WebFOCUS can read.

Use the RUN_MODEL function when the predictor fields in the new data source have the same field names as the predictor fields used to generate the model. Use the RUN_MODEL2 function If the field names are different. The data types and lengths must be the same in either case.

Syntax: How to Save a Python-Based Model

ON TABLE HOLD FORMAT PYTHON MODEL fieldname AS app/modelname

where:

fieldname

Is the fieldname in the COMPUTE command that returned the value from the machine learning function.

app

Is the application folder in which to save the model.

modelname

Is the name of the saved model.

Example: Saving a Python Model

The following request uses the REGRESS_RF function to return a field named predictedPrice and save it as a model named model5 in application folder app1/mymodels.

TABLE FILE imports85
PRINT price
COMPUTE predictedPrice/I5 = REGRESS_RF('{"trees":"123","test_ratio":"0.20","train_ratio":"0.80"}',
             make, numOfDoors, bodyStyle, height, horsepower, 
             peakRpm, cityMpg, highwayMpg, price);
WHERE OUTPUTLIMIT EQ 1
ON TABLE HOLD FORMAT PYTHON MODEL predictedPrice
    AS app1/mymodels/model5
END

Syntax: How to Run a Saved Python Model

Use the RUN_MODEL function if the predictor field names are the same in the model and the data.

COMPUTE fieldname/fmt = RUN_MODEL('app/modelname');

Use the RUN_MODEL2 function if the predictor field names are not the same in the model and the data.

COMPUTE fieldname/fmt = RUN_MODEL2('app/modelname', 
                pfield1, ..., pfieldn);

where:

fieldname

Is the name of the field that will contain the returned predictions from the model.

app/modelname'

Is the application folder and saved model name.

pfield1, ..., pfieldn

Are the field names in the data used for running the model that match the predictor fields used to generate the model.

Example: Running a Model Using RUN_MODEL

In the following request, the data source in the request has the same field names that were used to create the model.

TABLE FILE imports85
PRINT price COMPUTE predictedPriceFromSaved/I5 = 
      RUN_MODEL('app1/mymodels/model5');
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Partial output is shown in the following image.

Example: Running a Model Using RUN_MODEL2

In the following version of the request, the data source in the request is assumed to have field names that are different from the ones that were used to create the model.

TABLE FILE imports2
PRINT price COMPUTE predictedPriceFromSaved/I5 = 
   RUN_MODEL2('app1/mymodels/model5',
              type, doors, Style, ht, hpower, Rpm, CMpg, HMpg);
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Partial output is shown in the following image.