Problem with “ValueError: Expected 2D array, got 1D array instead”

点点圈 提交于 2021-01-29 13:18:55

问题


I need to run a SVR (supported vector regression). I have a CSV data frame.I had no problems to run the OLS regression, with one target variable and multiple regressors. But I have a problem with this part of the code.

So, here is my code:


import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR

sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_y.fit_transform(y)

y_pred = sc_y.inverse_transform ((regressor.predict (sc_X.transform(np.array([[6.5]])))))
plt.scatter(X, y, color = 'magenta')
plt.plot(X, regressor.predict(X))
plt.title('SVR')
plt.xlabel('X')
plt.ylabel('VF')
plt.show()

X_grid = np.arange(min(X), max(X), 0.1)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y)
plt.plot(X_grid, regressor.predict(X_grid))
plt.title('SVR')
plt.xlabel('X')
plt.ylabel('VF')
plt.show()

I have the following error message:"ValueError: Expected 2D array, got 1D array instead 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."

It's the first time I encounter this problem. I saw in other topic that is not scarce, but in fact i don't understand where to reshape the data in my code. When i tried to do it, it says that DataFrame has no reshape function.

Here is a pic of my dataset. The target is VF, all the other variables are the regressors.

Thanks,


回答1:


It seems that when you do:

 X = sc_X.fit_transform(X)

X contains more than one variables. 8 to be specific

Next you do:

 regressor.predict(sc_X.transform(np.array([[6.5]])))

Now you try to transform a new sample that has only one variable but the sc model was trained on data with more than one variable.



来源:https://stackoverflow.com/questions/59002898/problem-with-valueerror-expected-2d-array-got-1d-array-instead

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