mmcr_economics
mmcr_economics

Reputation: 37

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

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. my dataset

Thanks,

Upvotes: 0

Views: 679

Answers (1)

seralouk
seralouk

Reputation: 33127

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.

Upvotes: 1

Related Questions