python 概率累积分布曲线
时间: 2023-08-17 19:11:36 浏览: 156
概率累积分布曲线(Cumulative Distribution Function, CDF)是用来描述随机变量的累积分布情况的函数。在 Python 中,可以使用不同的库来计算和绘制概率累积分布曲线,比如 numpy 和 matplotlib。
下面是一个使用 numpy 和 matplotlib 绘制概率累积分布曲线的示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# 生成一组随机数据,假设服从标准正态分布
data = np.random.randn(1000)
# 计算概率累积分布
cdf = np.cumsum(np.sort(data)) / float(len(data))
# 绘制概率累积分布曲线
plt.plot(np.sort(data), cdf)
plt.xlabel('X')
plt.ylabel('CDF')
plt.title('Cumulative Distribution Function')
plt.grid(True)
plt.show()
```
上述代码中,首先使用 numpy 生成一组随机数据(这里假设服从标准正态分布),然后使用 np.sort 对数据进行排序,并使用 np.cumsum 计算累积和,再除以数据长度得到概率累积分布。最后使用 matplotlib 绘制概率累积分布曲线。
希望以上代码能够帮助你理解和绘制概率累积分布曲线。如果有其他问题,请继续提问。
相关问题
画出概率的累积分布曲线的代码
下面是 Python 中使用 matplotlib 库画出概率的累积分布曲线的示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# 生成一组随机数据
data = np.random.normal(size=1000)
# 计算概率密度函数
counts, bin_edges = np.histogram(data, bins=30, density=True)
pdf = counts / (sum(counts) * np.diff(bin_edges))
# 计算累积分布函数
cdf = np.cumsum(pdf * np.diff(bin_edges))
# 绘制累积分布曲线
plt.plot(bin_edges[1:], cdf)
plt.xlabel('X')
plt.ylabel('Cumulative Probability')
plt.title('Cumulative Distribution Function')
plt.show()
```
其中,`np.random.normal(size=1000)` 生成了一组包含 1000 个样本的正态分布随机数据。`np.histogram` 函数计算了数据的概率密度函数,`np.cumsum` 函数计算了概率密度函数的累积分布函数。最后,使用 `plt.plot` 函数将累积分布曲线绘制出来。
用python绘制泊松分布概率直方图及积累概率函数曲线
好的,以下是用 Python 绘制泊松分布概率直方图和积累概率函数曲线的代码:
```python
from scipy.stats import poisson
import matplotlib.pyplot as plt
import numpy as np
# 设置参数
mu = 5 # 泊松分布的参数 lambda
x = range(0, 20) # 横坐标从0到19
# 计算概率密度函数和累积分布函数
pmf = poisson.pmf(x, mu)
cdf = poisson.cdf(x, mu)
# 绘制概率直方图
plt.subplot(211) # 两行一列,第一个子图
plt.bar(x, pmf, align='center', alpha=0.5)
plt.title('Probability Histogram of Poisson Distribution')
plt.xlabel('Number of Events')
plt.ylabel('Probability')
# 绘制累积概率函数曲线
plt.subplot(212) # 两行一列,第二个子图
plt.plot(x, cdf, '-o')
plt.title('Cumulative Probability Function of Poisson Distribution')
plt.xlabel('Number of Events')
plt.ylabel('Cumulative Probability')
# 自适应调整子图间距
plt.subplots_adjust(hspace=0.4)
# 显示图像
plt.show()
```
运行代码后,便可以得到绘制的泊松分布概率直方图和积累概率函数曲线。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)