首页 > 解决方案 > scipy:如何在约束条件下最小化最小残差平方和?

问题描述

当使用普通最小二乘线性回归方法拟合xy时,它会得到一个函数y = a*x + b,但在我的情况下我需要做b <= 0

x = [139, 162, 147, 110, 145, 144, 131, 132, 135, 85, 77, 90, 103, 163, 102, 31, 71, 95, 143, 94, 81, 82, 79, 64, 102, 122, 130, 117, 109, 46, 26, 26, 36, 105, 135, 14, 63, 93, 75, 128, 86, 107, 137, 49, 99, 96, 112, 124, 129, 68, 111, 128, 11, 13, 63, 104, 127, 98, 95, 85, 87, 98, 76, 119, 69, 103, 86, 87, 91, 90, 100, 102, 105, 116, 127, 127, 82, 126, 99, 116, 61, 64, 83, 85, 83, 83, 82, 97, 105, 116, 130, 91, 120, 102, 124, 66, 79, 83, 82, 80, 80, 91, 91, 122, 99, 126, 49, 92, 76, 80, 97, 107, 109, 117, 62, 120, 111, 75, 130, 75, 105, 118, 115, 97, 62, 123, 98, 57, 109, 104, 51, 102, 60, 65, 113, 117, 18, 83, 88, 93, 31, 57, 39, 25, 37, 26, 27, 36, 109, 113, 68, 100, 110, 108, 109, 109, 96, 61, 55, 78, 97, 115, 109, 105, 107, 50, 98, 99, 89, 68, 107, 112, 119, 126, 117, 112, 104, 72, 38, 35, 30, 82, 101, 92, 98, 103, 97, 85, 37, 33, 73, 56, 49, 100, 63, 24]

y = [151, 170, 53, 182, 188, 181, 137, 186, 149, 76, 149, 156, 168, 147, 102, 107, 132, 150, 148, 57, 109, 110, 87, 151, 163, 152, 109, 131, 85, 63, 73, 102, 129, 155, 49, 89, 151, 157, 126, 136, 72, 150, 78, 142, 45, 171, 127, 132, 67, 139, 131, 73, 77, 34, 162, 131, 159, 61, 132, 100, 116, 106, 152, 164, 127, 104, 125, 100, 111, 122, 140, 139, 139, 160, 159, 91, 159, 150, 136, 122, 22, 100, 109, 108, 116, 115, 115, 147, 169, 167, 51, 143, 100, 145, 110, 89, 103, 98, 99, 102, 140, 153, 156, 88, 178, 168, 89, 105, 27, 152, 154, 60, 179, 129, 122, 146, 44, 157, 91, 142, 118, 155, 138, 72, 140, 146, 59, 168, 138, 74, 108, 121, 28, 141, 159, 172, 69, 112, 126, 103, 127, 95, 86, 79, 65, 43, 66, 71, 155, 139, 132, 139, 139, 161, 141, 73, 122, 97, 126, 77, 156, 141, 161, 169, 145, 116, 148, 141, 83, 138, 94, 120, 137, 149, 164, 139, 116, 94, 92, 79, 79, 134, 129, 110, 130, 138, 156, 129, 74, 128, 104, 50, 127, 107, 48, 58]

# Define the Model
f = lambda x, a, b: a * x + b

# The objective Function to minimize (least-squares regression)
obj = lambda x, y, a, b: np.sum(np.abs(y - f(x, a, b))**2)

我不确定fandobj是否正确。如何定义使b小于零的约束条件?

标签: pythonscipyscipy-optimize-minimize

解决方案


就像评论中已经提到的 sascha 一样,不需要这个abs函数,因为(y - f(x,a,b))**2它总是积极的。通过使用scipy.optimize.minimize,您可以这样做:

from scipy.optimize import minimize
import numpy as np

# x = np.array([139, ...])
# y = np.array([151, ...])

# Define the Model
def f(x, a, b): return a * x + b

# The objective Function to minimize (least-squares regression)
def obj(x, y, a, b): return np.sum((y - f(x, a, b))**2)

# define the bounds -infty < a < infty,  b <= 0
bounds = [(None, None), (None, 0)]

# res.x contains your coefficients
res = minimize(lambda coeffs: obj(x, y, *coeffs), x0=np.zeros(2), bounds=bounds)

推荐阅读