-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn_weight_gower_concat_2data_weighted_tune.m
62 lines (44 loc) · 1.95 KB
/
knn_weight_gower_concat_2data_weighted_tune.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function [w] = knn_weight_gower_concat_2data_weighted_tune(X,gamma,fs_weight,K,phi)
% Pick 1:K nearest neighbors in distance, for same distance, select
% randomly one
[~,n] = size(X);
% [p2 n] = size(Y);
% Total length of w
len_w = n*(n-1)/2;
% KNN method
tmp = gower_weighted(X,fs_weight);
% Make it a distance matrix
D = squareform(tmp);
% Distance matrix with sparse outputs(only contain K nearest neighbor)
M = zeros(n,n);
%%% For each node i, pick its K nearest neighbor
for i = 1:n
% sort_ind: the distance index from smallest to largest
[sort_dist, sort_ind] = sort(D(i,:),'ascend');
%%% Select even values in distance with the Kth one
even_index = [];
even_index = find( D(i,:) == D(i,sort_ind(K+1)) ); % select index which equals to the Kth minimum distance
if length(even_index) > 1 % If there are ties
strict_less_count = sum( D(i,:) < D(i,sort_ind(K+1))); % Find those who is strict nearest neighbor
sort_ind(strict_less_count+1:K+1) = randsample( even_index, K+1 - strict_less_count); % For those who have ties, randomly select some so we have K nearest neigobur
% update the K-nearest neighbor index
end
%%%
% choose 2 to K+1 since the first(smallest) is 0 , distance with itself D(i,i) is also included
M(i,sort_ind(2:K+1)) = D(i,sort_ind(2:K+1));
end
% make M symmetric, since for previous one, i is j's K nearest neighbor
% does not mean j is i's K nearest neighbor
for i = 1:n
for j = 1:n
if M(i,j) ~= 0
M(j,i) = M(i,j);
end
end
end
% get sparse distance vector
d = squareform(M);
% calculate w based on distance
% use gaussian kernel
w = gamma * (d~=0) .* exp(-phi*d);
end