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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With