本文簡要介紹 python 語言中  numpy.histogram_bin_edges  的用法。
- 用法:- numpy.histogram_bin_edges(a, bins=10, range=None, weights=None)
- 僅計算 - histogram函數使用的 bin 邊的函數。- a: array_like
- 輸入數據。直方圖是在展平的數組上計算的。 
- bins: int 或標量序列或 str,可選
- 如果 bins 是一個 int,它定義給定範圍內的 equal-width bins 的數量(默認為 10)。如果 bins 是一個序列,它定義了 bin 邊,包括最右邊的邊,允許不均勻的 bin 寬度。 - 如果箱子是下麵列表中的一個字符串, - histogram_bin_edges將使用選擇的方法來計算最佳 bin 寬度,從而計算 bin 的數量(參見注意有關估計器的更多詳細信息)來自位於請求範圍內的數據。雖然 bin 寬度對於範圍內的實際數據是最佳的,但將計算 bin 的數量以填充整個範圍,包括空白部分。對於可視化,建議使用‘auto’ 選項。自動 bin 大小選擇不支持加權數據。- ‘auto’
- ‘sturges’ 和 ‘fd’ 估計量的最大值。提供良好的全方位性能。 
- ‘fd’(Freedman Diaconis 估計器)
- 考慮數據可變性和數據大小的穩健(對異常值有彈性)估計器。 
- ‘doane’
- Sturges 估計器的改進版本,可以更好地處理非正態數據集。 
- ‘scott’
- 考慮到數據可變性和數據大小的不太穩健的估計器。 
- ‘stone’
- 基於積分平方誤差的留一法交叉驗證估計的估計器。可以看作是斯科特規則的概括。 
- ‘rice’
- 估計器不考慮可變性,隻考慮數據大小。通常會高估所需的箱子數量。 
- ‘sturges’
- R 的默認方法,隻考慮數據大小。僅適用於高斯數據,並低估了大型非高斯數據集的 bin 數量。 
- ‘sqrt’
- 平方根(數據大小)估計器,因其速度和簡單性而被 Excel 和其他程序使用。 
 
- range: (浮點數,浮點數),可選
- bin 的下限和上限範圍。如果沒有提供,範圍很簡單 - (a.min(), a.max()).超出範圍的值將被忽略。範圍的第一個元素必須小於或等於第二個元素。範圍也會影響自動 bin 計算。雖然根據內部的實際數據計算出 bin 寬度是最佳的範圍, bin 計數將填充整個範圍,包括不包含數據的部分。
- weights: 數組,可選
- 一組權重,與 a 的形狀相同。 a 中的每個值僅將其相關權重貢獻給 bin 計數(而不是 1)。這目前沒有被任何 bin 估計器使用,但將來可能會使用。 
 
- bin_edges: dtype 浮點數組
- 要傳遞到 - histogram的邊
 
 - 參數:- 返回:- 注意:- 估計最佳箱數的方法在文獻中已有充分依據,並且受到 R 為直方圖可視化提供的選擇的啟發。請注意,箱數與 成比例是漸近最優的,這就是它出現在大多數估計器中的原因。這些隻是簡單的插件方法,為 bin 數量提供了良好的起點。在下麵的等式中, 是 bin 寬度, 是 bin 數量。所有計算 bin 計數的估計器都使用數據的 - ptp重新轉換為 bin 寬度。最終的 bin 計數從- np.round(np.ceil(range / h))獲得。最終的箱寬度通常小於下麵的估計器返回的寬度。- 妥協以獲得良好的值。對於小型數據集,通常會選擇 Sturges 值,而較大的數據集通常會默認為 FD。避免 FD 和 Sturges 分別針對小型和大型數據集的過於保守的行為。切換點通常是 。 
- 
binwidth 與四分位距 (IQR) 成正比,與 a.size 的立方根成反比。對於小數據集可能過於保守,但對於大數據集來說非常好。 IQR 對異常值非常穩健。 
- 
binwidth 與數據的標準偏差成正比,與 x.size的立方根成反比。對於小數據集可能過於保守,但對於大數據集來說非常好。標準差對異常值不是很穩健。在沒有異常值的情況下,值與 Freedman-Diaconis 估計量非常相似。
- 
bin 的數量僅與 a.size的立方根成正比。它傾向於高估 bin 的數量,並且沒有考慮數據的可變性。
- 
bin 的數量是 a.size的以 2 為底的日誌。該估計器假設數據是正態的,對於較大的非正態數據集來說過於保守。這是 R 的hist方法中的默認方法。
- 
Sturges 公式的改進版本,可為非正態數據集生成更好的估計。該估計器試圖解釋數據的偏斜。 
- 
最簡單和最快的估計器。隻考慮數據大小。 
 - ‘auto’(‘sturges’ 和 ‘fd’ 估計量的最大值):- ‘fd’(Freedman Diaconis 估計器):- ‘scott’:- ‘rice’:- ‘sturges’:- ‘doane’:- ‘sqrt’:- 例子:- >>> arr = np.array([0, 0, 0, 1, 2, 3, 3, 4, 5]) >>> np.histogram_bin_edges(arr, bins='auto', range=(0, 1)) array([0. , 0.25, 0.5 , 0.75, 1. ]) >>> np.histogram_bin_edges(arr, bins=2) array([0. , 2.5, 5. ])- 為了與直方圖保持一致,預先計算的 bin 數組未經修改地通過: - >>> np.histogram_bin_edges(arr, [1, 2]) array([1, 2])- 此函數允許計算一組 bin,並在多個直方圖中重複使用: - >>> shared_bins = np.histogram_bin_edges(arr, bins='auto') >>> shared_bins array([0., 1., 2., 3., 4., 5.])- >>> group_id = np.array([0, 1, 1, 0, 1, 1, 0, 1, 1]) >>> hist_0, _ = np.histogram(arr[group_id == 0], bins=shared_bins) >>> hist_1, _ = np.histogram(arr[group_id == 1], bins=shared_bins)- >>> hist_0; hist_1 array([1, 1, 0, 1, 0]) array([2, 0, 1, 1, 2])- 與為每個直方圖使用單獨的 bin 相比,這提供了更容易比較的結果: - >>> hist_0, bins_0 = np.histogram(arr[group_id == 0], bins='auto') >>> hist_1, bins_1 = np.histogram(arr[group_id == 1], bins='auto') >>> hist_0; hist_1 array([1, 1, 1]) array([2, 1, 1, 2]) >>> bins_0; bins_1 array([0., 1., 2., 3.]) array([0. , 1.25, 2.5 , 3.75, 5. ])
相關用法
- Python numpy histogramdd用法及代碼示例
- Python numpy histogram2d用法及代碼示例
- Python numpy histogram用法及代碼示例
- Python numpy hamming用法及代碼示例
- Python numpy hermite.hermfromroots用法及代碼示例
- Python numpy hermite_e.hermediv用法及代碼示例
- Python numpy hsplit用法及代碼示例
- Python numpy hermite.hermline用法及代碼示例
- Python numpy hermite.hermpow用法及代碼示例
- Python numpy hermite.hermx用法及代碼示例
- Python numpy hermite_e.hermefromroots用法及代碼示例
- Python numpy hermite.hermmul用法及代碼示例
- Python numpy hermite.herm2poly用法及代碼示例
- Python numpy hermite.hermsub用法及代碼示例
- Python numpy hermite_e.hermeline用法及代碼示例
- Python numpy hermite_e.hermeint用法及代碼示例
- Python numpy hermite_e.hermeadd用法及代碼示例
- Python numpy hstack用法及代碼示例
- Python numpy hermite_e.poly2herme用法及代碼示例
- Python numpy hermite.hermdiv用法及代碼示例
- Python numpy hermite_e.hermevander用法及代碼示例
- Python numpy hermite_e.hermepow用法及代碼示例
- Python numpy hermite.poly2herm用法及代碼示例
- Python numpy hermite_e.hermetrim用法及代碼示例
- Python numpy hermite_e.hermezero用法及代碼示例
注:本文由純淨天空篩選整理自numpy.org大神的英文原創作品 numpy.histogram_bin_edges。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
