1

I made a dataset of 5 points. so I want to use PolynomialFeatures to fit a single curve but it gives me multiple curves. can you help me with this? Here is my code:

import numpy as np 
import matplotlib. pyplot as plt
from sklearn.preprocessing import PolynomialFeatures 
from sklearn.linear_model import LinearRegression

x=np.random.normal(size=5)
y= 2.2 * x - 1.1
y= y + np.random.normal(scale=3, size=y.shape)
x=x.reshape(-1,1)

preproc=PolynomialFeatures(degree=4)
x_poly=preproc.fit_transform(x)

x_line=np.linspace(-2,2,100)
x_line=x_line.reshape(-1,1)
poly_line=PolynomialFeatures(degree=4)
y_line=poly_line.fit_transform(x_line)

plt.plot(x,y,"bo")
plt.plot(x_line,y_line,"r.")
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Mo.be
  • 95
  • 2
  • 11
  • `PolynomialFeatures` alone doesn't fit a polynomial curve. There answers here show a couple of ways of doing it. https://stackoverflow.com/questions/32660231/how-to-fit-a-polynomial-curve-to-data-using-scikit-learn – bogovicj Feb 20 '21 at 16:38

0 Answers0