I am trying to get a predicted value instead of whole features for a particular level using predict method.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#Importing Dataset
dataset = pd.read_csv('C:/Users/Rupali Singh/Desktop/ML A-Z/Machine Learning A-Z Template Folder/Part 2 - Regression/Section 7 - Support Vector Regression (SVR)/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.reshape(-1,1))
#Fitting SVR model to dataset

from sklearn.svm import SVR

regressor = SVR(kernel='rbf')

#Visualizing the dataset

plt.scatter(X, Y, color = 'red')
plt.plot(X, regressor.predict(X), color = 'blue')

# Predicting a new Result

Y_pred = regressor.predict(6.5)

This is my dataset, here I am trying to predict value only for level 6

Position  Level   Salary
0   Business Analyst      1    45000
1  Junior Consultant      2    50000
2  Senior Consultant      3    60000
3            Manager      4    80000
4    Country Manager      5   110000
5     Region Manager      6   150000
6            Partner      7   200000
7     Senior Partner      8   300000
8            C-level      9   500000
9                CEO     10  1000000

This is the error message I am getting:

File "C:/Users/Rupali Singh/PycharmProjects/Machine_Learning/SVR.py", line 34, in <module>
    Y_pred = regressor.predict(6.5)
  File "C:\Users\Rupali Singh\PycharmProjects\Machine_Learning\venv\lib\site-packages\sklearn\svm\base.py", line 322, in predict
    X = self._validate_for_predict(X)
  File "C:\Users\Rupali Singh\PycharmProjects\Machine_Learning\venv\lib\site-packages\sklearn\svm\base.py", line 454, in _validate_for_predict
  File "C:\Users\Rupali Singh\PycharmProjects\Machine_Learning\venv\lib\site-packages\sklearn\utils\validation.py", line 514, in check_array
    "if it contains a single sample.".format(array))
ValueError: Expected 2D array, got scalar 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.

I would be really grateful for any kind of help.



Y_pred = regressor.predict(np.array([6.5]).reshape(1, 1))

Scikit does not work with scalars (just one single value). It expects a shape $(m\times n)$ where $m$ is the number of features and $n$ is the number of observations, both are 1 in your case.

  • 1
    $\begingroup$ It worked. Thank you so much. $\endgroup$ – Rupali Singh May 30 '19 at 9:48
Y_pred = regressor.predict([[6.5]])
  • $\begingroup$ This answer would be much more useful if you explained "Why?" and "How?". $\endgroup$ – Stephen Rauch Apr 23 '20 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.