Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

numpy - How do I correct this Value Error due to Buffer having the wrong dimensions in a quadprog SVM implementation?

I'm using the quadprog module to set up an SVM for speech recognition. I took a QP implementation from here: https://github.com/stephane-caron/qpsolvers/blob/master/qpsolvers/quadprog_.py

Here is their implementation:

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, 0, 0, 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

Shapes:

P: (127, 127)
h: (254, 1)
q: (127, 1)
A: (1, 127)
G: (254, 127)

I also had that qp_b was initially assigned to an hstack of an array arr = array([0]) with h but the shape: (1,) prevented numpy from concatenating the arrays. I fixed this error by inserting a [0] instead.

When I try quadprog_solve_qp(P, q, G, h, A) I get a:

File "----------------------------.py", line 95, in quadprog_solve_qp
    return solve_qp(qp_G, qp_a, qp_C, qp_b, meq)[0]

  File "quadprog/quadprog.pyx", line 12, in quadprog.solve_qp

ValueError: Buffer has wrong number of dimensions (expected 1, got 2)

And I have no idea where it's coming from, nor what I can do. If anyone has any idea how the quadprog module works or simply what I might be doing wrong I would be pleased to hear.

question from:https://stackoverflow.com/questions/65946488/how-do-i-correct-this-value-error-due-to-buffer-having-the-wrong-dimensions-in-a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...