-
Notifications
You must be signed in to change notification settings - Fork 0
/
hubness_analysis.m
223 lines (198 loc) · 8.37 KB
/
hubness_analysis.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
function hubness_analysis(D, classes, vectors)
% Performs a quick hubness analysis with all the functions provided in this
% toolbox.
%
% This file is part of the HUB TOOLBOX available at
% http://ofai.at/research/impml/projects/hubology.html
% https://github.com/OFAI/hub-toolbox-matlab/
% (c) 2013, Dominik Schnitzer <[email protected]>
% (c) 2016, Roman Feldbauer <[email protected]>
%
% Usage:
% hubness_analysis() - Loads the example data set and performs the
% analysis
%
% hubness_analysis(D, classes, vectors) - Uses the distance matrix D (NxN)
% together with an optional class labels vector (classes) and the
% original (optional) data vectors (vectors, Pts x Dims) to perform a
% full hubness analysis
haveClasses = false;
haveVectors = false;
if (nargin == 0)
[D, classes, vectors] = load_dexter();
haveClasses = true;
haveVectors = true;
elseif (nargin == 1)
% all ok, just analyze D
elseif (nargin == 2)
haveClasses = true;
else
haveClasses = true;
haveVectors = true;
end
n = size(D,1);
[Sn5, tmp, Nk5] = hubness(D, 5);
fprintf('\nHubness Analysis\n\n');
fprintf('ORIGINAL DATA:\n');
fprintf('data set hubness (S^n=5) : %.2f\n', Sn5);
fprintf('%% of anti-hubs at k=5 : %.2f%%\n',...
100*sum(Nk5==0)/n);
fprintf('%% of k=5-NN lists the largest hub occurs: %.2f%%\n',...
100*max(Nk5)/n);
if (haveClasses == true)
fprintf('k=5-NN classification accuracy : %.2f%%\n',...
100*knn_classification(D, classes, 5));
fprintf('Goodman-Kruskal index (higher=better) : %.3f\n',...
goodman_kruskal(D, classes));
else
fprintf('k=5-NN classification accuracy : No classes given\n');
fprintf('Goodman-Kruskal index (higher=better) : No classes given\n');
end
if (haveVectors == true)
fprintf('original dimensionality : %d\n', size(vectors, 2));
fprintf('intrinsic dimensionality estimate : %d\n',...
round(intrinsic_dim(vectors)));
else
fprintf('original dimensionality : No vectors given\n');
fprintf('intrinsic dimensionality estimate : No vectors given\n');
end
fprintf('\nMUTUAL PROXIMITY (Empiric/Slow):\n');
Dn = mutual_proximity(D, 'empiric');
[Sn5, tmp, Nk5] = hubness(Dn, 5);
fprintf('data set hubness (S^n=5) : %.2f\n', Sn5);
fprintf('%% of anti-hubs at k=5 : %.2f%%\n',...
100*sum(Nk5==0)/n);
fprintf('%% of k=5-NN lists the largest hub occurs: %.2f%%\n',...
100*max(Nk5)/n);
if (haveClasses == true)
fprintf('k=5-NN classification accuracy : %.2f%%\n',...
100*knn_classification(Dn, classes, 5));
fprintf('Goodman-Kruskal index (higher=better) : %.3f\n',...
goodman_kruskal(Dn, classes));
else
fprintf('k=5-NN classification accuracy : No classes given\n');
fprintf('Goodman-Kruskal index (higher=better) : No classes given\n');
end
% fprintf('\nMUTUAL PROXIMITY (Gauss):\n');
% Dn = mutual_proximity(D, 'gauss');
% [Sn5, tmp, Nk5] = hubness(Dn, 5);
% fprintf('data set hubness (S^n=5) : %.2f\n', Sn5);
% fprintf('%% of anti-hubs at k=5 : %.2f%%\n',...
% 100*sum(Nk5==0)/n);
% fprintf('%% of k=5-NN lists the largest hub occurs: %.2f%%\n',...
% 100*max(Nk5)/n);
% if (haveClasses == true)
% fprintf('k=5-NN classification accuracy : %.2f%%\n',...
% 100*knn_classification(Dn, classes, 5));
% fprintf('Goodman-Kruskal index (higher=better) : %.3f\n',...
% goodman_kruskal(Dn, classes));
% else
% fprintf('k=5-NN classification accuracy : No classes given\n');
% fprintf('Goodman-Kruskal index (higher=better) : No classes given\n');
% end
%
% fprintf('\nMUTUAL PROXIMITY (Gaussi):\n');
% Dn = mutual_proximity(D, 'gaussi');
% [Sn5, tmp, Nk5] = hubness(Dn, 5);
% fprintf('data set hubness (S^n=5) : %.2f\n', Sn5);
% fprintf('%% of anti-hubs at k=5 : %.2f%%\n',...
% 100*sum(Nk5==0)/n);
% fprintf('%% of k=5-NN lists the largest hub occurs: %.2f%%\n',...
% 100*max(Nk5)/n);
% if (haveClasses == true)
% fprintf('k=5-NN classification accuracy : %.2f%%\n',...
% 100*knn_classification(Dn, classes, 5));
% fprintf('Goodman-Kruskal index (higher=better) : %.3f\n',...
% goodman_kruskal(Dn, classes));
% else
% fprintf('k=5-NN classification accuracy : No classes given\n');
% fprintf('Goodman-Kruskal index (higher=better) : No classes given\n');
% end
fprintf('\nMUTUAL PROXIMITY (Gammai):\n');
Dn = mutual_proximity(D, 'gammai');
[Sn5, tmp, Nk5] = hubness(Dn, 5);
fprintf('data set hubness (S^n=5) : %.2f\n', Sn5);
fprintf('%% of anti-hubs at k=5 : %.2f%%\n',...
100*sum(Nk5==0)/n);
fprintf('%% of k=5-NN lists the largest hub occurs: %.2f%%\n',...
100*max(Nk5)/n);
if (haveClasses == true)
fprintf('k=5-NN classification accuracy : %.2f%%\n',...
100*knn_classification(Dn, classes, 5));
fprintf('Goodman-Kruskal index (higher=better) : %.3f\n',...
goodman_kruskal(Dn, classes));
else
fprintf('k=5-NN classification accuracy : No classes given\n');
fprintf('Goodman-Kruskal index (higher=better) : No classes given\n');
end
fprintf('\nLOCAL SCALING (Original, k=10):\n');
Dn = local_scaling(D, 10, 'original');
[Sn5, tmp, Nk5] = hubness(Dn, 5);
fprintf('data set hubness (S^n=5) : %.2f\n', Sn5);
fprintf('%% of anti-hubs at k=5 : %.2f%%\n',...
100*sum(Nk5==0)/n);
fprintf('%% of k=5-NN lists the largest hub occurs: %.2f%%\n',...
100*max(Nk5)/n);
if (haveClasses == true)
fprintf('k=5-NN classification accuracy : %.2f%%\n',...
100*knn_classification(Dn, classes, 5));
fprintf('Goodman-Kruskal index (higher=better) : %.3f\n',...
goodman_kruskal(Dn, classes));
else
fprintf('k=5-NN classification accuracy : No classes given\n');
fprintf('Goodman-Kruskal index (higher=better) : No classes given\n');
end
fprintf('\nSHARED NEAREST NEIGHBORS (k=10):\n');
Dn = shared_nn(D, 10);
[Sn5, tmp, Nk5] = hubness(Dn, 5);
fprintf('data set hubness (S^n=5) : %.2f\n', Sn5);
fprintf('%% of anti-hubs at k=5 : %.2f%%\n',...
100*sum(Nk5==0)/n);
fprintf('%% of k=5-NN lists the largest hub occurs: %.2f%%\n',...
100*max(Nk5)/n);
if (haveClasses == true)
fprintf('k=5-NN classification accuracy : %.2f%%\n',...
100*knn_classification(Dn, classes, 5));
fprintf('Goodman-Kruskal index (higher=better) : %.3f\n',...
goodman_kruskal(Dn, classes));
else
fprintf('k=5-NN classification accuracy : No classes given\n');
fprintf('Goodman-Kruskal index (higher=better) : No classes given\n');
end
fprintf('\n');
end
function [D, classes, vectors] = load_dexter()
fprintf('\nNO PARAMETERS GIVEN! Loading & evaluating DEXTER data set.\n\n');
fprintf('DEXTER is a text classification problem in a bag-of-word\n');
fprintf('representation. This is a two-class classification problem\n');
fprintf('with sparse continuous input variables.\n');
fprintf('This dataset is one of five datasets of the NIPS 2003 feature\n');
fprintf('selection challenge.\n\n');
fprintf('http://archive.ics.uci.edu/ml/datasets/Dexter\n\n');
n = 300;
dim = 20000;
vectors = zeros(n, dim);
classes = zeros(n, 1);
fid = fopen('example_datasets/dexter_train.data', 'r');
for i=1:n
tline = fgetl(fid);
d = sscanf(tline, '%d:%d');
vectors(i,d(1:2:end)) = d(2:2:end);
end
fclose(fid);
fid = fopen('example_datasets/dexter_train.labels', 'r');
for i=1:n
tline = fgetl(fid);
d = sscanf(tline, '%d');
classes(i) = d(1);
end
fclose(fid);
D = cosine_distance(vectors);
end
function D = cosine_distance(x)
xn = sqrt(sum(x.^2, 2));
x = x ./ repmat(xn, 1, size(x, 2));
D = 1 - x*x';
D(D<0) = 0;
D = triu(D, 1) + triu(D, 1)';
end