[英]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.histogram
中bins='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.