0
$\begingroup$
Error:Expected 2D array, got scalar array instead: array=6.5. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains

My code:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler

Data_Set_Persentage=100
Train_Data_Persentage=90

dataset = pd.read_csv('energydata_complete.csv')

dataset_train=dataset
dataset = dataset.iloc[:4933,:]
dataset_train=dataset_train.iloc[:4933,:]
NumAllData=len(dataset_train)
NumAllData=int((NumAllData*Data_Set_Persentage)/100)
#print(NumAllData)
TRP=Train_Data_Persentage       #Train Data Persentage
TEP=Data_Set_Persentage-TRP   #Test Data Persentage
TRDL=int((NumAllData*TRP)/Data_Set_Persentage)            #Train Data Lenth Number
#print(TRDL)
TEDL=int(NumAllData-TRDL)                   #Test Data Lenth Number
#print(TEDL)

X=dataset_train.iloc[0:TRDL,2:26]
y=dataset_train.iloc[0:TRDL,1:2]

sc=MinMaxScaler(feature_range=(-1,1))
sc_X = MinMaxScaler()
sc_y = MinMaxScaler()

X=sc_X.fit_transform(X)
#print(training_set_scaled_x)
y=sc_y.fit_transform(y)
#print(training_set_scaled_y)

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

y_pred = regressor.predict(6.5)
y_pred=regressor.predict(y.fit_transform([[6.5]]))

y_pred = sc_y.inverse_transform(y_pred) 

X_grid = np.arange(min(X), max(X), 0.01) #this step required because data is feature scaled.
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y, color = 'red')
plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')
plt.title('Truth or Bluff (SVR)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()
$\endgroup$
2
  • $\begingroup$ Which line of code is getting the error? y_pred = regressor.predict(6.5)? $\endgroup$
    – Lynn
    Jul 31 at 5:43
  • 1
    $\begingroup$ Amir, could you please guide how to create title of your question? Also, please use code formatting, this is a button near "make bold text" so your code become more readable $\endgroup$ Jul 31 at 10:00

0

Your Answer

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

Browse other questions tagged or ask your own question.