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')
  • $\begingroup$ what line throws the error? $\endgroup$ – Peter Jun 1 '19 at 14:58
  • 1
    $\begingroup$ You can try, lin_reg.predict([[6.5]]) $\endgroup$ – Hassan Oukhouya Aug 26 '19 at 11:38

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,


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))

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