簡體   English   中英

在sklearn.preprocessing模塊中,我得到ValueError:找到具有0個功能的數組

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

我看到很多問題都有此錯誤,但是我無法理解與代碼或問題的關系。

我正在嘗試修復從互聯網上找到的示例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:找到具有0個特征(shape =(15,0))的數組,而最小數量為1。

基本上,它正在尋找至少1個功能。 如果我們看一下imputer的文檔 :參數:X:形狀為[n_samples, n_features ]的numpy數組

在您的情況下,您有15個n_samples和0個n_features如果轉換數據並使n_features> 0,則將解決您的問題。

保留在開采的1D numpy數組中將返回0列,如果使用numpy.reshape()函數對其進行整形或將其轉換為pd.DataFrame ,則可以獲得1個n_features。

我希望這有幫助

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM