在机器学习中,处理缺失数据是一个常见的问题。IterativeImputer
类提供了一种灵活的方式来通过轮流将每个变量作为输出来进行循环回归,以填补数据中的空白。本文将探讨几种不同的估计器,用于与IterativeImputer
一起进行特征缺失值的插补。
比较了几种不同的估计器,以确定哪一种最适合与IterativeImputer
一起使用。这些估计器包括:
特别值得注意的是,IterativeImputer
能够模仿missForest的行为,missForest是R语言中一个流行的插补包。
目标是比较不同的估计器,看看哪一种最适合IterativeImputer
。使用贝叶斯岭回归估计器在加利福尼亚房屋数据集上进行实验,每行随机移除一个值。对于这种特定的缺失值模式,发现贝叶斯岭回归和随机森林回归给出了最好的结果。
需要注意的是,一些估计器,如HistGradientBoostingRegressor
,可以原生处理缺失特征,并且通常推荐使用,而不是构建包含复杂和昂贵缺失值插补策略的管道。
以下是使用不同插补方法对加利福尼亚房屋数据集进行回归分析的代码实现。代码中使用了matplotlib、numpy、pandas和scikit-learn库。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer, SimpleImputer
from sklearn.kernel_approximation import Nystroem
from sklearn.linear_model import BayesianRidge, Ridge
from sklearn.model_selection import cross_val_score
from sklearn.neighbors import KNeighborsRegressor
from sklearn.pipeline import make_pipeline
N_SPLITS = 5
rng = np.random.RandomState(0)
X_full, y_full = fetch_california_housing(return_X_y=True)
X_full = X_full[::10]
y_full = y_full[::10]
n_samples, n_features = X_full.shape
# 完整数据集的评分估计
br_estimator = BayesianRidge()
score_full_data = pd.DataFrame(cross_val_score(br_estimator, X_full, y_full, scoring="neg_mean_squared_error", cv=N_SPLITS), columns=["Full Data"])
# 每行添加一个缺失值
X_missing = X_full.copy()
y_missing = y_full
missing_samples = np.arange(n_samples)
missing_features = rng.choice(n_features, n_samples, replace=True)
X_missing[missing_samples, missing_features] = np.nan
# 使用简单插补和迭代插补的评分估计
score_simple_imputer = pd.DataFrame()
for strategy in ("mean", "median"):
estimator = make_pipeline(SimpleImputer(missing_values=np.nan, strategy=strategy), br_estimator)
score_simple_imputer[strategy] = cross_val_score(estimator, X_missing, y_missing, scoring="neg_mean_squared_error", cv=N_SPLITS)
estimators = [
BayesianRidge(),
RandomForestRegressor(n_estimators=4, max_depth=10, bootstrap=True, max_samples=0.5, n_jobs=2, random_state=0),
make_pipeline(Nystroem(kernel="polynomial", degree=2, random_state=0), Ridge(alpha=1e3)),
KNeighborsRegressor(n_neighbors=15),
]
score_iterative_imputer = pd.DataFrame()
tolerances = (1e-3, 1e-1, 1e-1, 1e-2)
for impute_estimator, tol in zip(estimators, tolerances):
estimator = make_pipeline(IterativeImputer(random_state=0, estimator=impute_estimator, max_iter=25, tol=tol), br_estimator)
score_iterative_imputer[impute_estimator.__class__.__name__] = cross_val_score(estimator, X_missing, y_missing, scoring="neg_mean_squared_error", cv=N_SPLITS)
scores = pd.concat([
score_full_data,
score_simple_imputer,
score_iterative_imputer
], keys=["Original", "SimpleImputer", "IterativeImputer"], axis=1)
# 绘制加利福尼亚房屋结果
fig, ax = plt.subplots(figsize=(13, 6))
means = -scores.mean()
errors = scores.std()
means.plot.barh(xerr=errors, ax=ax)
ax.set_title("California Housing Regression with Different Imputation Methods")
ax.set_xlabel("MSE (smaller is better)")
ax.set_yticks(np.arange(means.shape[0]))
ax.set_yticklabels([" w/ ".join(label) for label in means.index.tolist()])
plt.tight_layout(pad=1)
plt.show()
通过上述代码,可以比较不同插补方法在加利福尼亚房屋数据集上的表现。结果显示,贝叶斯岭回归和随机森林回归在这种特定的缺失值模式下表现最佳。