Kernel KNN ( K-Nearest Neighbors )

Kernel KNN函数

代码

knn.m

function [ y ] = knn( X, X_train, y_train, K)
%KNN K-Nearest Neighbors Algorithm.
%
% INPUT: X: testing sample features, P-by-N_test matrix.
% X_train: training sample features, P-by-N matrix.
% y_train: training sampele labels, 1-by-N vector.
% K: take k in k-Nearest Neighbors.
% OUTPUT: y: predicted labels, 1-by-N vector.
%Author: X-Lion
%Date: 20150807
[~,N_test] = size(X);

predicted_label = zeros(1,N_test);
for i = 1:N_test
    % calculate the K nearest neighbors and the distance
    [dists,neighbors] = top_K_neighbors(X_train,y_train,X(:,i),K);
    % recognize the label of the test vector.
    predicted_label(i) = recog(y_train(neighbors),max(y_train));
end

y = predicted_label;

end

top_K_neighbors.m

function [ dists,neighbors ] = top_K_neighbors( X_train,y_train,X_test,K )
% TOP_K_NEIGHBORS
% 
% INPUT: X_test: the test vector with P-by-1.
% X_train,y_train: training data set. 
% K: is the K neighbor parameter.
% OUTPUT: dists,neighbors: top K neighbors index and dist.
% Author: X-Lion
% Date: 20150807
[~,N_train] = size(X_train);
test_mat = repmat(X_test,1,N_train);
% The distance is the Euclid Distance.
dist_mat = (X_train-double(test_mat)).^2;
dist_array = sum(dist_mat);

% Linear Kernel(k(ui,vj) = ui'*vj)
% for i = 1:N_train
% Kxx = X_train(:,i)'*X_train(:,i);
% Kxy = X_train(:,i)'*X_test;
% Kyy = X_test'*X_test;
% dist_array(i) = Kxx - 2 * Kxy + Kyy;
% end

% Gaussian Kernel(k(ui,vj) = exp(-gama*||ui-vj||^2))
% Parameter:
% g:gama
% g = 0.1;
% for i = 1:N_train
% Kxx = exp(-g*norm(X_train(:,i)-X_train(:,i))^2);
% Kxy = exp(-g*norm(X_train(:,i)-X_test)^2);
% Kyy = exp(-g*norm(X_test-X_test)^2);
% dist_array(i) = Kxx - 2 * Kxy + Kyy;
% end

% Polynomial Kernel(k(ui,vj) = (gama*ui'*vj+coef)^d)
% Parameter:
% g:gama
% coef
% d:degree
% g = 0.01;
% coef = 1;
% d = 2;
% for i = 1:N_train
% Kxx = (g*X_train(:,i)'*X_train(:,i) + coef)^d;
% Kxy = (g*X_train(:,i)'*X_test + coef)^d;
% Kyy = (g*X_test'*X_test + coef)^d;
% dist_array(i) = Kxx - 2 * Kxy + Kyy;
% end

% Tanh Kernel(k(ui,vj) = tanh(gama*ui'*vj + coef))
% Parameter:
% g:gama
% coef
% g = 2;
% coef = 1;
% for i = 1:N_train
% Kxx = tanh(g*X_train(:,i)'*X_train(:,i) + coef);
% Kxy = tanh(g*X_train(:,i)'*X_test + coef);
% Kyy = tanh(X_test'*X_test + coef);
% dist_array(i) = Kxx - 2 * Kxy + Kyy;
% end


%The neighbors are the index of top K nearest points.
[dists,neighbors] = sort(dist_array);
dists = dists(1:K);
neighbors = neighbors(1:K);
end

recog.m

function result = recog( K_labels,class_num )
%RECOG
%
% INPUT: X_test: the test vector with P-by-1.
% X_train,y_train: training data set. 
% K: is the K neighbor parameter.
% OUTPUT: dists,neighbors: top K neighbors index and dist.
%Author: X-Lion
%Date: 20150807
[~,K] = size(K_labels);
class_count = zeros(1,class_num+1);
for i = 1:K
    class_index = K_labels(i)+1;
    class_count(class_index) = class_count(class_index) + 1;
end
[~,result] = max(class_count);
result = result - 1;
end

参考资料

  • http://blog.csdn.net/rk2900/article/details/9080821

你可能感兴趣的:(Matlab,杂小程序,机器学习)