首页 文章

Python sklearn poly回归

提问于
浏览
2

我现在坚持解决这个问题两天了 . 我有一些数据点,我放在 scatter plot 并得到这个:

enter image description here

哪个好,但是现在我也想添加一个回归线,所以我从sklearn看了一下example并将代码更改为此

import numpy as np
import matplotlib.pyplot as plt
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score

degrees = [3, 4, 5]
X = combined[['WPI score']]
y = combined[['CPI score']]

plt.figure(figsize=(14, 5))
for i in range(len(degrees)):
    ax = plt.subplot(1, len(degrees), i + 1)
    plt.setp(ax, xticks=(), yticks=())

    polynomial_features = PolynomialFeatures(degree=degrees[i], include_bias=False)
    linear_regression = LinearRegression()
    pipeline = Pipeline([("polynomial_features", polynomial_features), ("linear_regression", linear_regression)])
    pipeline.fit(X, y)

    # Evaluate the models using crossvalidation
    scores = cross_val_score(pipeline, X, y, scoring="neg_mean_squared_error", cv=10)

    X_test = X #np.linspace(0, 1, len(combined))
    plt.plot(X, pipeline.predict(X_test), label="Model")
    plt.scatter(X, y, label="CPI-WPI")
    plt.xlabel("X")
    plt.ylabel("y")
    plt.legend(loc="best")
    plt.title("Degree {}\nMSE = {:.2e}(+/- {:.2e})".format(degrees[i], -scores.mean(), scores.std()))
plt.savefig(pic_path + 'multi.png', bbox_inches='tight')
plt.show()

其输出如下:

enter image description here

请注意 Xy 都是 DataFrames ,大小为 (151, 1) . 如有必要,我也可以发布X和y的内容 .

我想要的是一条很流畅的线条,但我似乎无法弄明白,怎么做 .

[Edit]

这里的问题是:如何获得单个平滑,弯曲的多项式线而不是具有看似随机模式的多个线 .

[Edit 2]

问题是,当我像这样使用 linspace 时:

X_test = np.linspace(1, 4, 151)
X_test = X_test[:, np.newaxis]

我得到一个更随机的模式:

enter image description here

1 回答

  • 1

    诀窍是设置如下代码:

    X_test = np.linspace(min(X['GPI score']), max(X['GPI score']), X.shape[0])
    X_test = X_test[:, np.newaxis]
    plt.plot(X_test, pipeline.predict(X_test), label="Model")
    

    产生以下结果(更好,单一平滑线)

    Model with degree 1 to 6

相关问题