问题描述
我正在使用 quadprog 模块来设置 SVM 以进行语音识别。我从这里获取了 QP 实现:https://github.com/stephane-caron/qpsolvers/blob/master/qpsolvers/quadprog_.py
这是他们的实现:
def quadprog_solve_qp(P,q,G=None,h=None,A=None,b=None,initvals=None,verbose=False):
if initvals is not None:
print("quadprog: note that warm-start values ignored by wrapper")
qp_G = P
qp_a = -q
if A is not None:
if G is None:
qp_C = -A.T
qp_b = -b
else:
qp_C = -vstack([A,G]).T
qp_b = -np.insert(h,axis=0)
meq = A.shape[0]
else: # no equality constraint
qp_C = -G.T if G is not None else None
qp_b = -h if h is not None else None
meq = 0
try:
return solve_qp(qp_G,qp_a,qp_C,qp_b,meq)[0]
except ValueError as e:
if "matrix G is not positive definite" in str(e):
# quadprog writes G the cost matrix that we write P in this package
raise ValueError("matrix P is not positive definite")
raise
形状:
P: (127,127)
h: (254,1)
q: (127,1)
A: (1,127)
G: (254,127)
我也有 qp_b
最初被分配给一个 hstack
的数组 arr = array([0])
和 h
但形状:(1,)
阻止了 numpy 连接数组。我通过插入 [0] 来修正这个错误。
当我尝试 quadprog_solve_qp(P,G,h,A)
时,我得到:
File "----------------------------.py",line 95,in quadprog_solve_qp
return solve_qp(qp_G,meq)[0]
File "quadprog/quadprog.pyx",line 12,in quadprog.solve_qp
ValueError: Buffer has wrong number of dimensions (expected 1,got 2)
我不知道它来自哪里,也不知道我能做什么。如果有人知道 quadprog 模块的工作原理,或者我可能做错了什么,我会很高兴听到。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)