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 *
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, ])

Use the support vectors to solve the w and b in the decision boundary wTx+b=0. Use the property that a support vector x(k) must satistify y(k)(wTx(k)+b)=1. You can solve it with a paper and pen by listing linear equations. NOTE: Solve this task on paper. You only need to provide the answers for w1, w2, and b. Hint: You should solve the following linear equations: y(2)(wTx(2)+b)=1y(3)(wTx(3)+b)=1y(4)(wTx(4)+b)=1 \#\# START YOUR ANSWERS \#\#\# w1 = None w2 = None b= None \#\#\# END YOUR ANSWERS print( 'w1 = ', w1) print('w2 = ', w2) print(b=,b)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
