python 方差 信息熵
时间: 2024-01-24 09:16:08 浏览: 126
Python计算信息熵实例
5星 · 资源好评率100%
方差和信息熵是统计学和信息论中常用的概念。下面是Python中计算方差和信息熵的示例:
1. 计算方差:
```python
import numpy as np
data = [1, 2, 3, 4, 5]
variance = np.var(data)
print("方差:", variance)
```
2. 计算信息熵:
```python
import math
probabilities = [0.2, 0.3, 0.1, 0.4]
entropy = -sum(p * math.log2(p) for p in probabilities)
print("信息熵:", entropy)
```
阅读全文