Question: Build a function in Matlab named firstname_lastname_perceptron.m which takes 3 inputs, i) training features, ii) training labels, and iii) maximum number of iterations. The function
Build a function in Matlab named firstname_lastname_perceptron.m which takes 3 inputs, i) training features, ii) training labels, and iii) maximum number of iterations. The function should output the weight vector, accuracy and number of iterations required.
Function [w, a, iter] = firstname_lastname_perceptron(train_features, train_labels, maxiter)
Inputs:
1. train_features: (NxD) Array with D dimensional N data points
2. train_labels: (Nx1) Array containing labels of N data points. Remember that the perceptron algorithm requires your labels to be -1 and 1
3. maxiter: maximum number of iterations
Outputs:
1. w: weight vector. Bias value is in the weight vector itself. W = [w b]
2. a: accuracy of the classification
3. iter: number of iterations required
Use the following code to generate training data.
train_mean1 = [10,10]; %train data cluster 1 mean
train_sigma1 = [2 -1.5; -1.5 2];
train_mean2 = [7,7]; %train data cluster 2 mean
train_sigma2 = [2 -0.2; -0.2 2];
ntrain1 = 50; %Number of train data points for cluster 1
ntrain2 = 100; % Number of train data points for cluster 2 train_featuresa = mvnrnd(train_mean1,train_sigma1,ntrain1); train_labelsa = 1*ones(ntrain1,1);
train_featuresb = mvnrnd(train_mean2,train_sigma2,ntrain2); train_labelsb = -1*ones(ntrain2,1);
train_features = [train_featuresa; train_featuresb];
train_labels = [train_labelsa; train_labelsb];
The above code will not generate a linearly separable data every time. You have to modify your perceptron algorithm such that it gives the best possible decision boundary. Your function should return a=1 for linearly separable data and a<1 for linearly inseparable data.
Try to achieve maximum accuracy for linearly inseparable data. Your perceptron function will be tested for both linearly separable and linearly inseparable data.
Hints: You can shuffle your data (training sample,training label) pairs before you start updating the weight vector.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
