多项式图上的直线和曲线太多
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")
我曾尝试绘制多项式回归曲线,但无法完成。有人告诉我在通过“numpy.argsort”绘图之前对值进行排序,但没有任何改变。我该如何解决?
可能分散更适合你:
plt.scatter(x,y_pred,color="red")
或如前所述 argsort
:
orders = np.argsort(x.ravel())
plt.plot(x[orders], y[orders], color='red')
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")
我曾尝试绘制多项式回归曲线,但无法完成。有人告诉我在通过“numpy.argsort”绘图之前对值进行排序,但没有任何改变。我该如何解决?
可能分散更适合你:
plt.scatter(x,y_pred,color="red")
或如前所述 argsort
:
orders = np.argsort(x.ravel())
plt.plot(x[orders], y[orders], color='red')