numpy直方图中的自定义范围
Custom Range in numpy histogram
正在尝试使用 numpy 输出直方图数据;
NUMBER_OF_PRICE_BRACKETS = 8
HISTOGRAM_EDGE_RANGE = (0, 1_000_000)
hist, bin_edges = numpy.histogram(price_list, bins=NUMBER_OF_PRICE_BRACKETS, range=HISTOGRAM_EDGE_RANGE)
我使用上面的代码得到以下输出
hist: [0, 6, 6, 0, 0, 0, 0, 0],
bin_edges: [0.0, 125000.0, 250000.0, 375000.0, 500000.0, 625000.0, 750000.0, 875000.0, 1000000.0]
边缘是自动计算的。是否有任何选项可以像下面的示例输出那样强制创建边缘?
hist: [0, 6, 6, 0, 0, 0, 0, 0]
bin_edges: [0.0, 100000.0, 150000.0, 300000.0, 450000.0, 600000.0, 750000.0, 900000.0, 1000000.0]
也许使用像
这样的范围选项
range=(0, 1_000_000, 150)
仅将 bins
参数定义为整数值会自动选择值范围。但是,您也可以将 list/numpy 数组作为 bins
的参数,例如,
hist, bin_edges = np.histogram(price_list, bins=np.linspace(0, 1000000, 10) )
结果
bin_edges = [0., 111111.11111111, 222222.22222222, 333333.33333333, 444444.44444444, 555555.55555556, 666666.66666667, 777777.77777778, 888888.88888889, 1000000. ]
请注意,我没有在那里使用 range
参数。
您有两个选择,因为 histogram
将始终将您的数据分成等距的 bin,就好像
np.linspace(*HISTOGRAM_EDGE_RANGE, NUMBER_OF_PRICE_BRACKETS + 1)
选项 1: 手动提供不均匀的垃圾箱:
HISTOGRAM_EDGES = np.array([
0, 150_000, 300_000, 450_000, 600_000,
750_000, 900_000, 1_000_000])
hist, bin_edges = numpy.histogram(price_list, bins=HISTOGRAM_EDGES)
选项 2: 调整您的范围,使其均匀地分成您想要的 bin 数量:
NUMBER_OF_PRICE_BRACKETS = 8
HISTOGRAM_EDGE_RANGE = (0, 1_050_000)
hist, bin_edges = numpy.histogram(price_list, bins=NUMBER_OF_PRICE_BRACKETS, range=HISTOGRAM_EDGE_RANGE)
正在尝试使用 numpy 输出直方图数据;
NUMBER_OF_PRICE_BRACKETS = 8
HISTOGRAM_EDGE_RANGE = (0, 1_000_000)
hist, bin_edges = numpy.histogram(price_list, bins=NUMBER_OF_PRICE_BRACKETS, range=HISTOGRAM_EDGE_RANGE)
我使用上面的代码得到以下输出
hist: [0, 6, 6, 0, 0, 0, 0, 0],
bin_edges: [0.0, 125000.0, 250000.0, 375000.0, 500000.0, 625000.0, 750000.0, 875000.0, 1000000.0]
边缘是自动计算的。是否有任何选项可以像下面的示例输出那样强制创建边缘?
hist: [0, 6, 6, 0, 0, 0, 0, 0]
bin_edges: [0.0, 100000.0, 150000.0, 300000.0, 450000.0, 600000.0, 750000.0, 900000.0, 1000000.0]
也许使用像
这样的范围选项range=(0, 1_000_000, 150)
仅将 bins
参数定义为整数值会自动选择值范围。但是,您也可以将 list/numpy 数组作为 bins
的参数,例如,
hist, bin_edges = np.histogram(price_list, bins=np.linspace(0, 1000000, 10) )
结果
bin_edges = [0., 111111.11111111, 222222.22222222, 333333.33333333, 444444.44444444, 555555.55555556, 666666.66666667, 777777.77777778, 888888.88888889, 1000000. ]
请注意,我没有在那里使用 range
参数。
您有两个选择,因为 histogram
将始终将您的数据分成等距的 bin,就好像
np.linspace(*HISTOGRAM_EDGE_RANGE, NUMBER_OF_PRICE_BRACKETS + 1)
选项 1: 手动提供不均匀的垃圾箱:
HISTOGRAM_EDGES = np.array([
0, 150_000, 300_000, 450_000, 600_000,
750_000, 900_000, 1_000_000])
hist, bin_edges = numpy.histogram(price_list, bins=HISTOGRAM_EDGES)
选项 2: 调整您的范围,使其均匀地分成您想要的 bin 数量:
NUMBER_OF_PRICE_BRACKETS = 8
HISTOGRAM_EDGE_RANGE = (0, 1_050_000)
hist, bin_edges = numpy.histogram(price_list, bins=NUMBER_OF_PRICE_BRACKETS, range=HISTOGRAM_EDGE_RANGE)