In sklearn.preprocessing module I get ValueError: Found array with 0 feature(s)

In sklearn.preprocessing module I get ValueError: Found array with 0 feature(s)

我看到一堆问题都有这个错误,但我无法理解与我的代码或问题的关系。

我正在尝试修复从 Internet 上找到的样本 CSV 文件中获得的数据中的 NaN 值。我的代码其实很简单:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Importing stuff.
from sklearn.preprocessing import Imputer
import pandas

# Loading the corrupt data
corrupt_data = pandas.read_csv('SampleCorruptData.csv')

#Creating Imputer object
imputer = Imputer(missing_values = 'NaN', strategy= "mean", axis = 0)

owner_id = corrupt_data.iloc[:,2:]

print(owner_id)

imputer = imputer.fit(owner_id.iloc[:,2:])

owner_id.iloc[:,2:] = imputer.transform(owner_id[:,2:])

print(owner_id)

CSV 文件:

GroupName,Groupcode,GroupOwner
System Administrators,sysadmin,13456
Independence High Teachers,HS Teachers,
John Glenn Middle Teachers,MS Teachers,13458
Liberty Elementary Teachers,Elem Teachers,13559
1st Grade Teachers,1stgrade,NaN
2nd Grade Teachers,2nsgrade,13561
3rd Grade Teachers,3rdgrade,13562
Guidance Department,guidance,NaN
Independence Math Teachers,HS Math,13660
Independence English Teachers,HS English,13661
John Glenn 8th Grade Teachers,8thgrade,
John Glenn 7th Grade Teachers,7thgrade,13452
Elementary Parents,Elem Parents,NaN
Middle School Parents,MS Parents,18001
High School Parents,HS Parents,18002

您可以看到 NaN 值。

我得到的错误:

Traceback (most recent call last):

  File "<ipython-input-21-1bfc8eb216cc>", line 1, in <module>
    runfile('/home/teoman/Desktop/data science/Fix Corrupt Data/imputation.py', wdir='/home/teoman/Desktop/data science/Fix Corrupt Data')

  File "/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/teoman/Desktop/data science/Fix Corrupt Data/imputation.py", line 18, in <module>
    imputer = imputer.fit(owner_id.iloc[:,2:])

  File "/home/teoman/.local/lib/python3.5/site-packages/sklearn/preprocessing/imputation.py", line 155, in fit
    force_all_finite=False)

  File "/home/teoman/.local/lib/python3.5/site-packages/sklearn/utils/validation.py", line 470, in check_array
    context))

ValueError: Found array with 0 feature(s) (shape=(15, 0)) while a minimum of 1 is required.

我这里做错了什么?

如果我们跟踪您的错误,我们可以找到解决方案

你的错误是:

ValueError: Found array with 0 feature(s) (shape=(15, 0)) while a minimum of 1 is required.

基本上它正在寻找至少 1 个特征。如果我们查看 docs of imputer: 参数: X:形状为 [n_samples、n_features]

的 numpy 数组

在你的情况下你有 15 n_samples 和 0 n_features 如果您转换数据并使 n_features > 0 ,您的问题将得到解决。

保留在挖掘的 1D numpy 数组 returns 0 列中,如果你用 numpy.reshape() function or convert it to pd.DataFrame 重塑它,你可以获得 1 n_features.

希望对您有所帮助

谢谢