Expected 2D array, got 1D array instead error

喜夏-厌秋 提交于 2021-01-28 02:03:37

问题


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."

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()

回答1:


Seems, expected dimension is wrong. Could you try:

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



回答2:


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.

So to solve this problem you have to change the result of y.ndim from 1 to 2.

For this just use the reshape function that comes under numpy class.

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.



来源:https://stackoverflow.com/questions/52961851/expected-2d-array-got-1d-array-instead-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!