ValueError:找到具有0个特征的数组(shape =(546,0)),而最少需要1个

时间:2018-08-28 04:22:54

标签: python pandas numpy scikit-learn

我只是在尝试频繁发生此错误的DataPreprocessing。有人能解释一下给定数据集的此特定代码有什么问题吗?

谢谢!

# STEP 1: IMPORTING THE LIBARIES

import numpy as np
import pandas as pd

# STEP 2: IMPORTING THE DATASET
dataset = pd.read_csv("https://github.com/Avik-Jain/100-Days-Of-ML-Code/blob/master/datasets/Data.csv", error_bad_lines=False)

X = dataset.iloc[:,:-1].values  
Y = dataset.iloc[:,1:3].values

# STEP 3: HANDLING THE MISSING VALUES
from sklearn.preprocessing import Imputer

imputer = Imputer(missing_values = "NaN",strategy = "mean",axis = 0)
imputer = imputer.fit(X[ : , 1:3])
X[:,1:3] = imputer.transform(X[:,1:3]) 

# STEP 4: ENCODING CATEGPRICAL DATA
from sklearn.preprocessing import LaberEncoder,OneHotEncoder
labelencoder_X = LabelEncoder()  # Encode labels with value between 0 and n_classes-1.
X[ : , 0] = labelencoder_X.fit_transform(X[ : , 0]) # All the rows and first columns

onehotencoder = OneHotEncoder(categorical_features = [0])
X = onehotencoder.fit_transform(X).toarray()

labelencoder_Y = LabelEncoder()
Y =  labelencoder_Y.fit_transform(Y)

# Step 5: Splitting the datasets into training sets and Test sets

from sklearn.cross_validation import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split( X , Y , test_size = 0.2, random_state = 0)

# Step 6: Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.fit_transform(X_test)

返回错误:

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

1 个答案:

答案 0 :(得分:1)

您在此行中的链接

dataset = pd.read_csv("https://github.com/Avik-Jain/100-Days-Of-ML-Code/blob/master/datasets/Data.csv", error_bad_lines=False)

错了。

当前链接返回显示此csv的github上的网页,但不返回实际的csv数据。因此,dataset中存在的任何数据都是无效的。

将其更改为:

dataset = pd.read_csv("https://raw.githubusercontent.com/Avik-Jain/100-Days-Of-ML-Code/master/datasets/Data.csv", error_bad_lines=False)

除此之外,LabelEncoder导入中存在拼写错误。

现在,即使您更正了这些问题,由于以下原因,仍然会有错误

Y =  labelencoder_Y.fit_transform(Y)

LabelEncoder仅接受单个列数组作为输入,但是由于

,您当前的Y将是2列
Y = dataset.iloc[:,1:3].values

请更清楚地说明您想做什么。