问题描述
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')