【问题标题】:Scikit-learn's (0.22.1) KNNImputer returning wrong number of valuesScikit-learn 的 (0.22.1) KNNImputer 返回错误数量的值
【发布时间】:2020-06-20 10:05:25
【问题描述】:

我的原始数据集形状是 (790215,20),其中包含大约 60-80% 缺失值的特征。我决定使用 scikit-learn 的 KNNImputer 如下

import pandas as pd
from sklearn.impute import KNNImputer

# Loading the data
dataset = pd.read_csv('Dataset.csv')

# To avoid 'MemoryError' imputing individually

#Loading the first feature
X = dataset.iloc[:,0].values

#Imputing with default parameters 
imputer = KNNImputer()

#Reshaping to meet the dimensional requirement
X_imp  = imputer.fit_transform(X.reshape(1,-1))

现在X_imp 的形状是(1,729026)

我不确定我做错了什么。为什么 790215 改为 729026。

更新

X.shape(790215,)

X.reshape(1,-1).shape(1,790215)

X.reshape(1,-1)array([[ nan, 97., 89., ..., 140., 120., 115.]])

【问题讨论】:

  • 出于调试目的,能否打印 X.shapea 和 X.reshape(1, -1)?

标签: python pandas numpy scikit-learn


【解决方案1】:

您使用reshape 的方式是问题所在。您已通过提供.reshape(1, -1) 将数据转换为单个数据点。意思是 1 行 790215 列。因此,在转换 KNNImputer 时,会删除只有 nan 值的列。这就是下降的原因。

相反,您需要使用.reshape(-1,1),这将使其成为 790215 行和 1 列。

注意:对 KNNImputer 使用单一功能可能效果不佳。更好的是,您一次可以使用 3-5 个功能。你也可以看看SimpleImputer

【讨论】:

  • 1) 我用过.reshape(-1,1)MemoryError 出现了。 2) 再次同时使用更多功能 MemoryError 出现了 3) SimpleImputer 会是这么多缺失值的好选择吗?
  • 然后,fit 模型使用 50% 或 60% 的数据,然后在剩余数据中使用 transform
猜你喜欢
  • 2018-01-29
  • 2016-08-09
  • 2020-05-09
  • 2014-04-14
  • 2016-02-21
  • 2015-11-28
  • 2020-10-23
  • 2015-06-27
  • 2016-01-23
相关资源
最近更新 更多