scikit-learn的Imputation变换器的docs说
当axis = 0时,在变换时将丢弃仅包含缺失值的列。
由于imputer返回一个numpy数组,我如何检查在插补过程中丢弃了哪些功能,或相应地,在插补后保留了哪些功能?
这是一个简单的例子:
import pandas as pd
import numpy as np
from sklearn.preprocessing import Imputer
df = pd.DataFrame(np.random.randn(10, 5), columns=['a', 'b', 'c', 'd', 'e'])
df['f'] = len(df3)*['NaN']
这是数据框:
>>> df
a b c d e f
0 -1.284658 0.246541 -1.120987 0.559911 -1.189870 NaN
1 0.773717 0.430597 -0.004346 -1.292080 1.993266 NaN
2 1.418761 -0.004749 -0.181932 -0.305756 -0.135870 NaN
3 0.418673 -0.376318 -0.860783 0.074135 -1.034095 NaN
4 -0.019873 0.006210 0.364384 1.029895 -0.188727 NaN
5 0.903661 0.123575 -0.556970 1.344985 -1.109806 NaN
6 -0.069168 -0.385597 0.684345 0.645920 1.159898 NaN
7 0.695782 0.030239 -0.777304 -0.037102 2.053028 NaN
8 -0.256409 0.106735 -0.729710 0.254626 1.064925 NaN
9 0.235507 -0.087767 0.626121 1.391286 0.449158 NaN
现在我创建了一个imputer imp
:
imp = Imputer(missing_values='NaN', strategy='mean', axis=0)
imp.fit(df)
imputed = imp.transform(df)
这是从输入中返回的numpy数组。
>>> imputed
array([[-1.28465763, 0.24654083, -1.12098675, 0.55991059, -1.18986998],
[ 0.77371694, 0.43059674, -0.0043461 , -1.29208032, 1.99326594],
[ 1.41876145, -0.0047488 , -0.18193164, -0.30575631, -0.13586974],
[ 0.41867326, -0.37631792, -0.86078293, 0.07413458, -1.03409532],
答案 0 :(得分:5)
如何检查在插补过程中丢弃了哪些功能?
包含所有NaN
的列将被丢弃。您可以在不使用fit
的{{1}}和transform
流程的情况下进行检查。 df.isnull().all()
的地方,那些是"功能"那将被丢弃。
确切的答案是将True
添加到您的imputer中,如下所示:
verbose=1
要让此示例更清楚地显示正在进行的操作,请将另一列添加到包含所有imp = Imputer(verbose=1)
的{{1}}。
df
NaN
现在看起来像这样:
df.insert(2, 'g', np.nan)
...运行
df
现在输出以下"详细"消息,告诉您哪些列已被删除 a b g c d e f
0 -1.284658 0.246541 NaN -1.120987 0.559911 -1.189870 NaN
1 0.773717 0.430597 NaN -0.004346 -1.292080 1.993266 NaN
2 1.418761 -0.004749 NaN -0.181932 -0.305756 -0.135870 NaN
3 0.418673 -0.376318 NaN -0.860783 0.074135 -1.034095 NaN
4 -0.019873 0.006210 NaN 0.364384 1.029895 -0.188727 NaN
5 0.903661 0.123575 NaN -0.556970 1.344985 -1.109806 NaN
6 -0.069168 -0.385597 NaN 0.684345 0.645920 1.159898 NaN
7 0.695782 0.030239 NaN -0.777304 -0.037102 2.053028 NaN
8 -0.256409 0.106735 NaN -0.729710 0.254626 1.064925 NaN
9 0.235507 -0.087767 NaN 0.626121 1.391286 0.449158 NaN
:
imp.fit(df)
imp.transform(df)
插补后保留了哪些功能?
插补后留下的列和值。
使用我之前的[2 6]
,如果我们在混音中添加一些Warning (from warnings module):
File "C:\Python34\lib\site-packages\sklearn\preprocessing\imputation.py", line 347
"observed values: %s" % missing)
UserWarning: Deleting features without observed values: [2 6]
array([[-1.284658, 0.246541, -1.120987, 0.559911, -1.18987 ],
[ 0.773717, 0.430597, -0.004346, -1.29208 , 1.993266],
[ 1.418761, -0.004749, -0.181932, -0.305756, -0.13587 ],
[ 0.418673, -0.376318, -0.860783, 0.074135, -1.034095],
[-0.019873, 0.00621 , 0.364384, 1.029895, -0.188727],
[ 0.903661, 0.123575, -0.55697 , 1.344985, -1.109806],
[-0.069168, -0.385597, 0.684345, 0.64592 , 1.159898],
[ 0.695782, 0.030239, -0.777304, -0.037102, 2.053028],
[-0.256409, 0.106735, -0.72971 , 0.254626, 1.064925],
[ 0.235507, -0.087767, 0.626121, 1.391286, 0.449158]])
:
df
NaN
看起来像这样:
df.loc[[1, 7, 3], ['a', 'c', 'e']] = np.nan
了解您正在使用的估算策略的重要性。 df
的默认值为 mean 。这意味着它将使用给定列的平均值替换 a b g c d e f
0 -1.284658 0.246541 NaN -1.120987 0.559911 -1.189870 NaN
1 NaN 0.430597 NaN NaN -1.292080 NaN NaN
2 1.418761 -0.004749 NaN -0.181932 -0.305756 -0.135870 NaN
3 NaN -0.376318 NaN NaN 0.074135 NaN NaN
4 -0.019873 0.006210 NaN 0.364384 1.029895 -0.188727 NaN
5 0.903661 0.123575 NaN -0.556970 1.344985 -1.109806 NaN
6 -0.069168 -0.385597 NaN 0.684345 0.645920 1.159898 NaN
7 NaN 0.030239 NaN NaN -0.037102 NaN NaN
8 -0.256409 0.106735 NaN -0.729710 0.254626 1.064925 NaN
9 0.235507 -0.087767 NaN 0.626121 1.391286 0.449158 NaN
值。
为证明这一点,请先检查每列的平均值:
Imputer
然后你可以进行拟合和变换,看看变换的估算数据中的任何值是否在NaN
超参数中。
>>> df.mean()
a 0.132546
b 0.008947
g NaN
c -0.130678
d 0.366582
e 0.007101
f NaN
dtype: float64
返回以下内容 - 需要注意的关键是,imp.statistics_
值已替换为给定列的imp = Imputer(verbose=1)
imp.fit(df)
imp.transform(df)
。例如,无论您在第一列中看到NaN
,您都会注意到它们出现在第1,3和7行(之前为mean
s):
0.13254586
如果您想进行布尔比较以查看估算的值,您可以执行以下操作(不是万无一失但是最可靠的方式):
NaN