简体   繁体   English

"预期的二维数组,得到一维数组而不是错误"

[英]Expected 2D array, got 1D array instead error

Iam getting the error as我收到错误为

"ValueError: Expected 2D array, got 1D array instead: array=[ 45000. 50000. 60000. 80000. 110000. 150000. 200000. 300000. 500000. 1000000.]. 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." “ValueError:预期 2D 数组,得到 1D 数组:array=[ 45000. 50000. 60000. 80000. 110000. 150000. 200000. 300000. 500000. 1000000.]。使用 array.reshape(-1, 1) 重塑数据) 如果您的数据具有单个特征或 array.reshape(1, -1) 如果它包含单个样本。"

<\/blockquote>

while executing the following code:在执行以下代码时:

 # SVR # Importing the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd # Importing the dataset dataset = pd.read_csv('Position_S.csv') X = dataset.iloc[:, 1:2].values y = dataset.iloc[:, 2].values # Feature Scaling from sklearn.preprocessing import StandardScaler sc_X = StandardScaler() sc_y = StandardScaler() X = sc_X.fit_transform(X) y = sc_y.fit_transform(y) # Fitting SVR to the dataset from sklearn.svm import SVR regressor = SVR(kernel = 'rbf') regressor.fit(X, y) # Visualising the SVR results plt.scatter(X, y, color = 'red') plt.plot(X, regressor.predict(X), color = 'blue') plt.title('Truth or Bluff (SVR)') plt.xlabel('Position level') plt.ylabel('Salary') plt.show() # Visualising the SVR results (for higher resolution and smoother curve) X_grid = np.arange(min(X), max(X), 0.01) X_grid = X_grid.reshape((len(X_grid), 1)) plt.scatter(X, y, color = 'red') plt.plot(X_grid, regressor.predict(X_grid), color = 'blue') plt.title('Truth or Bluff (SVR)') plt.xlabel('Position level') plt.ylabel('Salary') plt.show()<\/code><\/pre>"

Seems, expected dimension is wrong.似乎,预期的尺寸是错误的。 Could you try:你能不能试试:

regressor = SVR(kernel='rbf')
regressor.fit(X.reshape(-1, 1), y)

The problem is if you type y.ndim, you will see the dimension as 1, and if you type X.ndim, you will see the dimension as 2.问题是如果你输入 y.ndim,你会看到维度为 1,如果你输入 X.ndim,你会看到维度为 2。

So to solve this problem you have to change the result of y.ndim from 1 to 2.所以要解决这个问题,你必须将 y.ndim 的结果从 1 改为 2。

For this just use the reshape function that comes under numpy class.为此,只需使用 numpy 类下的 reshape 函数。

data=pd.read_csv("Position_Salaries.csv")
X=data.iloc[:,1:2].values
y=data.iloc[:,2].values
y=np.reshape(y,(10,1))

It should solve the problem caused due to dimension.它应该解决由于尺寸引起的问题。 Do the regular Feature Scaling after the above code and it will work for sure.在上面的代码之后做常规的特征缩放,它肯定会起作用。

Do vote if it works for you.如果它对你有用,请投票。

Thanks.谢谢。

from sklearn.preprocessing import StandardScaler  

#Creating two objects for dependent and independent variable 
ss_X = StandardScaler()
ss_y = StandardScaler()

X = ss_X.fit_transform(X)
y = ss_y.fit_transform(y.reshape(-1,1))

After Reshape thing it will work fine重塑后它会正常工作

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

相关问题 预期的二维数组,得到的是一维数组:array=[1 3 5 6 7 8 9]? - Expected 2D array, got 1D array instead: array=[1 3 5 6 7 8 9]? MLPClassifier:预期的2D数组取而代之的是1D数组 - MLPClassifier: Expected 2D array got 1D array instead ValueError:预期的二维数组,取而代之的是一维数组 - ValueError: Expected 2D array, got 1D array instead 预期的2D阵列,改为1D阵列 - Expected 2D array, got 1D array instead Python 脚本中的错误“预期的二维数组,而是得到一维数组:”? - Error in Python script "Expected 2D array, got 1D array instead:"? 错误消息:ValueError:预期的 2D 数组,得到 1D 数组: - error message: ValueError: Expected 2D array, got 1D array instead: Sklearn 变换错误:预期 2D 数组,得到 1D 数组 - Sklearn transform error: Expected 2D array, got 1D array instead google colab 中的错误“预期的二维数组,得到的是一维数组:” - Error in google colab "Expected 2D array, got 1D array instead:" python程序中的错误。 “预期为2D数组,但改为有1D数组” - error in python program. “expected 2D array but got 1D array instead” 修复缩放器上的错误“预期 2D 数组,得到 1D 数组” - Fix error ''Expected 2D array, got 1D array instead'' on a scaler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM