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))
lin_reg.predict([[6.5]])
$\endgroup$