forked from LABSN-pubs/2015-JNE-Wronkiewicz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldaReg.py
63 lines (46 loc) · 1.68 KB
/
ldaReg.py
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
#dpylint: disable-msg=C0103
import numpy as np
def ldaRegWeights(X, y, reg=[0.05]):
"""
Fit the Regularized LDA model according to the given training data and
parameters.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Training vector, where n_samples in the number of samples and
n_features is the number of features.
y : array, shape = [n_samples]
Target values (integers)
reg : array of float
Amount of regularization to use.
"""
n, m = X.shape
classLabel = np.unique(y)
k = len(classLabel)
nReg = len(reg)
nGroup = np.zeros(k)
groupMean = np.zeros((m, k))
pooledCov = np.zeros((m, m))
#Perform intermediate calculations
for i in np.arange(k):
# Establish location and size of each class
group = (y == classLabel[i])
nGroup[i] = np.sum(group)
temp = X[group, :]
#Calculate group mean vectors
groupMean[:, i] = np.mean(temp, axis=0)
#Accumulate pooled cov information
pooledCov = pooledCov + ((nGroup[i] - 1) / (n - k)) * \
np.cov(temp, rowvar=0)
# Calculate prior probs
priorProb = nGroup / n
# Loop over classes to calculate LDA coefs
W = np.zeros((pooledCov.shape[0] + 1, len(classLabel), nReg))
for ri in np.arange(nReg):
pooledCov2 = pooledCov * (1 - reg[ri]) + (reg[ri] / m) * \
np.trace(pooledCov) * np.eye(m)
temp = np.linalg.solve(pooledCov2, groupMean)
wtsPt1 = np.diag(-0.5 * np.dot(groupMean.T, temp)
+ np.log(priorProb)).reshape(1, -1)
W[:, :, ri] = np.concatenate((wtsPt1, temp))
return W