13

Can anyone help me with this error. I did the following code but it does not work and I am getting the following error:

ValueError: 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 a single sample. 

My code:

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

import pandas
dataset = pandas.read_excel('PEG RATIOS.xlsx')

X = dataset.iloc[:, 2].values
X =X.reshape(-1,1)
y = dataset.iloc[:, 3].values
y = y.reshape (-1,1)


from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X, y)

from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4)
X_poly = poly_reg.fit_transform(X)
poly_reg.fit(X_poly, y)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(X_poly, y)

X_grid = np.arange(min(X), max(X), 0.1)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y, color = 'red')
plt.plot(X_grid, lin_reg_2.predict(poly_reg.fit_transform(X_grid)), color = 'blue')
plt.title('PEG Ratios verrus Exoected Growth: Semiconductor Firms')
plt.xlabel('Expected Growth rate')
plt.ylabel('PEGH Ratio')
plt.show()
lin_reg_2.predict(poly_reg.fit_transform(6.5))
Ethan
  • 1,633
  • 9
  • 24
  • 39
Siri1993
  • 131
  • 1
  • 1
  • 3

1 Answers1

8

The error itself solves your problem. Just follow what it says. The predict() method takes a 2d array of values you want to predict on. Each item in the array is a "point" you want your model to predict on. So try,

lin_reg_2.predict(poly_reg.fit_transform([[6.5]]))

Here the input is a 2D array of shape (1,1).

Or as the error suggests:

lin_reg_2.predict(np.array([6.5]).reshape(1, 1))
bkshi
  • 2,235
  • 2
  • 11
  • 23