Skip to content

Commit bdab07d

Browse files
Merge pull request #2729 from MicrosoftDocs/main638931853762435195sync_temp
For protected branch, push strategy should use PR and merge to target branch method to work around git push error
2 parents 8d73b68 + 3ce190b commit bdab07d

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 |

data-explorer/kusto/functions-library/functions-library.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ The following section contains common [PromQL](https://prometheus.io/docs/promet
101101
| [bartlett_test_fl()](bartlett-test-fl.md) | Perform the Bartlett test. |
102102
| [binomial_test_fl()](binomial-test-fl.md) | Perform the binomial test. |
103103
| [comb_fl()](comb-fl.md) | Calculate *C(n, k)*, the number of combinations for selection of k items out of n. |
104+
| [entropy_fl()](entropy-fl.md) | Calculate Shannon Entropy of multiple probability vectors. |
104105
| [factorial_fl()](factorial-fl.md) | Calculate *n!*, the factorial of n. |
105106
| [ks_test_fl()](ks-test-fl.md) | Perform a Kolmogorov Smirnov test. |
106107
| [levene_test_fl()](levene-test-fl.md) | Perform a Levene test. |

data-explorer/kusto/functions-library/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ items:
2626
- name: detect_anomalous_spike_fl()
2727
displayName: functions library, security, cybersecurity, anomaly detection, spike, cyber, detect anomalous spike
2828
href: detect-anomalous-spike-fl.md
29+
- name: entropy_fl()
30+
displayName: functions library, information theory, Shannon entropy
31+
href: entropy-fl.md
2932
- name: factorial_fl()
3033
displayName: functions library, statistics
3134
href: factorial-fl.md

0 commit comments

Comments
 (0)