scipy.stats.
cumfreq#
- scipy.stats.cumfreq(a, numbins=10, defaultreallimits=None, weights=None)[source]#
返回一个累积频率直方图,使用直方图函数。
累积直方图是一种映射,它计算直到指定 bin 的所有 bin 中观察值的累积数量。
- 参数:
- aarray_like
输入数组。
- numbinsint, optional
用于直方图的 bin 的数量。默认值为 10。
- defaultreallimitstuple (lower, upper), optional
直方图范围的下限和上限值。 如果没有给出值,则使用略大于 a 中值的范围。 具体来说
(a.min() - s, a.max() + s)
,其中s = (1/2)(a.max() - a.min()) / (numbins - 1)
。- weightsarray_like, optional
a 中每个值的权重。 默认值为 None,这使得每个值的权重为 1.0
- 返回值:
- cumcountndarray
累积频率的箱值。
- lowerlimitfloat
下限实数极限
- binsizefloat
每个 bin 的宽度。
- extrapointsint
额外的点。
示例
>>> import numpy as np >>> import matplotlib.pyplot as plt >>> from scipy import stats >>> rng = np.random.default_rng() >>> x = [1, 4, 2, 1, 3, 1] >>> res = stats.cumfreq(x, numbins=4, defaultreallimits=(1.5, 5)) >>> res.cumcount array([ 1., 2., 3., 3.]) >>> res.extrapoints 3
创建一个具有 1000 个随机值的正态分布
>>> samples = stats.norm.rvs(size=1000, random_state=rng)
计算累积频率
>>> res = stats.cumfreq(samples, numbins=25)
计算 x 的值空间
>>> x = res.lowerlimit + np.linspace(0, res.binsize*res.cumcount.size, ... res.cumcount.size)
绘制直方图和累积直方图
>>> fig = plt.figure(figsize=(10, 4)) >>> ax1 = fig.add_subplot(1, 2, 1) >>> ax2 = fig.add_subplot(1, 2, 2) >>> ax1.hist(samples, bins=25) >>> ax1.set_title('Histogram') >>> ax2.bar(x, res.cumcount, width=res.binsize) >>> ax2.set_title('Cumulative histogram') >>> ax2.set_xlim([x.min(), x.max()])
>>> plt.show()