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.
ON TABLE HOLD FORMAT PYTHON MODEL fieldname AS app/modelname
where:
Is the fieldname in the COMPUTE command that returned the value from the machine learning function.
Is the application folder in which to save the model.
Is the name of the saved 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
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:
Is the name of the field that will contain the returned predictions from the model.
Is the application folder and saved model name.
Are the field names in the data used for running the model that match the predictor fields used to generate the 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.
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.