source: prextra/densityc.m @ 72

Last change on this file since 72 was 17, checked in by dtax, 14 years ago

Better than bayesc.m?

File size: 1.1 KB
RevLine 
[17]1%    w = densityc(a,u)
2%
3% Train a density per class in dataset A. The density is defined in the
4% cell array U, where each element U{i} is an untrained density mapping.
5% When just a single untrained mapping U is defined, the same model will
6% be used for all classes.
7%
8% For instance:
9% >> a = gendatb;
10% >> u = scalem([],'variance')*parzenm;
11% >> w = densityc(a,u)
12%
13function w = densityc(a,u)
14
15if nargin<2
16        u = gaussm;
17end
18if nargin<1 || isempty(a)
19        w = mapping(mfilename,{u});
20        w = setname(w,'Densitybased classifier');
21        return
22end
23
24if ~istrained(u)
25        [n,d,c]=getsize(a);
26        if ~isa(u,'cell')
27                % make sure we have a mapping per class
28                u = repmat({u},c,1);
29        else
30                if length(u)~=c
31                        error('I need %s untrained mappings in the cell array.',c);
32                end
33        end
34        % train them:
35        dens = cell(c,1);
36        for i=1:c
37                dens{i} = seldat(a,i)*u{i};
38        end
39        % save the stuff:
40        W.dens = dens;
41        W.prior = getprior(a,0);
42        w = mapping(mfilename,'trained',W,getlablist(a),d,c);
43        w = setname(w,'Density based classifier');
44else
45        % extract data:
46        W = getdata(u);
47        n = size(a,1);
48        c = length(W.dens);
49        out = zeros(n,c);
50        for i=1:c
51                out(:,i) = +(a*W.dens{i});
52        end
53        w = setdat(a,out,u);
54end
Note: See TracBrowser for help on using the repository browser.