forked from neurominer-git/NeuroMiner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnk_EqualizeHisto.m
More file actions
65 lines (55 loc) · 1.97 KB
/
nk_EqualizeHisto.m
File metadata and controls
65 lines (55 loc) · 1.97 KB
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
function [ tI, I ] = nk_EqualizeHisto(Eq, V, I, modeflag)
if ~exist('V','var') || isempty(V), error('target label vector missing or undefined'); end
switch modeflag
case 'regression'
sV = sort(V,'ascend');
for i=1:numel(sV)
X = numel(find( V > sV(i)));
if X <= Eq.MinCount, break, end
end
OptMaxV = sV(i-1);
for i=numel(sV):-1:2
X = numel(find( V < sV(i)));
if X <= Eq.MaxCount, break, end
end
OptMinV = sV(i+1);
Edges = OptMinV : (OptMaxV - OptMinV) / Eq.BinCount : OptMaxV;
nE = numel(Edges);
N = histc( V, Edges) ; N(end) = [];
% fprintf('\nHistogram analysis of target labels:')
% for i=1:numel(N)-1
% fprintf('\n%1.1f<->%1.1f =>\t%g', Edges(i), Edges(i+1), N(i));
% end
minN = min(N); tI = []; tV = [];
for i = 1:nE-1
if i == nE
ind = find(V >= Edges(i) & V <= Edges(i+1));
else
ind = find(V >= Edges(i) & V < Edges(i+1));
end
if N(i) > minN
rInd = randperm(numel(ind));
rInd = rInd(1:minN);
tI = [ tI; I(ind(rInd)) ] ;
tV = [ tV; V(ind(rInd)) ] ;
else
tI = [ tI; I(ind) ] ;
tV = [ tV; V(ind) ];
end
end
I = setdiff(I, tI);
case 'classification'
uL = unique(V); nuL = numel(uL); suL= zeros(1,nuL); ruL = suL;
for i = 1:nuL, suL(i) = sum(V==uL(i)); end
[~,indmin] = min(suL); rI = [];
targsize = floor(suL(indmin)*Eq.posnegrat);
for i = 1:nuL,
ruL(i) = suL(i)/suL(indmin);
if i==indmin || ruL(i)<Eq.posnegrat, continue; end
remsize = floor(suL(i) - targsize);
f = find(V==uL(i)); remind = f(randperm(numel(f),remsize));
rI = [rI; remind];
end
tI = I(rI);
I(rI) = [];
end