The function predict_onnx_fl() predicts using an existing trained machine learning model. This model has been converted to ONNX format, serialized to string, and saved in a standard Azure Data Explorer table.



T | invoke predict_onnx_fl(models_tbl, model_name, features_cols, pred_col)


  • models_tbl: The name of the table containing all serialized models. This table must contain the following columns:
    • name: the model name
    • timestamp: time of model training
    • model: string representation of the serialized model
  • model_name: The name of the specific model to use.
  • features_cols: Dynamic array containing the names of the features columns that are used by the model for prediction.
  • pred_col: The name of the column that stores the predictions.


predict_onnx_fl() is a user-defined tabular function to be applied using the invoke operator. You can either embed its code in your query, or install it in your database. There are two usage options: ad hoc and persistent usage. See the below tabs for examples.

For ad hoc usage, embed the code using the let statement. No permission is required.

let predict_onnx_fl=(samples:(*), models_tbl:(name:string, timestamp:datetime, model:string), model_name:string, features_cols:dynamic, pred_col:string)
    let model_str = toscalar(models_tbl | where name == model_name | top 1 by timestamp desc | project model);
    let kwargs = pack('smodel', model_str, 'features_cols', features_cols, 'pred_col', pred_col);
    let code =
    'import binascii\n'
    'smodel = kargs["smodel"]\n'
    'features_cols = kargs["features_cols"]\n'
    'pred_col = kargs["pred_col"]\n'
    'bmodel = binascii.unhexlify(smodel)\n'
    'features_cols = kargs["features_cols"]\n'
    'pred_col = kargs["pred_col"]\n'
    'import onnxruntime as rt\n'
    'sess = rt.InferenceSession(bmodel)\n'
    'input_name = sess.get_inputs()[0].name\n'
    'label_name = sess.get_outputs()[0].name\n'
    'df1 = df[features_cols]\n'
    'predictions =[label_name], {input_name: df1.values.astype(np.float32)})[0]\n'
    'result = df\n'
    'result[pred_col] = pd.DataFrame(predictions, columns=[pred_col])'
    samples | evaluate python(typeof(*), code, kwargs)
// Predicts room occupancy from sensors measurements, and calculates the confusion matrix
// Occupancy Detection is an open dataset from UCI Repository at
// It contains experimental data for binary classification of room occupancy from Temperature,Humidity,Light and CO2.
// Ground-truth labels were obtained from time stamped pictures that were taken every minute
| where Test == 1
| extend pred_Occupancy=bool(0)
| invoke predict_onnx_fl(ML_Models, 'ONNX-Occupancy', pack_array('Temperature', 'Humidity', 'Light', 'CO2', 'HumidityRatio'), 'pred_Occupancy')
| summarize n=count() by Occupancy, pred_Occupancy

Confusion matrix:

Occupancy	pred_Occupancy	n
TRUE	    TRUE	        3006
FALSE	    TRUE	        112
TRUE	    FALSE	        15
FALSE	    FALSE	        9284