While practicing Support Vector Regression Model I got this error
Here is my data set:
Here is independent variable X:
Here is dependent variable Y:
Here is X_train
Here Is Y_train
Error body:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-40-2de23298b092> in <module>()
----> 1 sc_y.inverse_transform(regressor.predict(sc_X.transform([[6.5]])))
1 frames
/usr/local/lib/python3.7/dist-packages/sklearn/preprocessing/_data.py in inverse_transform(self, X, copy)
1020 estimator=self,
1021 dtype=FLOAT_DTYPES,
-> 1022 force_all_finite="allow-nan",
1023 )
1024
/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
763 "Reshape your data either using array.reshape(-1, 1) if "
764 "your data has a single feature or array.reshape(1, -1) "
--> 765 "if it contains a single sample.".format(array)
766 )
767
ValueError: Expected 2D array, got 1D array instead:
array=[0.01150915].
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.
My code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:, 1:-1].values
y = dataset.iloc[:, -1].values
y = y.reshape(len(y), 1)
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_y.fit_transform(y)
from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf')
regressor.fit(X, y)
sc_y.inverse_transform(regressor.predict(sc_X.transform([[6.5]])))
CodePudding user response:
The issue is with the test input [[6.5]] which you are providing to sc_X.transform()) in the last line of your code.
Try to find the shape of X and test input using numpy.shape and make sure that number of dimensions should be 2 in both cases. So you will get something like (Xi, Xj), (Ti, Tj). Also, Xj must be same as Tj (basically number of features must match for the train and test data)
CodePudding user response:
If your dataset has 8 columns, here is how you pick X and y
values = dataset.values
X = values[:,0:8]
y = values[:,8]
take it from there, note too that values is deprecated, instead it is to_numpy()
