cavuscanay
cavuscanay

Reputation: 27

Too many lines and curves on the polynomial graph

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt    
from sklearn.linear_model import LinearRegression
from sklearn import metrics
from sklearn.preprocessing import PolynomialFeatures

df = pd.read_csv("C:\\Users\\MONSTER\\Desktop\\dosyalar\\datasets\\Auto.csv")
x = df["horsepower"].to_numpy()
y = df["mpg"].to_numpy()

x = x.reshape(-1,1)

poly = PolynomialFeatures(degree = 5)
X_poly = poly.fit_transform(x)
poly.fit(X_poly,y)

lr = LinearRegression()
lr.fit(X_poly, y)
y_pred = lr.predict(X_poly)

plt.scatter(x,y,color="blue",marker=".")
plt.plot(x,y_pred,color="red")

I have tried to draw a polynomial regression curve but I couldn't manage it. Someone told me to sorting values before plotting via "numpy.argsort" but nothing has changed. How can I fix it?

Here is the result

Upvotes: 1

Views: 1210

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

probably scatter is better for you:

plt.scatter(x,y_pred,color="red")

Or with argsort as mentioned:

orders = np.argsort(x.ravel())

plt.plot(x[orders], y[orders], color='red')

Upvotes: 3

Related Questions