Skip to content Skip to sidebar Skip to footer

Wrong Polynomial Regression Plot

I am trying to use linear and polynomial regression for the data. import numpy as np import matplotlib.pyplot as plt from sklearn import linear_model, cross_validation from sklearn

Solution 1:

If I've understood you correctly, just add a sorting to your x_test before passing it to predict() function and increase the degree of polynomial to 3:

poly_model = make_pipeline(PolynomialFeatures(degree=3), linear_model.LinearRegression())

and

x_test.sort(axis=0)

with these adjustments I'm getting following plot: enter image description here

Solution 2:

1) You can just call plot twice, it will add new line to existing plot. eg:

ax.plot(x_test, model1.predict(x_test), color='red', linewidth=2)
ax.plot(x_test, model2.predict(x_test), color='green', linewidth=2)

In your case I'd do sth like that:

linear_model = linear_model.LinearRegression(fit_intercept=False)
poly_model = model = Pipeline([('poly', PolynomialFeatures(degree=2)),
                   ('linear', linear_model.LinearRegression(fit_intercept=False))])

linear_model.fit(x_train, y_train)
poly_model.fit(x_train, y_train)

And then:

ax.plot(x_test, linear_model.predict(x_test), color='red', linewidth=2, label='linear')
ax.plot(x_test, poly_model.predict(x_test), color='green', linewidth=2, label='poly')
ax.legend()

Post a Comment for "Wrong Polynomial Regression Plot"