csci5521/assignments/hwk04/kp_predict.m
2023-12-08 05:22:31 -06:00

30 lines
633 B
Matlab

function scores = kp_predict(alphas, b, d, X, Y, tst_data)
% alphas, b: sample weights and bias term for classification
% d: degree of polynomial kernel <x,y>^d
% X,Y: training data and label
% tst_data: test data for prediction
% scores: the perdiction scores.
N = size(X,1);
%TO DO: calculate the predictions
[tst_size, ~] = size(tst_data);
scores = zeros(tst_size, 1);
for t = 1 : tst_size
xt = tst_data(t, :);
sum = 0;
for i = 1 : N
ai = alphas(i);
yi = Y(i);
xi = X(i, :);
sum = sum + ai * yi * dot(xi, xt) ^ d;
end
sum = sum + b;
scores(t) = sign(sum);
end
%********************************