【发布时间】:2021-09-21 07:48:56
【问题描述】:
我有一个训练和测试数据作为交叉验证的一部分。当我使用标准化训练数据时 Yeo Johnson 变换,为了防止数据泄漏,我计划将 lambda 从训练数据归一化中保存,并将其用于测试数据归一化。 我写了小sn-p来测试如下:
import seaborn as sns
from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
# fig = plt.figure(figsize=(10,10), dpi=600)
ax1 = fig.add_subplot(421)
xTr = stats.loggamma.rvs(5, size=500) + 5
prob = stats.probplot(xTr, dist=stats.norm, plot=ax1)
ax1.set_xlabel('')
ax1.set_title('Probplot:Train')
ax2 = fig.add_subplot(422)
sns.distplot(xTr, color="skyblue")
ax2.set_title('Distribution of Training Data')
ax3 = fig.add_subplot(423)
xt_scipy, lmbda = stats.yeojohnson(xTr)
prob = stats.probplot(xt_scipy, dist=stats.norm, plot=ax3)
ax3.set_title('Probplot:Yeo-Johnson:Scipy on train')
ax4 = fig.add_subplot(424)
sns.distplot(xt_scipy, color="skyblue")
ax4.set_title('Distribution of Transformed Train Data')
ax5 = fig.add_subplot(425)
xTst = stats.loggamma.rvs(10, size=500) + 5
# xTst = stats.loglaplace.rvs(7, size=500)
prob = stats.probplot(xTst, dist=stats.norm, plot=ax5)
ax5.set_xlabel('')
ax5.set_title('Probplot:Test')
ax6 = fig.add_subplot(426)
sns.distplot(xTst, color="skyblue")
ax6.set_title('Distribution of Test Data')
ax7 = fig.add_subplot(427)
xtst_scipy = stats.yeojohnson(xTst, lmbda=lmbda)
prob = stats.probplot(xtst_scipy, dist=stats.norm, plot=ax7)
ax7.set_title('Probplot:Yeo-Johnson:Scipy on Test')
ax8 = fig.add_subplot(428)
sns.distplot(xtst_scipy, color="skyblue")
ax8.set_title('Distribution of Transformed Test Data')
plt.tight_layout(h_pad=0.9, w_pad=0.9)
plt.show()
- 是否使用 Scipy 正确完成了测试数据的标准化步骤,如我的代码所示?
- 如何在 SKlearn 中使用先前从训练数据中计算出的 lambda 来完成此操作?我问的原因是 Yeo Johnson 的 Sklearn PowerTransformer 和 fit_transform 不允许传递预先计算的 lambda。
谢谢
赛迪
【问题讨论】:
标签: scikit-learn scipy normalization