Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MatLab to caluclate 1. Sum of Squared Distances 2. Angle Between Vectors 3. Numb

ID: 3885695 • Letter: M

Question

MatLab to caluclate

1.      Sum of Squared Distances

2.      Angle Between Vectors

3.      Number of Common Words

The remaining features are the training set, their labels (positive, negative) should be stored in array train_label, and their feature vectors should be stored in array train_feat.
1. Template file cse408_knn.m
This function returns the label (positive or negative) predicted by the kNN algorithm for the feature vector of a test file.
2. Function template function pred_label = cse408_knn(test_feat, train_label_set, train_feat_set, k, DstType)
3. Input arguments:
a test_feat is the feature vector of a test file (size is the size of the lexicon)
b train_label_set is the set of labels for the training set (size is the number of training files)
c train_feat_set is the set of feature vectors for the training set (size is the size of lexicon X number of training files)
d k is the hyperparameter of kNN algorithm, i.e. the number of neighbors used
e DstType is the distance computation method, i.e. 1 for sum of squared distances (SSD) and 2 for angle between vectors and 3 for Number of words in common;
4. Output:
a pred_label is the predicted label of the testing file. 1 for positive review, 0 for negative review;
5. Implement your code under %PUT YOUR IMPLEMENTATION HERE tag;
6. Useful Matlab functions sort();

Matlab Code

% function to run KNN classification


function pred_label = cse408_knn(test_feat_set, train_label, train_feat_set, k, DstType)


if DstType == 1 %SSD
%PUT YOUR CODE HERE
elseif DstType == 2 %Angle Between Vectors
%PUT YOUR CODE HERE
elseif DstType == 3 %Number of words in common
%PUT YOUR CODE HERE
dist = -dist; % Why minus?
end

%Find the top k nearest neighbors, and do the voting.

[B,I] = sort(dist);

posCt=0;
negCt=0;
for ii = 1:k
if train_label(I(ii)) == 1
posCt = posCt + 1;
elseif train_label(I(ii)) == 0
negCt = negCt + 1;
end
end

if posCt >= negCt
pred_label = 1;
else
pred_label = 0;
end

Explanation / Answer

Matlab Code

% function to run KNN classification

function pred_label = cse408_knn(test_feat_set, train_label, train_feat_set, k, DstType)

if DstType == 1 %SSD
%PUT YOUR CODE HERE
elseif DstType == 2 %Angle Between Vectors
%PUT YOUR CODE HERE
elseif DstType == 3 %Number of words in common
%PUT YOUR CODE HERE
dist = -dist; % Why minus?
end

%Find the top k nearest neighbors, and do the voting.
[B,I] = sort(dist);
posCt=0;
negCt=0;
for ii = 1:k
if train_label(I(ii)) == 1
posCt = posCt + 1;
elseif train_label(I(ii)) == 0
negCt = negCt + 1;
end
end
if posCt >= negCt
pred_label = 1;
else
pred_label = 0;
end