將數據映射到正態分布?
本示例演示通過PowerTransformer使用Box-Cox和Yeo-Johnson變換將數據從各種分布映射到正態分布。
冪變換可用作在建模中需要均等和正態的問題的變換。以下是適用于六個不同概率分布的Box-Cox和Yeo-Johnwon的示例:對數正態,卡方,威布爾,高斯,均勻和雙峰。
請注意,當應用于某些數據集時,轉換成功地將數據映射到正態分布,但對其他數據集無效。這突出了在轉換之前和之后可視化數據的重要性。
還要注意,即使Box-Cox在對數正態分布和卡方分布方面的表現似乎比Yeo-Johnson好,但請記住Box-Cox不支持負值輸入。
為了進行比較,我們還添加了QuantileTransformer的輸出。只要有足夠的訓練樣本(數千個),它就可以將任意分布強加給高斯。因為它是非參數方法,所以比參數方法(Box-Cox和Yeo-Johnson)更難解釋。
在“小型”數據集(少于幾百個點)上,分位數轉換器容易過擬合。然后建議使用功率變換。
# 作者: Eric Chang <ericchang2017@u.northwestern.edu>
# Nicolas Hug <contact@nicolas-hug.com>
# 執照: BSD 3 clause
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PowerTransformer
from sklearn.preprocessing import QuantileTransformer
from sklearn.model_selection import train_test_split
print(__doc__)
N_SAMPLES = 1000
FONT_SIZE = 6
BINS = 30
rng = np.random.RandomState(304)
bc = PowerTransformer(method='box-cox')
yj = PowerTransformer(method='yeo-johnson')
# n_quantiles設置為訓練集大小,而不是默認值,以避免此示例引發警告
qt = QuantileTransformer(n_quantiles=500, output_distribution='normal',
random_state=rng)
size = (N_SAMPLES, 1)
# 對數正態分布
X_lognormal = rng.lognormal(size=size)
# 卡方分布
df = 3
X_chisq = rng.chisquare(df=df, size=size)
# 威布爾分布
a = 50
X_weibull = rng.weibull(a=a, size=size)
# 高斯分布
loc = 100
X_gaussian = rng.normal(loc=loc, size=size)
# 均勻分布
X_uniform = rng.uniform(low=0, high=1, size=size)
# 雙峰分布
loc_a, loc_b = 100, 105
X_a, X_b = rng.normal(loc=loc_a, size=size), rng.normal(loc=loc_b, size=size)
X_bimodal = np.concatenate([X_a, X_b], axis=0)
# 繪制圖像
distributions = [
('Lognormal', X_lognormal),
('Chi-squared', X_chisq),
('Weibull', X_weibull),
('Gaussian', X_gaussian),
('Uniform', X_uniform),
('Bimodal', X_bimodal)
]
colors = ['#D81B60', '#0188FF', '#FFC107',
'#B7A2FF', '#000000', '#2EC5AC']
fig, axes = plt.subplots(nrows=8, ncols=3, figsize=plt.figaspect(2))
axes = axes.flatten()
axes_idxs = [(0, 3, 6, 9), (1, 4, 7, 10), (2, 5, 8, 11), (12, 15, 18, 21),
(13, 16, 19, 22), (14, 17, 20, 23)]
axes_list = [(axes[i], axes[j], axes[k], axes[l])
for (i, j, k, l) in axes_idxs]
for distribution, color, axes in zip(distributions, colors, axes_list):
name, X = distribution
X_train, X_test = train_test_split(X, test_size=.5)
# 執行冪變換和分位數變換
X_trans_bc = bc.fit(X_train).transform(X_test)
lmbda_bc = round(bc.lambdas_[0], 2)
X_trans_yj = yj.fit(X_train).transform(X_test)
lmbda_yj = round(yj.lambdas_[0], 2)
X_trans_qt = qt.fit(X_train).transform(X_test)
ax_original, ax_bc, ax_yj, ax_qt = axes
ax_original.hist(X_train, color=color, bins=BINS)
ax_original.set_title(name, fontsize=FONT_SIZE)
ax_original.tick_params(axis='both', which='major', labelsize=FONT_SIZE)
for ax, X_trans, meth_name, lmbda in zip(
(ax_bc, ax_yj, ax_qt),
(X_trans_bc, X_trans_yj, X_trans_qt),
('Box-Cox', 'Yeo-Johnson', 'Quantile transform'),
(lmbda_bc, lmbda_yj, None)):
ax.hist(X_trans, color=color, bins=BINS)
title = 'After {}'.format(meth_name)
if lmbda is not None:
title += r'\n$\lambda$ = {}'.format(lmbda)
ax.set_title(title, fontsize=FONT_SIZE)
ax.tick_params(axis='both', which='major', labelsize=FONT_SIZE)
ax.set_xlim([-3.5, 3.5])
plt.tight_layout()
plt.show()
輸出:

腳本的總運行時間:(0分鐘2.021秒)