Question: import quadprog import numpy as np # The helper function. Dot not change it def quadprog_solve_qp(P, q, G=None, h=None, A=None, b=None): qp_G = .5 *



![= -q if A is not None: qp_C = -np.vstack([A, G]).T qp_b](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f2f6e2be38b_94666f2f6e24ccb4.jpg)
import quadprog import numpy as np
# The helper function. Dot not change it def quadprog_solve_qp(P, q, G=None, h=None, A=None, b=None): qp_G = .5 * (P + P.T) # make sure P is symmetric qp_a = -q if A is not None: qp_C = -np.vstack([A, G]).T qp_b = -np.hstack([b, h]) meq = A.shape[0] else: # no equality constraint qp_C = -G.T qp_b = -h meq = 0 return quadprog.solve_qp(qp_G, qp_a, qp_C, qp_b, meq)[0]
# Toy data X = np.array([ [0, 0], [2, 0], [0, 2], [3, 3], [4, 4] ]) Y = np.array([-1, -1, -1, 1, 1])
Q = np.zeros((5, 5))
### START YOUR CODE ### for i in range(Q.shape[0]): for j in range(Q.shape[1]): # Use the ith and jth examples in X and Y to compute Q_ij # Hint: Q_ij = y^i * y^j * (x^i @ x^j) Q[i, j] = Y[i] * Y[j] * np.dot(X[i], X[j]) ### END YOUR CODE ###
print('Q = ', Q)
### START YOUR CODE ### P = Q + np.eye(5)*1e-5 # To solve the non-positive finite issue
# Hint: Use np.ones(), q is of length 5 q = 1*np.ones(5)
# Hint: G is a matrix whose diagnal elements are 1s, and other elements are 0s. Use np.eye() G = -1*np.eye(5)
# Hint: h is of length 5, with all zeros; Use np.zeros() h = np.zeros(5)
A = Y.reshape((1,5))
# Hint: b is of length 1, with zero value; Use np.zeros() b = np.zeros(1)
### END YOUR CODE ###
print('q = ', q) print('G = ', G) print('h = ', h) print('b = ', b)
### START YOUR CODE ###
# Hint: Call quadprog_solve_qp() with the correct arguments solution = None
### END YOUR CODE ###
print('solution = ', solution) print('The support vectors are: ', X[solution > 0, ])
\#\#\# START YOUR CODE \#\#\# \# Hint: Call quadprog_solve_qp() with the correct arguments solution = None \#\#\# END YOUR CODE \#\#\# print('solution = ', solution) print('The support vectors are: ', X[ solution >,]) solution = None TypeError Traceback (most recent call last) Input In [15], in () 6 \#\#\# END YOUR CODE \#\#\# 8 print('solution = ', solution) >9 print('The support vectors are: ', X[ solution >,]) TypeError: ' > ' not supported between instances of 'NoneType' and 'int' Expected output solution [0.0.1250.1250.250] The support vectors are: | [[2 0.] [02] [33]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
