ValueError 无法将字符串转换为浮点数:sklearn 中的 IterativeImputer 是否仅用于数值特征?

ValueError could not convert string to float: is IterativeImputer in sklearn only for numerical features?

我有一个包含这种数据的数据框:

ID      category
ID2     category
Sex     category
Cysts   category
Death   category
Years   int64
Group   category

数据示例:

0    11090    1  0  0  0  46  1
1    10336    5  0  0  1  60  2
2     8117    8  1  0  1  39  1
3    10262    9  0  0  1  37  5
4    11084   10  0  0  1  47  1

'Cysts' 列中缺少 15 个条目,我想进行估算。

当我为 SimpleImputer 编写这段代码时:

import pandas as pd
import scipy.sparse as sp
from sklearn.impute import SimpleImputer
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer
import numpy as np

df = pd.read_csv('filtered.txt',sep='\t',dtype='category').iloc[:,:7]
print(df.dtypes)
imp = SimpleImputer(missing_values='-1',strategy='most_frequent')
df = pd.DataFrame(imp.fit_transform(df))
print(df)

它按预期打印输出:

..     ...  ... .. .. ..  .. ..
209  10373  164  1  1  0  44  1
210  11267  171  1  1  0  81  6
211  11101  175  1  1  1  65  1
212  11232  176  1  1  0  28  1
213  11236  176  1  1  0  31  1

(即本列中最初作为缺失数据的 -1 在第 4 列中替换为 1)。

import pandas as pd
import scipy.sparse as sp
from sklearn.impute import SimpleImputer
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer
import numpy as np

df = pd.read_csv('filtered.txt',sep='\t',dtype='category').iloc[:,:7]
print(df.dtypes)
imp = IterativeImputer(missing_values='-1',initial_strategy='most_frequent')
df = pd.DataFrame(imp.fit_transform(df))
print(df)

但是我得到错误:

ValueError: could not convert string to float: '8127/10206'

该值是 ID2 列中的值之一,我知道它不是浮点数,它不是故意的。

迭代输入器只能用于数字列吗?我想通过 'most_frequent' initial_strategy 参数可以使用分类数据,但也许我错了?

IterativeImputer 使用 Estimator 对象(默认为贝叶斯岭回归)使用其他列的值作为特征迭代地对每列的缺失值做出更好的预测。它不支持 non-numeric 数据。如果将数据转换为数字,迭代估算,然后 re-discretize 它,您可能会得到可接受的结果。

我没有对此进行测试,但您可以 one-hot 严格编码分类数据并用 Classifier.

迭代地估算它