簡體   English   中英

seaborn distplot的權重選項?

[英]weights option for seaborn distplot?

我想在 seaborn distplot 中有一個權重選項,類似於 numpy 直方圖中的選項。 如果沒有此選項,唯一的替代方法是將權重應用於輸入數組,這可能會導致不切實際的大小(和時間)。

您可以通過使用hist_kws參數將權重傳遞給底層 matplotlib 的直方圖函數來提供權重,如下所示:

sns.distplot(..., hist_kws={'weights': your weights array}, ...)

但請注意,權重將僅傳遞給基礎直方圖; kde 和distplot的擬合函數都不會受到影響。

正如@vlasisla 在他們的回答中已經提到的,應該通過關鍵字參數hist_kws提供權重,以便將它們傳遞給 mathpolotlib 的hist函數。 但是,除非同時禁用kde (內核密度估計)選項,否則這不會產生任何影響。 這段代碼實際上會產生預期的效果:

sns.distplot(x, hist_kws={'weights': x_weights}, kde=False)

要理解為什么權重和 kde 都不允許,讓我們考慮以下示例,其中x_weights計算為x_weights = np.ones_like(x) / len(x)以便所有 bin 的高度總和為 1:

# generate 1000 samples from a normal distribution
np.random.seed(8362) 
x = np.random.normal(size=1000)

# calculate weights
x_weights = np.ones_like(x) / len(x)

# figure 1 - use weights
sns.distplot(x, hist_kws={'weights': x_weights}, kde=False)
# figure 2 - default plot with kde
sns.distplot(x)

圖 1. 使用帶有權重的 dist 而不是 KDE圖 2. 使用帶有默認參數的 dist

在圖 1 中,我們提供了帶有權重的dist函數,因此在該圖中,所有 bin 的高度總和為 1 在圖 2 中, dist的默認行為已啟用,因此KDE 函數下的區域總和為 1,並且 bins 的高度相應地標准化。 現在很容易看出,在提供權重時繪制 KDE 確實沒有多大意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM