-
Notifications
You must be signed in to change notification settings - Fork 0
/
LDA.m
92 lines (73 loc) · 2.33 KB
/
LDA.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
% LDA - MATLAB subroutine to perform linear discriminant analysis
% by Will Dwinnell and Deniz Sevis
%
% Use:
% W = LDA(Input,Target,Priors)
%
% W = discovered linear coefficients (first column is the constants)
% Input = predictor data (variables in columns, observations in rows)
% Target = target variable (class labels)
% Priors = vector of prior probabilities (optional)
%
% Note: discriminant coefficients are stored in W in the order of unique(Target)
%
% Example:
%
% % Generate example data: 2 groups, of 10 and 15, respectively
% X = [randn(10,2); randn(15,2) + 1.5]; Y = [zeros(10,1); ones(15,1)];
%
% % Calculate linear discriminant coefficients
% W = LDA(X,Y);
%
% % Calulcate linear scores for training data
% L = [ones(25,1) X] * W';
%
% % Calculate class probabilities
% P = exp(L) ./ repmat(sum(exp(L),2),[1 2]);
%
%
% Last modified: Dec-11-2010
function W = LDA(Input,Target,Priors)
% Determine size of input data
[n m] = size(Input);
% Discover and count unique class labels
ClassLabel = unique(Target);
k = length(ClassLabel);
% Initialize
nGroup = NaN(k,1); % Group counts
GroupMean = NaN(k,m); % Group sample means
PooledCov = zeros(m,m); % Pooled covariance
W = NaN(k,m+1); % model coefficients
if (nargin >= 3) PriorProb = Priors; end
% Loop over classes to perform intermediate calculations
for i = 1:k,
% Establish location and size of each class
Group = (Target == ClassLabel(i));
nGroup(i) = sum(double(Group));
% Calculate group mean vectors
GroupMean(i,:) = mean(Input(Group,:));
% Accumulate pooled covariance information
PooledCov = PooledCov + ((nGroup(i) - 1) / (n - k) ).* cov(Input(Group,:));
end
% Assign prior probabilities
if (nargin >= 3)
% Use the user-supplied priors
PriorProb = Priors;
else
% Use the sample probabilities
PriorProb = nGroup / n;
end
% Loop over classes to calculate linear discriminant coefficients
for i = 1:k,
% Intermediate calculation for efficiency
% This replaces: GroupMean(g,:) * inv(PooledCov)
Temp = GroupMean(i,:) / PooledCov;
% Constant
W(i,1) = -0.5 * Temp * GroupMean(i,:)' + log(PriorProb(i));
% Linear
W(i,2:end) = Temp;
end
% Housekeeping
clear Temp
end
% EOF