繁体   English   中英

如何在numpy.histogram中选择箱数?

[英]How to choose number of bins in numpy.histogram?

如果我使用matplotlib的直方图,则可以选择箱数。 但是,如何选择numpy直方图中的垃圾箱数量?

import matplotlib.pyplot as plt
import numpy as np
array = [1,3,4,4,8,9,10,12]

range = int((max(array)) - min(array))+1
x, bins, patch = plt.hist(array, bins=range)

在这种情况下,范围=箱数=(12-1)+1 = 12

因此,结果是x = [1. 0. 1. 2. 0. 0. 0. 1. 1. 1. 1. 0. 1.]

但是numpy的结果是

hist, bin_edges = np.histogram(array, density=False)

numpy = [1 1 2 0 0 0 1 1 1 1] numpy_bin = [1. 2.1 3.2 4.3 5.4 6.5 7.6 8.7 9.8 10.9 12.]

使用numpy时,如何选择箱数(= int((max(array))-min(array))+ 1)

我想要像matplotlib一样的结果

Matplotlib使用numpys直方图,通过仓的数量简单地增加bins=bin_range作为关键字参数到np.histogram

hist, edges = np.histogram(array, bins=bin_range, density=False)

如果range是整数, bin_range得到等大小的bin的bin_range数量。 np.histogrambins='auto'的默认值为bins='auto' ,它使用一种算法来确定bin的数量。 有关更多信息, 访问: https : //docs.scipy.org/doc/numpy-1.13.0/reference/genic/numpy.histogram.html

array = [1,3,4,4,8,9,10,12]
bin_range = int((max(array)) - min(array))+1
x, bins, patch = plt.hist(array, bins=bin_range)

x
array([ 1.,  0.,  1.,  2.,  0.,  0.,  0.,  1.,  1.,  1.,  0.,  1.])

hist, edges = np.histogram(array, bins=bin_range)

hist
array([1, 0, 1, 2, 0, 0, 0, 1, 1, 1, 0, 1], dtype=int64)

bins == edges
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True], dtype=bool)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM