python - 计算特定 bin 中的元素数
问题描述
我很好奇是否可以计算直方图中特定 bin 的元素数量,即 0-10 范围内的所有元素
你会怎么做?
例如 plt.hist(data, bins=[0, 10, 20, 30, 40, 50, 100]) 是否可以计算数据集中进入 bin 0-10 的所有元素
解决方案
Matplotlib 直方图返回每个 bin 的计数:
import matplotlib.pyplot as plt
import numpy as np
x = np.random.uniform(0, 100, 1000)
counts, edges, plot = plt.hist(x, bins=[0, 10, 20, 50, 100])
print(counts)
print(counts[0]) # first bin
推荐阅读
- xslt - XSLT:谓词内 key() 的行为
- python - 重采样错误:ValueError:无法使用方法或限制重新索引非唯一索引
- java - 自动生成代码以动态创建 Java 对象(反射)
- python - SQLite3 在第二个查询中绑定的数量不正确
- python-3.x - 是否有一个名为 document_base 的包?
- python - Pip3 和 python 不同步 | ModuleNotFoundError:没有名为“numpy”的模块`
- python - 嵌套python类(或django外键)的正确方法。*概念/工程问题*
- c# - 在后面的代码中创建文本框后使用文本框
- java - javax.xml.bind.JAXBException 与 Java 11
- c# - 对上下文的异步请求引发异常