Issue
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?
Solution
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)
Answered By - MaximeKan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.