无法正确拟合多项式回归线

Unable to fit polynomial regression line correctly

我有如下数据框:

price |  Sales
6.62  |  64.8
8.71  |  38

看起来像这样

我对非线性回归不是很熟悉,但是根据一些教程,我使用以下代码来拟合多项式分布:

X = df['price'].values
y = df['sales'].values

X = X.reshape(-1,1)

from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

pre_process = PolynomialFeatures(degree=2)
X_poly = pre_process.fit_transform(X)

pr_model = LinearRegression()
# Fit our preprocessed data to the polynomial regression model
pr_model.fit(X_poly, y)
# Store our predicted Humidity values in the variable y_new
y_pred = pr_model.predict(X_poly)
# Plot our model on our data
plt.scatter(X, y, c = "black")
plt.plot(X, y_pred)

我理解错了:

有什么我想念的我无法获得合适的线条的想法吗?

预测正确:

X = np.random.uniform(0,1,100)
y = 3*X**2 + 2*X - 8 + np.random.normal(0,1,100)

X = X.reshape(-1,1)

from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

pre_process = PolynomialFeatures(degree=2)
X_poly = pre_process.fit_transform(X)

pr_model = LinearRegression()
pr_model.fit(X_poly, y)

y_pred = pr_model.predict(X_poly)

plt.scatter(X, y, c = "black")
plt.scatter(X, y_pred, c="orange")

要绘制一条线,您需要对 x 值进行排序:

plt.scatter(X, y, c = "black")
x_sorted = np.sort(X,axis=0)
y_pred_sorted = pr_model.predict(pre_process.fit_transform(x_sorted))
plt.plot(x_sorted,y_pred_sorted,c="orange")