Edit

Share via


entropy_fl()

Applies to: ✅ Microsoft FabricAzure Data ExplorerAzure MonitorMicrosoft Sentinel

The function entropy_fl() is a user-defined function (UDF) that calculates the Shannon Entropy of multiple probability vectors.

Syntax

T | invoke entropy_fl(val_col)

Learn more about syntax conventions.

Parameters

Name Type Required Description
val_col string ✔️ The name of the column containing the probability vectors.

Function definition

You can define the function by either embedding its code as a query-defined function, or creating it as a stored function in your database, as follows:

Define the function using the following let statement. No permissions are required.

Important

A let statement can't run on its own. It must be followed by a tabular expression statement. To run a working example of time_weighted_avg_fl(), see Example.

let entropy_fl=(tbl:(*), val_col:string)
{
	tbl
    | extend _vals = column_ifexists(val_col, dynamic(null))
    | mv-apply pa=_vals to typeof(real) on 
    (summarize H=sum(-pa*log2(pa)))
    | project-away _vals
};
// Write your query to use the function here.

Example

The following example uses the invoke operator to run the function.

To use a query-defined function, invoke it after the embedded function definition.

let entropy_fl=(tbl:(*), val_col:string)
{
	tbl
    | extend _vals = column_ifexists(val_col, dynamic(null))
    | mv-apply pa=_vals to typeof(real) on 
    (summarize H=sum(-pa*log2(pa)))
    | project-away _vals
};
let probs = datatable(p:dynamic) [
    dynamic([0.2, 0.4, 0.3, 0.1]),
    dynamic([0.5, 0.5])
];
probs
| invoke entropy_fl('p')

Output

p H
[0.2, 0.4, 0.3, 0.1] 1.84643934467102
[0.5, 0.5] 1