Python机器学习代写 | 4120_COMP_SCI_X_0015 Assignment 2: Support Vector Machines


This is not a group assignment. Everyone is asked to complete the assignment individually.
The assignment consists of a mix set of tasks, including derivation, question answering, coding
and result analysis. You need to provide your solutions to the corresponding sections in this quiz.

You need to also upload your code. We will check the code to ensure that your results can be
generated from your code. If your result does not match with your code, you will get 0 mark for
the relevant sections.

You should use Python to finish this assignment. Juptyer notebook is allowed and encouraged.

Please download the data that will be used in this assignment here: train.csv test.csv

There are a “train.csv” file and a “test.csv” file. They are the training data and testing data,
respectively. Inside the CSV file, the first column is the class label, the remaining columns are the

The label (y) can take one of two classes – 0, 1;
In total, there are 200 features (X);
Trainset consists of 8500 samples, testset has 1500;

Please use the first 4000 samples in the “train.csv” file as the training set and the remaining samples
as validation set. All the samples in the “test.csv” file will be used as test samples.

You can use any third-party libraries to read and process data.
You can use numpy in any question.
Please follow the instruction of each question to complete the relevant parts.

Question 1

Please write down the primal form of the hard-margin SVM and derive its dual formation. You are
required to upload a PDF file (preferably in one page) to show your derivation of the dual

Question 2

Please implement the training and testing algorithms of so”-margin Linear Support Vector Machine
from its primal form, that is,

by using CVX. Your implementation should follow the following I/O format:
svm_model = svm_train_primal ( data_train , label_train , regularisation_para_C )
test_accuracy = svm_predict_primal ( data_test , label_test , svm_model )

Please copy the code snippet for the implementation of those two functions (You can also do this by
a#aching the screenshot of your code. ).

By se$ng C = 100, run your implementation, please report the solution of and sum of all
dimensions of solution, e.g., np.sum(w). (For a quick check of the correctness of your code)