多项式图上的直线和曲线过多

问题描述

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”进行绘图之前先对值进行排序,但没有任何改变。我该如何解决?

Here is the result

解决方法

可能散布对您更好:

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

或如上所述使用argsort

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...