【问题标题】:Multiple Linear Regression with constraints on coefficients [closed]对系数有约束的多元线性回归[关闭]
【发布时间】:2020-04-19 21:47:06
【问题描述】:

我正在尝试使用

实现多元线性回归

sklearn.linear_model.LinearRegression 函数。

回归方程为:

y = c + a1x1 + a2x2

还有一个附加条件:

  • c 总是取一个介于 (1,10) 之间的值
  • a1 取 (0,1) 之间的值。

我如何主要使用 Python 解决这些方程?

【问题讨论】:

  • 请给我们更多关于您的问题和数据的背景信息,因为您的问题很笼统。
  • @sentence 请查看我对这个问题的回答,这是一个示例有界 3D 曲面拟合器。我个人发现问题陈述足以在没有用户数据的情况下做出回答。
  • @JamesPhillips 感谢您提供示例。不过,我看不出您的回答如何满足使用 sklearn 的需要(否则问题确实很笼统)。
  • @sentence 请重新阅读所提出的实际问题。

标签: python scikit-learn constraints regression linear-regression


【解决方案1】:

这是一个 Python 中的有界图形 3D 曲面拟合器示例,它使用您的方程和 c 上的边界以及 3D 散点图、3D 曲面图和等高线图。您应该能够用鼠标单击拖动并在 3 空间中旋转 3D 图以进行检查。您当然可以根据需要更改或添加边界。

请注意,此示例使用 scipy 的 curve_fit() 允许拟合参数的界限,并且估计的初始参数必须在界限内,以便 curve_fit() 可以开始。在本例中,a1、a2 和 c 的拟合参数值为:

拟合参数 [9.71206053e-01 3.57603742e-02 1.63260453e-16]

参数“c”有效地位于零的下限。如果您从对 curve_fit() 的调用中删除边界,则在此示例中参数“c”将为负数。

import numpy, scipy, scipy.optimize
import matplotlib
from mpl_toolkits.mplot3d import  Axes3D
from matplotlib import cm # to colormap 3D surfaces from blue to red
import matplotlib.pyplot as plt

graphWidth = 800 # units are pixels
graphHeight = 600 # units are pixels

# 3D contour plot lines
numberOfContourLines = 16


def SurfacePlot(func, data, fittedParameters):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

    matplotlib.pyplot.grid(True)
    axes = Axes3D(f)

    x_data = data[0]
    y_data = data[1]
    z_data = data[2]

    xModel = numpy.linspace(min(x_data), max(x_data), 20)
    yModel = numpy.linspace(min(y_data), max(y_data), 20)
    X, Y = numpy.meshgrid(xModel, yModel)

    Z = func(numpy.array([X, Y]), *fittedParameters)

    axes.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=1, antialiased=True)

    axes.scatter(x_data, y_data, z_data) # show data along with plotted surface

    axes.set_title('Surface Plot (click-drag with mouse)') # add a title for surface plot
    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label
    axes.set_zlabel('Z Data') # Z axis data label

    plt.show()
    plt.close('all') # clean up after using pyplot or else there can be memory and process problems


def ContourPlot(func, data, fittedParameters):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    axes = f.add_subplot(111)

    x_data = data[0]
    y_data = data[1]
    z_data = data[2]

    xModel = numpy.linspace(min(x_data), max(x_data), 20)
    yModel = numpy.linspace(min(y_data), max(y_data), 20)
    X, Y = numpy.meshgrid(xModel, yModel)

    Z = func(numpy.array([X, Y]), *fittedParameters)

    axes.plot(x_data, y_data, 'o')

    axes.set_title('Contour Plot') # add a title for contour plot
    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label

    CS = matplotlib.pyplot.contour(X, Y, Z, numberOfContourLines, colors='k')
    matplotlib.pyplot.clabel(CS, inline=1, fontsize=10) # labels for contours

    plt.show()
    plt.close('all') # clean up after using pyplot or else there can be memory and process problems


def ScatterPlot(data):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

    matplotlib.pyplot.grid(True)
    axes = Axes3D(f)
    x_data = data[0]
    y_data = data[1]
    z_data = data[2]

    axes.scatter(x_data, y_data, z_data)

    axes.set_title('Scatter Plot (click-drag with mouse)')
    axes.set_xlabel('X Data')
    axes.set_ylabel('Y Data')
    axes.set_zlabel('Z Data')

    plt.show()
    plt.close('all') # clean up after using pyplot or else there can be memory and process problems


def func(data, a1, a2, c):
    x1 = data[0]
    x2 = data[1]
    return c + (a1 * x1) + (x2 * a2)


if __name__ == "__main__":
    xData = numpy.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
    yData = numpy.array([11.0, 12.1, 13.0, 14.1, 15.0, 16.1, 17.0, 18.1, 90.0])
    zData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.0, 12.0])

    data = [xData, yData, zData]

    # initial parameter estimates must be within bounds
    initialParameters = [1.0, 1.0, 0.5]

    # bounds on parameters - initial parameters must be within these
    # note that +/- infinity means "no bound" on that parameter
    lowerBounds = (-numpy.Inf, -numpy.Inf, 0.0)
    upperBounds = (numpy.Inf, numpy.Inf, 1.0)
    parameterBounds = [lowerBounds, upperBounds]

    # now call curve_fit passing in parameter bounds
    fittedParameters, pcov = scipy.optimize.curve_fit(func, [xData, yData], zData, p0 = initialParameters, bounds = parameterBounds)

    ScatterPlot(data)
    SurfacePlot(func, data, fittedParameters)
    ContourPlot(func, data, fittedParameters)

    print('fitted parameters', fittedParameters)

    modelPredictions = func(data, *fittedParameters) 

    absError = modelPredictions - zData

    SE = numpy.square(absError) # squared errors
    MSE = numpy.mean(SE) # mean squared errors
    RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
    Rsquared = 1.0 - (numpy.var(absError) / numpy.var(zData))
    print('RMSE:', RMSE)
    print('R-squared:', Rsquared)

【讨论】:

    猜你喜欢
    • 2018-10-28
    • 2014-01-30
    • 2013-09-05
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 2016-08-17
    • 2021-01-02
    • 2018-06-19
    相关资源
    最近更新 更多