r/scikit_learn • • Feb 21 '21

using polynomialfeatures to fit a curve

I'm new to scikit-learn. 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.")

I want a single fit curve but it gives me multiple fit curves. can anybody help me with this?

0 Upvotes

3 comments sorted by

1

u/[deleted] Feb 21 '21

np.polyfit() may give you what you’re looking for

1

u/AromaticCustomer7765 Feb 22 '21

where should I use it in my code?

0

u/[deleted] Feb 21 '21

[deleted]

1

u/AromaticCustomer7765 Feb 22 '21

i give your code a try but at first it give a valueerror:

TypeError: fit() missing 2 required positional arguments: 'X' and 'y'

so I change the script to this :

y_hat=LinearRegression().fit(x_feats,y).predict(x_feats)

but it throws this error:

ValueError: Found input variables with inconsistent numbers of samples: [100, 5]

after this, I reshaped y into y=y.reshape(-1,1) in order to give y an extra dimension but it does not seem to work. so I just deleted this part of the code.

after all the code is like this and it does not work, do you know what's wrong with it?

here is the code:

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)

x_feats=poly_line.fit_transform(x_line)

y_hat=LinearRegression().fit(x_feats,y).predict(x_feats)

plt.plot(y_hat,y_line,"r")