Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVR Model -->Feature Scaling - Expected 2D array, got 1D array instead

I am trying to understand what is wrong with the code below. I know that the Y variable is 1D array and expected to be 2D array and need to reshape the structure but that code was working previously fine with a warning.

# Importing the libraries
  import numpy as np
  import matplotlib.pyplot as plt
  import pandas as pd

# Importing the dataset
  dataset = pd.read_csv('Position_Salaries.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)

ValueError: Expected 2D array, got a 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.
like image 655
Raman goyal Avatar asked May 19 '18 15:05

Raman goyal


1 Answers

The solution is in the error message:

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.

Since you're passing in a single feature (not a single sample), try:

y = sc_y.fit_transform(y.reshape(-1, 1))
like image 127
Peter Leimbigler Avatar answered Sep 29 '22 20:09

Peter Leimbigler