-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmilvector.m
More file actions
executable file
·226 lines (215 loc) · 5.45 KB
/
Copy pathmilvector.m
File metadata and controls
executable file
·226 lines (215 loc) · 5.45 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
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
223
224
%MILVECTOR Vector representation of a bag
%
% W = MILVECTOR(X,RTYPE)
%
% INPUT
% X MIL dataset
% RTYPE Method for obtaining a vector from a bag
% (default = 'm')
%
% OUTPUT
% W Standard Prtools mapping
%
% DESCRIPTION
% Extract a single feature vector from each bag of instances in X and
% store it in Y. The following features are defined:
% RTYPE: DOES:
% 'm' mean per bag (default)
% 's' sum per bag
% 'e' extreme (min and max) values per feature per bag
% 'c' covariance matrix elements
% 'n' number of instances per bag
%
% The parameter COPYMETHOD determines what label each feature vector
% obtains, given the labels of the instances in the bag.
%
% Note that this is a *trained* mapping, so this:
% >> y=milvector(x,'m') will result in y being a mapping. To get a
% dataset, you have to do:
% >> y=x*milvector(x,'m')
%
% SEE ALSO
% MILCOMBINE, LABELSET
% Copyright: D.M.J. Tax, D.M.J.Tax@prtools.org
% Faculty EWI, Delft University of Technology
% P.O. Box 5031, 2600 GA Delft, The Netherlands
%function y = milvector(x,rtype)
function y = milvector(varargin)
argin= shiftargin(varargin,'char');
argin = setdefaults(argin,[],'m');
if mapping_task(argin,'definition')
[x,rtype] = deal(argin{:});
W = define_mapping(argin,'untrained',milvectorname(rtype));
y = setbatch(W,0); %NEVER use batches!!
elseif mapping_task(argin,'training')
[x,rtype] = deal(argin{:});
if size(rtype,1)==1 && size(rtype,2)>1
warning('Please make RTYPE a column matrix.');
rtype = rtype';
end
if isa(rtype,'cell')
rtype = cell2mat(rtype);
end
% 'train' the mapping:
% (training is bullshit here, but we have to make sure that the input
% and output dimensionalities are correct...)
if size(rtype,1)==1 && size(rtype,2)>1
warning('Please make RTYPE a column matrix.');
rtype = rtype';
end
if isa(rtype,'cell')
rtype = cell2mat(rtype);
end
if size(rtype,1)>1
p = size(x,2);
pnew = 0;
for i=1:length(rtype)
pnew = pnew + size(milvector(x,rtype(i,:)),2);
end
else
% define here the output dimensionality for each of the feature
% definitions:
[m,p] = size(x);
switch rtype
case 'm'
pnew = p;
case 's'
pnew = p;
case 'e'
pnew = 2*p;
case 'u'
pnew = 2*p;
case 'c'
pnew = p*(p+1)/2;
case 'n'
pnew = 1;
end
end
y = prmapping(mfilename,'trained',{rtype},[],p,pnew);
y = setname(y,milvectorname(rtype));
y = setbatch(y,0); % NEVER do batches!
elseif mapping_task(argin,'trained execution') %testing
[x,rtype] = deal(argin{:});
%x = genmil(x); % I need a MIL dataset to derive the bag labels
% now we have data, and we *apply* the mapping:
if ismapping(rtype)
W = getdata(rtype);
rtype = W{1};
end
% if we give it a cell array full of options, we do each in turn:
if size(rtype,1)>1
y = x*milvector(x,rtype(1,:));
for i=2:length(rtype)
y = [y x*milvector(x,rtype(i,:))];
end
return
end
% now we only have one feature type to take care of:
[bags,lab,bagid] = getbags(x);
[m,p] = size(x);
n = length(bags);
switch rtype
case 'm' % only the mean vector of a bag
y = zeros(n,p);
for i=1:n
y(i,:) = mean(bags{i},1);
end
oldfl = getfeatlab(x);
if ~isempty(oldfl)
fl = [repmat('mean ',p,1) num2str(oldfl)];
else
fl = cellprintf('mean %d',1:p);
end
case 's' % the sum vector of a bag
y = zeros(n,p);
for i=1:n
y(i,:) = sum(bags{i},1);
end
oldfl = getfeatlab(x);
if ~isempty(oldfl)
fl = [repmat('sum ',p,1) num2str(oldfl)];
else
fl = cellprintf('sum %d',1:p);
end
case 'e' % the min and max values of a bag
y = zeros(n,2*p);
for i=1:n
y(i,:) = [min(bags{i},[],1) max(bags{i},[],1)];
end
oldfl = getfeatlab(x);
if ~isempty(oldfl)
fl = num2str(oldfl);
else
fl = num2str((1:p)');
end
fl = [ [repmat('min ',p,1) fl]; [repmat('max ',p,1) fl]];
case 'u' % the min and max values of a bag, but differently
y = zeros(n,2*p);
for i=1:n
x1 = min(bags{i},[],1);
x2 = max(bags{i},[],1);
y(i,:) = [(x1+x2)/2 x2-x1];
end
oldfl = getfeatlab(x);
if ~isempty(oldfl)
fl = num2str(oldfl);
else
fl = num2str((1:p)');
end
fl = [ [repmat('centr ',p,1) fl]; [repmat('width ',p,1) fl]];
case 'c' % the elements in the cov. matrix
% first define the indices
D = p*(p+1)/2; % total nr of unique elements
I = [];
for i=1:p
I = [I (i-1)*p+(i:p)];
end
y = zeros(n,D);
for i=1:n
if size(bags{i},1)==1 % sigh, when we have one instance...
c = zeros(size(bags{i},2));
else
c = cov(bags{i});
end
y(i,:) = c(I);
end
fl = cellprintf('cov %d',I);
case 'n'
for i=1:n
y(i,1) = size(bags{i},1);
end
fl = 'nr.inst';
otherwise
error('Type %s is not defined.',rtype);
end
% we have the new features, and the feature labels, so go:
y = prdataset(y,lab,'prior',0,'featlab',fl);
[nlab,ll] = renumlab(lab,getlablist(x));
y = setlablist(y,getlablist(x));
y = setnlab(y,nlab);
y = setident(y,(1:n)','milbag');
y = setname(y,getname(x));
y = setprior(y,getprior(x,0)); %DXD well, is this a good idea? What
% alternative do we have?
end
function name = milvectorname(rtype)
if size(rtype,1)>1
name = 'milvector';
else
switch rtype
case 'm'
name = 'mean-inst';
case 's'
name = 'sum-inst';
case 'e'
name = 'extremes';
case 'u'
name = 'extremes2';
case 'c'
name = 'cov-coef';
case 'n'
name = 'nr.inst';
otherwise
error('rtype is not recognized');
end
end