pandas ValueError: 找到具有 0 个特征的数组 (shape=(546, 0)) 而最少需要 1 个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52050057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: Found array with 0 feature(s) (shape=(546, 0)) while a minimum of 1 is required
提问by Rushiraj Parmar
I was just trying out for DataPreprocessing where I frequently get this error.Can anyone explain me what is wrong in this particular code for the given dataset?
我只是在尝试 DataPreprocessing,我经常收到这个错误。谁能解释一下给定数据集的这个特定代码有什么问题?
Thanks in advance!
提前致谢!
# 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)
Returns Error:
返回错误:
ValueError: Found array with 0 feature(s) (shape=(546, 0)) while a minimum of 1 is required.
回答by Vivek Kumar
Your link in this line
你在这一行的链接
dataset = pd.read_csv("https://github.com/Avik-Jain/100-Days-Of-ML-Code/blob/master/datasets/Data.csv", error_bad_lines=False)
is wrong.
是错的。
The current link returns the webpage on github where this csv is shown, but not the actual csv data. So whatever data is present in dataset
is invalid.
当前链接返回 github 上显示此 csv 的网页,但不是实际的 csv 数据。所以任何存在的数据dataset
都是无效的。
Change that to:
将其更改为:
dataset = pd.read_csv("https://raw.githubusercontent.com/Avik-Jain/100-Days-Of-ML-Code/master/datasets/Data.csv", error_bad_lines=False)
Other than that, there is a spelling mistake in LabelEncoder
import.
除此之外,LabelEncoder
导入中存在拼写错误。
Now even if you correct these, there will still be errors, because of
现在即使你纠正了这些,仍然会有错误,因为
Y = labelencoder_Y.fit_transform(Y)
LabelEncoder only accepts a single column array as input, but your current Y
will be of 2 columns due to
LabelEncoder 只接受单列数组作为输入,但Y
由于
Y = dataset.iloc[:,1:3].values
Please explain more clearly what do you want to do.
请更清楚地说明您想做什么。