0

i am trying to do a polynomial fit to my data and the output is a linear fit. i am trying to understand where my mistake is.

regr2 = PolynomialFeatures(degree=2)
regr2.fit_transform(diabetes_X_train, diabetes_y_train)
regr2 = PolynomialFeatures(interaction_only=True)
regr2.fit_transform(diabetes_X_train, diabetes_y_train)

regr = LinearRegression()
regr.fit(diabetes_X_train, diabetes_y_train)

diabetes_y_pred = regr.predict(diabetes_X_test)

plt.scatter(diabetes_X_test, diabetes_y_test,  color='blue')
plt.plot(diabetes_X_test, diabetes_y_pred, color='red', linewidth=1)

1 Answer 1

1

It's doing it because you told it to.

regr = LinearRegression()

You are plotting "diabetes_y_pred," which comes from "regr.fit" and regr is defined above with "LinearRegression()."

https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html

Sign up to request clarification or add additional context in comments.

4 Comments

Hi,Thanks for the help. How do i fix it?
Don't use scikit-learn much. My guess is change diabetes_y_pred = regr.predict(diabetes_X_test) to diabetes_y_pred = regr2.predict(diabetes_X_test).
AttributeError: 'PolynomialFeatures' object has no attribute 'predict' getting this error when i run that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.