Home > Blockchain >  Inverse Feature Scaling not working while predicting results
Inverse Feature Scaling not working while predicting results

Time:12-12

# Importing required libraries
import numpy as np
import pandas as pd

# Importing dataset
dataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:, 1: -1].values
y = dataset.iloc[:, -1].values

y = y.reshape(len(y), 1)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
scy = StandardScaler()
scX = StandardScaler()
X = scX.fit_transform(X)
y = scy.fit_transform(y)

# Training SVR model
from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf')
regressor.fit(X, y)

# Predicting results from SCR model
# this line is generating error
scy.inverse_transform(regressor.predict(scX.transform([[6.5]])))

I am trying to execute this code to predict values from the model but after running it I am getting errors like this:

ValueError: Expected 2D array, got 1D array instead:
array=[-0.27861589].
Reshape your data either using an array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

Complete Stack trace of error: Stack Trace of Error

Even my instructor is using the same code but his one is working mine one not I am new to machine learning can anybody tell me what I am doing wrong here. Thanks for your help. This is the data for reference Position_Salaries.csv

CodePudding user response:

It is because of the shape of your predictions, the scy is expecting an output with (-1, 1) shape.
Change your last line to this:

scy.inverse_transform([regressor.predict(scX.transform([[6.5]]))])

You can also use this line to predict:

pred = regressor.predict(scX.transform([[6.5]]))
pred = pred.reshape(-1, 1)
scy.inverse_transform(pred)
  • Related