sed*_*deh 5 python python-2.7 orange scikit-learn imputation
我试图在Python中计算缺失值,并且sklearn似乎没有超出平均值(均值,中位数或模式)插补的方法.橙色插补模型似乎提供了一个可行的选择.然而,似乎Orange.data.Table没有认识到np.nan或以某种方式归因于失败.
import Orange
import numpy as np
tmp = np.array([[1, 2, np.nan, 5, 8, np.nan], [40, 4, 8, 1, 0.2, 9]])
data = Orange.data.Table(tmp)
imputer = Orange.feature.imputation.ModelConstructor()
imputer.learner_continuous = Orange.classification.tree.TreeLearner(min_subset=20)
imputer = imputer(data )
impdata = imputer(data)
for i in range(0, len(tmp)):
print impdata[i]
Run Code Online (Sandbox Code Playgroud)
输出是
[1.000, 2.000, 1.#QO, 5.000, 8.000, 1.#QO]
[40.000, 4.000, 8.000, 1.000, 0.200, 9.000]
Run Code Online (Sandbox Code Playgroud)
知道我错过了什么吗?谢谢!
问题似乎在于 Orange 中的缺失值表示为?或~。奇怪的是,Orange.data.Table(numpy.ndarray)构造函数并没有推断numpy.nan应该转换为?or ~,而是将它们转换为1.#QO. 下面的自定义函数pandas_to_orange()解决了这个问题。
import Orange
import numpy as np
import pandas as pd
from collections import OrderedDict
# Adapted from https://github.com/biolab/orange3/issues/68
def construct_domain(df):
columns = OrderedDict(df.dtypes)
def create_variable(col):
if col[1].__str__().startswith('float'):
return Orange.feature.Continuous(col[0])
if col[1].__str__().startswith('int') and len(df[col[0]].unique()) > 50:
return Orange.feature.Continuous(col[0])
if col[1].__str__().startswith('date'):
df[col[0]] = df[col[0]].values.astype(np.str)
if col[1].__str__() == 'object':
df[col[0]] = df[col[0]].astype(type(""))
return Orange.feature.Discrete(col[0], values = df[col[0]].unique().tolist())
return Orange.data.Domain(list(map(create_variable, columns.items())))
def pandas_to_orange(df):
domain = construct_domain(df)
df[pd.isnull(df)]='?'
return Orange.data.Table(Orange.data.Domain(domain), df.values.tolist())
df = pd.DataFrame({'col1':[1, 2, np.nan, 4, 5, 6, 7, 8, 9, np.nan, 11],
'col2': [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110.]})
tmp = pandas_to_orange(df)
for i in range(0, len(tmp)):
print tmp[i]
Run Code Online (Sandbox Code Playgroud)
输出是:
[1.000, 10.000]
[2.000, 20.000]
[?, 30.000]
[4.000, 40.000]
[5.000, 50.000]
[6.000, 60.000]
[7.000, 70.000]
[8.000, 80.000]
[9.000, 90.000]
[?, 100.000]
[11.000, 110.000]
Run Code Online (Sandbox Code Playgroud)
我想要正确编码缺失值的原因是这样我可以使用 Orange 插补库。然而,库中的预测树模型似乎只做简单的均值插补。具体来说,它为所有缺失值估算相同的值。
imputer = Orange.feature.imputation.ModelConstructor()
imputer.learner_continuous = Orange.classification.tree.TreeLearner(min_subset=20)
imputer = imputer(tmp )
impdata = imputer(tmp)
for i in range(0, len(tmp)):
print impdata[i]
Run Code Online (Sandbox Code Playgroud)
这是输出:
[1.000, 10.000]
[2.000, 20.000]
[5.889, 30.000]
[4.000, 40.000]
[5.000, 50.000]
[6.000, 60.000]
[7.000, 70.000]
[8.000, 80.000]
[9.000, 90.000]
[5.889, 100.000]
[11.000, 110.000]
Run Code Online (Sandbox Code Playgroud)
我一直在寻找能够在完整案例上拟合模型(例如 kNN)的东西,并使用拟合模型来预测缺失的案例。fancyimpute(Python 3 包)可以做到这一点,但会MemoryError引发我的 300K+ 输入。
from fancyimpute import KNN
df = pd.DataFrame({'col1':[1, 2, np.nan, 4, 5, 6, 7, 8, 9, np.nan, 11],
'col2': [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110.]})
X_filled_knn = KNN(k=3).complete(df)
X_filled_knn
Run Code Online (Sandbox Code Playgroud)
输出是:
array([[ 1. , 10. ],
[ 2. , 20. ],
[ 2.77777784, 30. ],
[ 4. , 40. ],
[ 5. , 50. ],
[ 6. , 60. ],
[ 7. , 70. ],
[ 8. , 80. ],
[ 9. , 90. ],
[ 9.77777798, 100. ],
[ 11. , 110. ]])
Run Code Online (Sandbox Code Playgroud)
我可能可以找到解决方法或将数据集分成块(不理想)。