0

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,

8
  • post some data to reproduce this Commented Nov 22, 2019 at 23:06
  • I put a picture of my dataset in order to show you the structure. It's a big one, with more than 25.000 values. Commented Nov 22, 2019 at 23:14
  • what does X.shape return? Commented Nov 22, 2019 at 23:16
  • It returns (14085, 8). I made a mistake on the comment above, I have 14085 observations. Commented Nov 22, 2019 at 23:18
  • np.arange(min(X), max(X), 0.1) returns a 1D array, not a 2D one. Commented Nov 22, 2019 at 23:20

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, that's exactly the point. How can I fix the problem then ?
you have to define a sample that has also 8 features, or fit the sc model using only one variable from X
After modifying the code, I have this new error message : non-broadcastable output operand with shape (1,1) doesn't match the broadcast shape (1,2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.