使用 fancyimpute 对测试集进行插补

Imputation on the test set with fancyimpute

python 程序包 Fancyimpute 提供了几种方法来填补 Python 中的缺失值。该文档提供了示例,例如:

# X is the complete data matrix
# X_incomplete has the same values as X except a subset have been replace with NaN

# Model each feature with missing values as a function of other features, and
# use that estimate for imputation.
X_filled_ii = IterativeImputer().fit_transform(X_incomplete)

将插补方法应用于数据集时效果很好X。但是,如果需要 training/test 拆分怎么办?一次

X_train_filled = IterativeImputer().fit_transform(X_train_incomplete)

被调用,我如何估算测试集并创建 X_test_filled?需要使用训练集中的信息来估算测试集。我猜 IterativeImputer() 应该 returns 和可以适合 X_test_incomplete 的对象。那可能吗?

请注意,对整个数据集进行插补然后拆分为训练集和测试集是不正确

这个包看起来像是在模仿 scikit-learn 的 API。在查看源代码后,它看起来确实有一个 transform 方法。

my_imputer = IterativeImputer()
X_trained_filled = my_imputer.fit_transform(X_train_incomplete)

# now transform test
X_test_filled = my_imputer.transform(X_test)

插补器将应用它从训练集中学到的相同插补。