On this page

Prediction for trained model

Do prediction for a trained model

Method : POST
URI : /ml/pred/<pred_type>
Body : json doc for prediction

<pred_type> has two possible values
1 = Async, should be used when predicting for lots of data in a file
2 = sync, should be used when predicting for a single data line

Structure of the body

{
"schema-name":"<schema-name>",
"model_name":"<model_name>",
"data_type":"<1 for file, 2 for single data>",
"attr_type":"<1 for number only, 2 for string only, 3 for hybrid>", // user hybrid when in doubt
"algo_type":"<SVM | KMEANS etc ..>",
"input_format":"<CSV | SVM | JSON | TEXT | KV>", // format of input data (data we wish to predict on)
"expected_format":"<CSV | SVM | JSON >", // format of data expected by algo/model
"data":"data"
}

Now for the model trained in previous stage, let’s do the prediction
Example

curl -H "Content-Type: application/json" -d'{"schema-name":"website","model_name":"sales_model", "data_type":2,"attr_type":3,"algo_type":"SVM","input_format":"CSV","expected_format":"SVM","data":"v1,p2,c2,pg2,2"}' -X POST http://192.168.1.105:18080/ml/pred/2

Response

{
"predict_labels": 49.239650444325,
"mean_sq_err": 2424.543175879315,
"sq_cor_coef": 1,
"errorcode": 0
}

The predicted output is 49.23, which is the predicted sales for this row

Now let’s add an event into the “visitor” stream, since the model is available, the stream processing engine should be able to use the model to predict the value and add into the stream as defined

curl -H "Content-Type: application/json" -d'{"vid":"v1","prod":"p2","catid":"c2","pgid":"pg2","price":54.50,"items":1}' -X POST http://192.168.1.105:18080/stream/website/visitor

Now fetch the row from the stream and check

curl -H "Content-Type: application/json" -d'{"sql":"select * from website.visitor limit 1"}' -X POST http://192.168.1.105:18080/db/mydb/query
{
"rows": [
{
"k": 1648557066923805,
"v": "{\"vid\":\"v1\",\"prod\":\"p2\",\"catid\":\"c2\",\"pgid\":\"pg2\",\"price\":54.5,\"items\":1,\"_pk\":1648557066923805,\"pred_sales\":49.275022189464,\"_v\":1}"
}
],
"levk": 1648557066923805,
"num_items": 1,
"more_data_to_come": 1,
"switch_done": 1
}

As you see now, “pred_sales”:49.275 is added automatically by the db

Was this article helpful to you? Yes No