|
| 1 | +--- |
| 2 | +title: entropy_fl() |
| 3 | +description: This article describes entropy_fl() user-defined function. |
| 4 | +ms.reviewer: adieldar |
| 5 | +ms.topic: reference |
| 6 | +ms.date: 09/10/2025 |
| 7 | +monikerRange: "microsoft-fabric || azure-data-explorer || azure-monitor || microsoft-sentinel" |
| 8 | +--- |
| 9 | +# entropy_fl() |
| 10 | + |
| 11 | +>[!INCLUDE [applies](../includes/applies-to-version/applies.md)] [!INCLUDE [fabric](../includes/applies-to-version/fabric.md)] [!INCLUDE [azure-data-explorer](../includes/applies-to-version/azure-data-explorer.md)] [!INCLUDE [monitor](../includes/applies-to-version/monitor.md)] [!INCLUDE [sentinel](../includes/applies-to-version/sentinel.md)] |
| 12 | +
|
| 13 | +The function `entropy_fl()` is a [user-defined function (UDF)](../query/functions/user-defined-functions.md) that calculates the [Shannon Entropy](https://en.wikipedia.org/wiki/Entropy_(information_theory)) of multiple probability vectors. |
| 14 | + |
| 15 | +## Syntax |
| 16 | + |
| 17 | +`T | invoke entropy_fl(`*val_col*`)` |
| 18 | + |
| 19 | +[!INCLUDE [syntax-conventions-note](../includes/syntax-conventions-note.md)] |
| 20 | + |
| 21 | +## Parameters |
| 22 | + |
| 23 | +| Name | Type | Required | Description | |
| 24 | +|--|--|--|--| |
| 25 | +| *val_col* | `string` | :heavy_check_mark: | The name of the column containing the probability vectors.| |
| 26 | + |
| 27 | +## Function definition |
| 28 | + |
| 29 | +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: |
| 30 | + |
| 31 | +### [Query-defined](#tab/query-defined) |
| 32 | + |
| 33 | +Define the function using the following [let statement](../query/let-statement.md). No permissions are required. |
| 34 | + |
| 35 | +> [!IMPORTANT] |
| 36 | +> A [let statement](../query/let-statement.md) can't run on its own. It must be followed by a [tabular expression statement](../query/tabular-expression-statements.md). To run a working example of `time_weighted_avg_fl()`, see [Example](#example). |
| 37 | +
|
| 38 | +```kusto |
| 39 | +let entropy_fl=(tbl:(*), val_col:string) |
| 40 | +{ |
| 41 | + tbl |
| 42 | + | extend _vals = column_ifexists(val_col, dynamic(null)) |
| 43 | + | mv-apply pa=_vals to typeof(real) on |
| 44 | + (summarize H=sum(-pa*log2(pa))) |
| 45 | + | project-away _vals |
| 46 | +}; |
| 47 | +// Write your query to use the function here. |
| 48 | +``` |
| 49 | + |
| 50 | +### [Stored](#tab/stored) |
| 51 | + |
| 52 | +Define the stored function once using the following [`.create function`](../management/create-function.md). [Database User permissions](../access-control/role-based-access-control.md) are required. |
| 53 | + |
| 54 | +> [!IMPORTANT] |
| 55 | +> You must run this code to create the function before you can use the function as shown in the [Example](#example). |
| 56 | +
|
| 57 | +```kusto |
| 58 | +.create-or-alter function with (folder = "Packages\\Stats", docstring = "Shannon entropy") |
| 59 | +entropy_fl(tbl:(*), val_col:string) |
| 60 | +{ |
| 61 | + tbl |
| 62 | + | extend _vals = column_ifexists(val_col, dynamic(null)) |
| 63 | + | mv-apply pa=_vals to typeof(real) on |
| 64 | + (summarize H=sum(-pa*log2(pa))) |
| 65 | + | project-away _vals |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## Example |
| 72 | + |
| 73 | +The following example uses the [invoke operator](../query/invoke-operator.md) to run the function. |
| 74 | + |
| 75 | +### [Query-defined](#tab/query-defined) |
| 76 | + |
| 77 | +To use a query-defined function, invoke it after the embedded function definition. |
| 78 | + |
| 79 | +```kusto |
| 80 | +let entropy_fl=(tbl:(*), val_col:string) |
| 81 | +{ |
| 82 | + tbl |
| 83 | + | extend _vals = column_ifexists(val_col, dynamic(null)) |
| 84 | + | mv-apply pa=_vals to typeof(real) on |
| 85 | + (summarize H=sum(-pa*log2(pa))) |
| 86 | + | project-away _vals |
| 87 | +}; |
| 88 | +let probs = datatable(p:dynamic) [ |
| 89 | + dynamic([0.2, 0.4, 0.3, 0.1]), |
| 90 | + dynamic([0.5, 0.5]) |
| 91 | +]; |
| 92 | +probs |
| 93 | +| invoke entropy_fl('p') |
| 94 | +``` |
| 95 | + |
| 96 | +### [Stored](#tab/stored) |
| 97 | + |
| 98 | +> [!IMPORTANT] |
| 99 | +> For this example to run successfully, you must first run the [Function definition](#function-definition) code to store the function. |
| 100 | +
|
| 101 | +```kusto |
| 102 | +let probs = datatable(p:dynamic) [ |
| 103 | + dynamic([0.2, 0.4, 0.3, 0.1]), |
| 104 | + dynamic([0.5, 0.5]) |
| 105 | +]; |
| 106 | +probs |
| 107 | +| invoke entropy_fl('p') |
| 108 | +``` |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +**Output** |
| 113 | + |
| 114 | +| p | H | |
| 115 | +|---|---| |
| 116 | +[0.2, 0.4, 0.3, 0.1] | 1.84643934467102 | |
| 117 | +[0.5, 0.5] | 1 | |
0 commit comments