简体   繁体   English

预期的二维数组,取而代之的是一维数组,重塑数据

[英]Expected 2D array, got 1D array instead, Reshape Data

I'm really stuck on this problem.我真的被这个问题困住了。 I'm trying to use OneHotEncoder to encode my data into a matrix after using LabelEncoder but getting this error: Expected 2D array, got 1D array instead.在使用 LabelEncoder 后,我尝试使用 OneHotEncoder 将我的数据编码为矩阵,但出现此错误:预期为二维数组,改为得到一维数组。

At the end of the error message(included below) it said to "Reshape my data" which I thought I did but it's still not working.在错误消息的末尾(包括在下面),它说“重塑我的数据”,我以为我做到了,但它仍然无法正常工作。 If I understand Reshaping, is that just when you want to literally reshape some data into a different matrix size?如果我理解 Reshaping,那是否只是当您想将某些数据从字面上重塑为不同的矩阵大小时? For example, if I want to change a 3 x 2 matrix into a 4 x 6?例如,如果我想将 3 x 2 矩阵更改为 4 x 6?

My code is failing on these 2 lines:我的代码在这两行失败:

X = X.reshape(-1, 1) # I added this after I saw the error
X[:, 0] = onehotencoder1.fit_transform(X[:, 0]).toarray()

Here is the code I have so far:这是我到目前为止的代码:

# Data Preprocessing

# Import Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Import Dataset
dataset = pd.read_csv('Data2.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 5].values
df_X = pd.DataFrame(X)
df_y = pd.DataFrame(y)

# Replace Missing Values
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X[:, 3:5 ])
X[:, 3:5] = imputer.transform(X[:, 3:5])


# Encoding Categorical Data "Name"
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_x = LabelEncoder()
X[:, 0] = labelencoder_x.fit_transform(X[:, 0])

# Transform into a Matrix

onehotencoder1 = OneHotEncoder(categorical_features = [0])
X = X.reshape(-1, 1)
X[:, 0] = onehotencoder1.fit_transform(X[:, 0]).toarray()


# Encoding Categorical Data "University"
from sklearn.preprocessing import LabelEncoder
labelencoder_x1 = LabelEncoder()
X[:, 1] = labelencoder_x1.fit_transform(X[:, 1])

Here is the full error message:这是完整的错误消息:

 File "/Users/jim/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/data.py", line 1809, in _transform_selected
    X = check_array(X, accept_sparse='csc', copy=copy, dtype=FLOAT_DTYPES)

  File "/Users/jim/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py", line 441, in check_array
    "if it contains a single sample.".format(array))

ValueError: Expected 2D array, got 1D array instead:
array=[  2.00000000e+00   7.00000000e+00   3.20000000e+00   2.70000000e+01
   2.30000000e+03   1.00000000e+00   6.00000000e+00   3.90000000e+00
   2.80000000e+01   2.90000000e+03   3.00000000e+00   4.00000000e+00
   4.00000000e+00   3.00000000e+01   2.76700000e+03   2.00000000e+00
   8.00000000e+00   3.20000000e+00   2.70000000e+01   2.30000000e+03
   3.00000000e+00   0.00000000e+00   4.00000000e+00   3.00000000e+01
   2.48522222e+03   5.00000000e+00   9.00000000e+00   3.50000000e+00
   2.50000000e+01   2.50000000e+03   5.00000000e+00   1.00000000e+00
   3.50000000e+00   2.50000000e+01   2.50000000e+03   0.00000000e+00
   2.00000000e+00   3.00000000e+00   2.90000000e+01   2.40000000e+03
   4.00000000e+00   3.00000000e+00   3.70000000e+00   2.77777778e+01
   2.30000000e+03   0.00000000e+00   5.00000000e+00   3.00000000e+00
   2.90000000e+01   2.40000000e+03].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

Any help would be great.任何帮助都会很棒。

try changing you code to this尝试将您的代码更改为此

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Import Dataset
dataset = pd.read_csv('Data2.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 5].values
df_X = pd.DataFrame(X)
df_y = pd.DataFrame(y)

# Replace Missing Values
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X[:, 3:5 ])
X[:, 3:5] = imputer.transform(X[:, 3:5])


# Encoding Categorical Data "Name"
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_x = LabelEncoder()
X[:, 0] = labelencoder_x.fit_transform(X[:, 0])

# Transform into a Matrix

onehotencoder1 = OneHotEncoder(categorical_features = [0])
res_0 = onehotencoder1.fit_transform(X[:, 0].reshape(-1, 1))  # <=== Change
X[:, 0] = res_0.ravel()

# Encoding Categorical Data "University"
from sklearn.preprocessing import LabelEncoder
labelencoder_x1 = LabelEncoder()
X[:, 1] = labelencoder_x1.fit_transform(X[:, 1])

If you are getting error at labelencoder_x1.fit_transform(X[:, 1]) then make it labelencoder_x1.fit_transform(X[:, 1].reshape(-1, 1))如果您在labelencoder_x1.fit_transform(X[:, 1])处遇到错误, labelencoder_x1.fit_transform(X[:, 1])labelencoder_x1.fit_transform(X[:, 1].reshape(-1, 1))

Ok I finally got the code to work.好的,我终于让代码工作了。 Please see the solution below:请参阅以下解决方案:

# Data Preprocessing

# Import Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Import Dataset
dataset = pd.read_csv('Data2.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 5].values
df_X = pd.DataFrame(X)
df_y = pd.DataFrame(y)

# Replace Missing Values
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X[:, 3:5 ])
X[:, 3:5] = imputer.transform(X[:, 3:5])


# Encoding Categorical Data "Name"
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_x = LabelEncoder()
X[:, 0] = labelencoder_x.fit_transform(X[:, 0])


# Encoding Categorical Data "University"
from sklearn.preprocessing import LabelEncoder
labelencoder_x1 = LabelEncoder()
X[:, 1] = labelencoder_x1.fit_transform(X[:, 1])


# Transform Name into a Matrix
onehotencoder1 = OneHotEncoder(categorical_features = [0])
X = onehotencoder1.fit_transform(X).toarray()

# Transform University into a Matrix
onehotencoder2 = OneHotEncoder(categorical_features = [6])
X = onehotencoder2.fit_transform(X).toarray()

I got same error when I was trying to do same.当我尝试做同样的事情时,我遇到了同样的错误。 I was transforming single column of data.我正在转换单列数据。 Here, how I override this problem在这里,我如何解决这个问题

encoding_X = OneHotEncoder(categories = [np.unique(X[:,0]).tolist()])
encoding_X.fit(np.unique(X[:,0]).reshape(-1,1).tolist())
encoding_X.transform(X[:,0].reshape(-1,1).tolist()).toarray()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预期 2D 数组,得到 1D 数组:array=[5.6 7. ]。 使用 array.reshape(-1, 1) 重塑数据 - Expected 2D array, got 1D array instead: array=[5.6 7. ]. Reshape your data either using array.reshape(-1, 1) 预期的二维数组,得到的是一维数组:array=[1 3 5 6 7 8 9]? - Expected 2D array, got 1D array instead: array=[1 3 5 6 7 8 9]? ValueError:预期的 2D 数组,得到 1D 数组:array=[19. 27.896 0. 1. 0. ]。 使用 array.reshape(-1, 1) 重塑数据 - ValueError: Expected 2D array, got 1D array instead: array=[19. 27.896 0. 1. 0. ]. Reshape your data either using array.reshape(-1, 1) 预期的2D阵列,改为1D阵列 - Expected 2D array, got 1D array instead "预期的二维数组,得到一维数组而不是错误" - Expected 2D array, got 1D array instead error MLPClassifier:预期的2D数组取而代之的是1D数组 - MLPClassifier: Expected 2D array got 1D array instead ValueError:预期的二维数组,取而代之的是一维数组 - ValueError: Expected 2D array, got 1D array instead 这是什么原因:'ValueError: Expected 2D array, got 1D array instead: &amp; reshape your data using array.reshape(-1, 1)'? - what's the reasons about this:'ValueError: Expected 2D array, got 1D array instead: & reshape your data using array.reshape(-1, 1)'? 预期的2D数组,尝试反转比例数据时获得了1D数组 - Expected 2D array, got 1D array instead when attempting to invert scaled data sklearn MinMaxScaler - ValueError: Expected 2D array, got 1D array instead - data as series objects - sklearn MinMaxScaler - ValueError: Expected 2D array, got 1D array instead - data as series objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM