【问题标题】:Polynomial Regression with weights带权重的多项式回归
【发布时间】:2021-07-11 05:03:50
【问题描述】:

为了拟合,我正在使用带有加权值的 sklearn LinearRegression:

from sklearn.linear_model import LinearRegression

regr = LinearRegression()
regr.fit(x, y, w)

plt.scatter(x, y,  color='black')
plt.plot(x, regr.predict(x), color='blue', linewidth=3)

但是,现在我的数据显示的不是线性行为,而是二次行为。所以我尝试实现 polynomicFeatures:

from sklearn.preprocessing import PolynomialFeatures

regr = PolynomialFeatures()
regr.fit(x, y, w)

plt.scatter(x, y,  color='black')
plt.plot(x, regr.predict(x), color='blue', linewidth=3)

但是,PolynomialFeatures 拟合函数似乎不允许在其拟合函数中使用 sample_weight 参数。有没有办法使用多项式拟合的权重?

【问题讨论】:

    标签: scikit-learn


    【解决方案1】:

    PolynomialFeatures 不是回归,它只是执行数据多项式变换的预处理函数。然后,您需要像往常一样将其插入线性回归中。

    from sklearn.linear_model import LinearRegression
    from sklearn.preprocessing import PolynomialFeatures
    
    poly = PolynomialFeatures()
    reg = LinearRegression()
    
    x_poly = poly.fit_transform(x)
    reg.fit(x_poly, y, w)
    
    plt.scatter(x, y,  color='black')
    plt.plot(x, reg.predict(x_poly), color='blue', linewidth=3)
    

    【讨论】:

      猜你喜欢
      • 2020-05-08
      • 2020-01-10
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 2011-04-23
      • 2019-01-24
      • 1970-01-01
      • 2018-12-19
      相关资源
      最近更新 更多