![]() You only need to provide the answers for w1, w2, and b. You can solve it with a paper and pen by listing linear equations. Use the property that a support vector x ( k ) must satistify y ( k ) ( w T x ( k ) b ) = 1. Task 3: Call quadprog 1 point \#\#\# 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 ) Expected output ]ģ points Use the support vectors to solve the w and b in the decision boundary w T x b = 0. Use np.eye() G = None \# Hint: h is of length 5, with all zeros Use np.zeros() h = None A = Y ⋅ res ha p e (( 1, 5 )) \# Hint: b is of length 1, with zero value Use np.zeros() b = None \#\#\# END YOUR CODE \#\#\# print ( ′ q = ' q, q ) print ( ′ G = ' G, G ) print ( ′ h = ′, h ) print ( ′ b = ′, b ) Expected output q = G = ] h = b = We want to build an SVM model on the toy dataset: x ( 1 ) = ( 0, 0 ), y ( 1 ) = − 1 x ( 2 ) = ( 2, 0 ), y ( 2 ) = − 1 x ( 3 ) = ( 0, 2 ), y ( 3 ) = − 1 x ( 4 ) = ( 3, 3 ), y ( 4 ) = 1 x ( 5 ) = ( 4, 4 ), y ( 5 ) = 1 We need to solve the quadratic programming (QP) problem as the following form: α min ( 2 1 α T Q α − ( 1 ) T α ) subject to: y T α = 0, α ≥ 0 The quadprog package by defaualt solves the QP as this form: x min ( 2 1 x T P x q T x ) subject to: G x ≤ h, A x = b Therefore, in order to use quadprog, we need to establish the responding relationships between variables: P = Q, q = − ( 1 ) T, G = − ( 1 ) T, h = ( 0 ) T, A = y T, b = ( 0 ) T Task 1: Compute matrix Q 3 points First, we need to use x ( i ) and y ( i ) to compute matrix Q : Q = y ( 1 ) y ( 1 ) x ( 1 ) T x ( 1 ) y ( 2 ) y ( 1 ) x ( 2 ) T x ( 1 ) ⋮ y ( 5 ) y ( 1 ) x ( 5 ) T x ( 1 ) y ( 1 ) y ( 2 ) x ( 1 ) T x ( 2 ) y ( 2 ) y ( 2 ) x ( 2 ) T x ( 2 ) ⋮ y ( 5 ) y ( 2 ) x ( 5 ) T x ( 2 ) … … ⋱ … y ( 1 ) y ( 5 ) x ( 1 ) T x ( 5 ) y ( 2 ) y ( 5 ) x ( 2 ) T x ( 5 ) ⋮ y ( 5 ) y ( 5 ) x ( 5 ) T x ( 5 ) Įxpected output Q =∣ ] ģ points Use the folumas: P = Q, q = − ( 1 ) T, G = − ( 1 ) T, h = ( 0 ) T, A = y T, b = ( 0 ) T \#\#\# START YOUR CODE \#\#\# P = Q n p.eye ( 5 ) ∗ 1 e − 5 \# To solve the non-positive finite issue \# Hint: Use n p.ones(), q is of length 5 q = None \# Hint: G is a matrix whose diagnal elements are 1s, and other elements are Os. Stack Numpy Arrays Using rowstack() Function in Python Stack Numpy Arrays Across Depth Using dstack() Function Conclusion The Concatenate() Function You can use concatenate() function to concatenate 1-D and 2-D numpy arrays along an existing axis. You can install it using the following command: (Some mismatch in the last digit of floating numbers is tolerable) Install dependencies quadprog is a Python package for solving quadratic programming problems. The output of the evaluation cell should be the same as the "expected output" provided. For each task cell that requires your completion, you can run the evaluation cell right after it to check if your answer correct. Before you run some task cells, you need to complete the missing lines (notified by "= None" in Python) in them. Your goal is to run all the cells below one by one from top to bottom. Total: 10 points In this assignment, we will build a "toy" SVM model using a mini dataset step by step. features np.vstack ( features,extfeatures) return np.array (features) def parseaudiofiles (parentdir, subdirs, fileextNone, verboseTrue): '''Parses directory in search of specified file types, then compiles feature data from them. # Hint: b is of length 1, with zero value Use np.zeros() # Hint: h is of length 5, with all zeros Use np.zeros() # Hint: G is a matrix whose diagnal elements are 1s, and other elements are 0s. P = Q np.eye(5)*1e-5 # To solve the non-positive finite issue # Use the ith and jth examples in X and Y to compute Q_ij Return quadprog.solve_qp(qp_G, qp_a, qp_C, qp_b, meq) ![]() Here I started with a (0,3) array.Def quadprog_solve_qp(P, q, G=None, h=None, A=None, b=None): You actually have to understand array shapes, and what concatenate does when it combines 2 or more arrays. It is harder to initialize, as you found out. vstack makes whole new array each time, with a full copy! This function makes most sense for arrays with up to 3 dimensions. This is equivalent to concatenation along the first axis after 1-D arrays of shape (N,) have been reshaped to (1,N). It is slower list append operates in-place simply adding a pointer to the list. numpy.vstack () function The vstack () function is used to stack arrays in sequence vertically (row wise). You could use vstack in the loop: In : arr = np.zeros((0,3),int) ![]() Or since vstack is happy with a list of arrays: In : np.vstack(alist) Out: ), array(), array(), array()]Īnd make the array: In : np.array(alist) Your first use, with list append: In : alist=
0 Comments
Leave a Reply. |