user10679526
user10679526

Reputation: 109

Polynomial Regression with weights

For fitting I am using sklearn LinearRegression with weighted values:

from sklearn.linear_model import LinearRegression

regr = LinearRegression()
regr.fit(x, y, w)

plt.scatter(x, y,  color='black')
plt.plot(x, regr.predict(x), color='blue', linewidth=3)

However, now my data is not showing a linear behaviour, but a quadratic. So I tried to implement the polynomicFeatures:

from sklearn.preprocessing import PolynomialFeatures

regr = PolynomialFeatures()
regr.fit(x, y, w)

plt.scatter(x, y,  color='black')
plt.plot(x, regr.predict(x), color='blue', linewidth=3)

However, the PolynomialFeatures fit function seems not to allowing a sample_weight parameter in its fit-function. Is there a way to use weights with polynomial fits anyway?

Upvotes: 2

Views: 1155

Answers (1)

MaximeKan
MaximeKan

Reputation: 4221

PolynomialFeatures is not a regression, it is just the preprocessing function that carries out the polynomial transformation of your data. You then need to plug it into your linear regression as usual.

from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

poly = PolynomialFeatures()
reg = LinearRegression()

x_poly = poly.fit_transform(x)
reg.fit(x_poly, y, w)

plt.scatter(x, y,  color='black')
plt.plot(x, reg.predict(x_poly), color='blue', linewidth=3)

Upvotes: 3

Related Questions