在SCIPY线性规划方案中缩放矩阵变量的最佳方法 [英] Best way to scale the matrix variables in SCIPY linear programming scheme
问题描述
I have the following optimization scheme implemented under NNLS in scipy.
import numpy as np
from scipy.optimize import nnls
from scipy import stats
#Define problem
A = np.array([[60., 90., 120.],
[30., 120., 90.]])
b = np.array([6700.5, 699.,])
# Add ones to ensure the solution sums to 1
b = np.hstack([b,1.0])
A = np.vstack([A,np.ones(3)])
x, rnorm = nnls(A,b)
print x
# the solution is
# [ 93.97933792 0. 0. ]
# we expect it to sum to 1 if it's not skewed
如您所见,b
向量远高于A
中的值.
我的问题是缩放A
和b
的最佳/合理方法是什么,以便解决方案
不偏斜.
As you can see the b
vector is much higher than values in A
.
My question is what's the best/reasonable way to scale A
and b
so that the solution
is not skewed.
请注意,A
和b
都是未经预处理的基因表达原始数据.
Note that both A
and b
are gene expression raw data without pre-processing.
推荐答案
如果要包含相等约束,则不能真正使用nnls例程,因为它不能满足相等性要求.如果您只限于scipy提供的功能,则可以使用以下方法:
If you want to include the equality constraint, you can't really use the nnls routine, since it doesn't cater for equalities. If you are limited to what's on offer in scipy, you can use this:
import numpy as np
from scipy.optimize import minimize
#Define problem
A = np.array([[60., 90., 120.],
[30., 120., 90.]])
b = np.array([6700.5, 699.,])
#-----------------------------
# I tried rescaling the data by adding this two lines,
# so that they're in same scale.
# but why the solution is different?
# x: array([ 1., 0., 0.])
# What's the correct way to go?
#-----------------------------
# A = A/np.linalg.norm(A,axis=0)
# b = b/np.linalg.norm(b)
def f(x):
return np.linalg.norm(A.dot(x) - b)
cons ={'type': 'eq',
'fun': lambda x: sum(x) - 1}
x0 = [1, 0, 0] # initial guess
minimize(f, x0, method='SLSQP', bounds=((0, np.inf),)*3, constraints=cons)
输出:
status: 0
success: True
njev: 2
nfev: 10
fun: 6608.620222860367
x: array([ 0., 0., 1.])
message: 'Optimization terminated successfully.'
jac: array([ -62.50927734, -100.675354 , -127.78314209, 0. ])
nit: 2
这将直接最小化目标函数,同时还施加了您感兴趣的相等约束.
This minimises the objective function directly while also imposing the equality constraint you're interested in.
如果速度很重要,则可以添加jacobian和hessian信息,甚至更好,请使用由 cvxopt .
If speed is important, you can add the jacobian and hessian information, or even better, use a proper QP solver, as supplied by cvxopt.
这篇关于在SCIPY线性规划方案中缩放矩阵变量的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!