IterativeImputer#
- class sklearn.impute.IterativeImputer(estimator=None, *, missing_values=nan, sample_posterior=False, max_iter=10, tol=0.001, n_nearest_features=None, initial_strategy='mean', fill_value=None, imputation_order='ascending', skip_complete=False, min_value=-inf, max_value=inf, verbose=0, random_state=None, add_indicator=False, keep_empty_features=False)[源代码]#
多元估算器,从所有其他特征中估计每个特征。
一种通过以循环方式将每个具有缺失值的特征建模为其他特征的函数来估算缺失值的策略。
阅读更多的 User Guide .
Added in version 0.21.
备注
这个估计器仍然是 experimental 目前:预测和API可能会在没有任何弃用周期的情况下发生变化。要使用它,您需要显式导入
enable_iterative_imputer
>>> # explicitly require this experimental feature >>> from sklearn.experimental import enable_iterative_imputer # noqa >>> # now you can import normally from sklearn.impute >>> from sklearn.impute import IterativeImputer
- 参数:
- estimator估计器对象,默认=BayesianRidge()
循环插补的每个步骤使用的估计量。如果
sample_posterior=True
,估计器必须支持return_std
在其predict
法- missing_valuesint或NP.nan,默认=NP.nan
缺失值的占位符。的所有匹配项
missing_values
将被归因。对于具有缺失值的可空整数据类型的pandas的数据包,missing_values
应设置为np.nan
,自从pd.NA
将转换为np.nan
.- sample_posterior布尔,默认=假
是否对每次插补的匹配估计量的(高斯)预测后验进行抽样。估算者必须支持
return_std
在其predict
方法(如果设置为True
.设置为True
如果使用IterativeImputer
用于多次估算。- max_iterint,默认值=10
在返回最后一轮计算的插补之前要执行的最大插补轮数。一轮是对每个缺失值的特征的单一插补。满足一次停止标准
max(abs(X_t - X_{t-1}))/max(abs(X[known_vals])) < tol
,在哪里X_t
是X
在迭代t
.请注意,只有在以下情况下才适用提前停止sample_posterior=False
.- tol浮点数,默认值= 1 e-3
停止条件的容忍度。
- n_nearest_featuresint,默认=无
用于估计每个特征列的缺失值的其他特征的数量。使用每个特征对之间的绝对相关系数(初始插补后)来测量特征之间的接近度。为了确保在整个估算过程中覆盖特征,邻近特征不一定是最近的,而是以与每个估算目标特征的相关性成比例的概率绘制。当功能数量庞大时,可以提供显着的加速。如果
None
,将使用所有功能。- initial_strategy'mean','median','most_frequent','constant'}, 默认值='平均值'
使用哪种策略来初始化缺失的值。相同
strategy
参数SimpleImputer
.- fill_value字符串或数字值,默认=无
当
strategy="constant"
,fill_value
用于替换所有出现的missing_values。对于字符串或对象数据类型,fill_value
一定是一根绳子。如果None
,fill_value
当为字符串或对象数据类型插补数字数据和“missing_Value”时,将为0。Added in version 1.3.
- imputation_order“上升”、“下降”、“罗马”、“阿拉伯”, '随机'},默认='上升'
特征的估算顺序。可能的值:
'ascending'
:从缺失值最少的要素到缺失值最多的要素。'descending'
:从缺失值最多到最少的要素。'roman'
:从左到右。'arabic'
:从右到左。'random'
: A random order for each round.
- skip_complete布尔,默认=假
如果
True
则在以下期间具有缺失值的要素transform
在此期间没有任何缺失值fit
将仅使用初始插补方法进行插补。设置为True
如果您有许多要素两者都没有缺失值fit
和transform
是时候保存计算了。- min_value形状(n_features,)的浮点或类数组,默认=-NP.inf
最小可能的估算值。广播塑造
(n_features,)
如果是标量。如果是数组类型,则需要形状(n_features,)
,每个特征一个最小值。默认值为-np.inf
.在 0.23 版本发生变更: 添加了对类数组的支持。
- max_value形状(n_features,)的浮点或类数组,默认=NP.inf
最大可能的估算值。广播塑造
(n_features,)
如果是标量。如果是数组类型,则需要形状(n_features,)
,每个特征有一个最大值。默认值为np.inf
.在 0.23 版本发生变更: 添加了对类数组的支持。
- verboseint,默认=0
冗长标志,控制在评估函数时发出的调试消息。越高,越冗长。可以是0、1或2。
- random_stateint,RandomState实例或无,默认=无
要使用的伪随机数生成器的种子。如果满足以下条件,则随机选择估计器特征
n_nearest_features
不None
,imputation_order
如果random
,以及从后验如果进行抽样sample_posterior=True
.使用一个整数来表示决定性。看到 the Glossary .- add_indicator布尔,默认=假
如果
True
、aMissingIndicator
转换将叠加到输入器转换的输出上。这使得预测估计器能够解释失踪情况,尽管有估算。如果某个特征在调整/训练时没有缺失值,则即使在变换/测试时存在缺失值,该特征也不会出现在缺失指示器上。- keep_empty_features布尔,默认=假
如果为True,则仅包含缺失值的功能
fit
被调用时在结果中返回transform
被称为。估算价值始终是0
除非initial_strategy="constant"
在这种情况下fill_value
将被使用。Added in version 1.2.
- 属性:
- initial_imputer_ : object of type
SimpleImputer
类型的对象 用于初始化缺失值的输入机。
- imputation_sequence_元组列表
每个二元组都有
(feat_idx, neighbor_feat_idx, estimator)
,在哪里feat_idx
是当前要估算的特征,neighbor_feat_idx
是用于估算当前特征的其他特征的数组,并且estimator
是用于插补的经过训练的估计器。长度self.n_features_with_missing_ * self.n_iter_
.- n_iter_int
Number of iteration rounds that occurred. Will be less than
self.max_iter
if early stopping criterion was reached.- n_features_in_int
期间看到的功能数量 fit .
Added in version 0.24.
- feature_names_in_ :nd形状数组 (
n_features_in_
,)nd数组形状( Names of features seen during fit. Defined only when
X
has feature names that are all strings.Added in version 1.0.
- n_features_with_missing_int
缺失值的功能数量。
- indicator_ :
MissingIndicator
MissingIndicator
用于为缺失值添加二进制指示符的指示符。
None
如果add_indicator=False
.- random_state_RandomState实例
RandomState实例,该实例由种子、随机数生成器或
np.random
.
- initial_imputer_ : object of type
参见
SimpleImputer
Univariate imputer for completing missing values with simple strategies.
KNNImputer
多元插补器,使用最近的样本估计缺失的特征。
注意到
To support imputation in inductive mode we store each feature's estimator during the
fit
phase, and predict without refitting (in order) during thetransform
phase.包含所有缺失值的功能
fit
被丢弃在transform
.使用默认值,估算器进行缩放 \(\mathcal{O}(knp^3\min(n,p))\) 哪里 \(k\) =
max_iter
, \(n\) 样本数量和 \(p\) 功能的数量。因此,当特征数量增加时,它的成本就会变得高得令人望而却步。设置n_nearest_features << n_features
,skip_complete=True
或增加tol
可以帮助降低其计算成本。根据缺失值的性质,在预测环境中,简单插补器可能更可取。
引用
[1]Stef van Buuren, Karin Groothuis-Oudshoorn (2011). "mice: Multivariate Imputation by Chained Equations in R". Journal of Statistical Software 45: 1-67. <https://www.jstatsoft.org/article/view/v045i03>
_[2]S. F. Buck, (1960). "A Method of Estimation of Missing Values in Multivariate Data Suitable for use with an Electronic Computer". Journal of the Royal Statistical Society 22(2): 302-306. <https://www.jstor.org/stable/2984099>
_示例
>>> import numpy as np >>> from sklearn.experimental import enable_iterative_imputer >>> from sklearn.impute import IterativeImputer >>> imp_mean = IterativeImputer(random_state=0) >>> imp_mean.fit([[7, 2, 3], [4, np.nan, 6], [10, 5, 9]]) IterativeImputer(random_state=0) >>> X = [[np.nan, 2, 3], [4, np.nan, 6], [10, np.nan, 9]] >>> imp_mean.transform(X) array([[ 6.9584..., 2. , 3. ], [ 4. , 2.6000..., 6. ], [10. , 4.9999..., 9. ]])
有关更详细的示例,请参阅 在构建估计器之前输入缺失值 或 使用IterativeImputer的变体输入缺失值 .
- fit(X, y=None, **fit_params)[源代码]#
安装输入器
X
并回归自我。- 参数:
- X类数组,形状(n_samples,n_features)
输入数据,其中
n_samples
是样本数量和n_features
是功能的数量。- y忽视
未使用,按照惯例,为了API一致性而存在。
- **fit_paramsdict
参数路由到
fit
方法的子估计器通过元数据路由API。Added in version 1.5: Only available if
sklearn.set_config(enable_metadata_routing=True)
is set. See Metadata Routing User Guide for more details.
- 返回:
- self对象
拟合估计量。
- fit_transform(X, y=None, **params)[源代码]#
安装输入器
X
并归还变形的X
.- 参数:
- X类数组,形状(n_samples,n_features)
输入数据,其中
n_samples
是样本数量和n_features
是功能的数量。- y忽视
未使用,按照惯例,为了API一致性而存在。
- **paramsdict
参数路由到
fit
方法的子估计器通过元数据路由API。Added in version 1.5: Only available if
sklearn.set_config(enable_metadata_routing=True)
is set. See Metadata Routing User Guide for more details.
- 返回:
- Xt类数组,形状(n_samples,n_features)
插补的输入数据。
- get_feature_names_out(input_features=None)[源代码]#
获取用于转换的输出要素名称。
- 参数:
- input_features字符串或无的类数组,默认=无
输入功能。
如果
input_features
是None
那么feature_names_in_
在中用作功能名称。如果feature_names_in_
未定义,则生成以下输入要素名称:["x0", "x1", ..., "x(n_features_in_ - 1)"]
.如果
input_features
是一个类似阵列的,那么input_features
必须匹配feature_names_in_
如果feature_names_in_
是定义的。
- 返回:
- feature_names_out字符串对象的nd数组
转换的功能名称。
- get_metadata_routing()[源代码]#
获取此对象的元数据路由。
请检查 User Guide 关于路由机制如何工作。
Added in version 1.5.
- 返回:
- routingMetadataRouter
A
MetadataRouter
封装路由信息。
- get_params(deep=True)[源代码]#
获取此估计器的参数。
- 参数:
- deep布尔,默认=True
如果为True,将返回此估计量和包含的作为估计量的子对象的参数。
- 返回:
- paramsdict
参数名称映射到其值。
- set_output(*, transform=None)[源代码]#
设置输出容器。
看到 介绍 set_output API 了解如何使用API的示例。
- 参数:
- transform{“默认”,“pandas”,“polars”},默认=无
配置输出
transform
和fit_transform
."default"
:Transformer的默认输出格式"pandas"
:DataFrame输出"polars"
:两极输出None
:转换配置不变
Added in version 1.4:
"polars"
添加了选项。
- 返回:
- self估计器实例
估计实例。