-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadratic_discriminant_analysis.py
251 lines (188 loc) · 7.53 KB
/
quadratic_discriminant_analysis.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import torch
import warnings
import torchml as ml
class QuadraticDiscriminantAnalysis(ml.Model):
"""
<a href="https://github.com/learnables/torchml/blob/master/torchml/discriminant_analysis/quadratic_discriminant_analysis.py">[Source]</a>
## Description
Quadratic Discriminant Analysis is a classifier with a quadratic decision boundary, which is calculated by fitting class conditional densities to the data and using Bayes' rule. This model fits a Gaussian density to each class.
This current implementation only includes "svd" solver.
## References
1. Carl J Huberty's Discriminant Analysis [paper](https://www.jstor.org/stable/1170065#metadata_info_tab_contents)
2. The scikit-learn [documentation page](https://scikit-learn.org/stable/modules/generated/sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis.html)
## Arguments
* `priors` (torch.Tensor, default=None) - The class prior probabilities. By default, the class proportions are calculated from the input training data.
* `reg_param` (float, default=0.0) - Regularizes the per-class covariance estimates by transforming S2 as `S2 = ((1 - reg_param) * S2) + reg_param`, where S2 corresponds to the scaling_ attribute of a given class.
* `store_covariance` (bool, default=False) - If True, the class covariance matrices will be explicitly computed and stored in the self.covariance_ attribute.
* `tol` (float, default=1e-4) - Absolute threshold for a singular value to be considered significant. This parameter does not affect the predictions. It controls a warning that is raised when features are considered to be colinear.
## Example
~~~python
qda = QuadraticDiscriminantAnalysis()
~~~
"""
def __init__(
self,
*,
priors: torch.Tensor = None,
reg_param: float = 0.0,
store_covariance: bool = False,
tol: float = 1e-4
):
super(QuadraticDiscriminantAnalysis, self).__init__()
self.priors = priors
self.reg_param = reg_param
self.store_covariance = store_covariance
self.tol = tol
def _classes_means(self, X: torch.Tensor, y: torch.Tensor):
"""
## Description
Compute class means.
## Arguments
* `X` (Tensor) - Input variates.
* `y` (Tensor) - Target covariates.
"""
means = torch.zeros(self.classes_.shape[0], X.shape[1], dtype=X.dtype)
for i in range(self.classes_.shape[0]):
means[i, :] = torch.mean(X[y == i], 0)
return means
def fit(self, X: torch.Tensor, y: torch.Tensor):
"""
## Description
Fit the Quadratic Discriminant Analysis model.
## Arguments
* `X` (Tensor) - Input variates.
* `y` (Tensor) - Target covariates.
## Example
~~~python
qda = QuadraticDiscriminantAnalysis()
qda.fit(X_train, y_train)
~~~
"""
# data validation check
assert X.shape[0] == y.shape[0], "Number of X and y rows don't match"
self.classes_, y = torch.unique(y, return_inverse=True)
n_samples, n_features = X.shape
n_classes = self.classes_.shape[0]
if n_classes < 2:
raise ValueError(
"The number of classes has to be greater than one; got %d class"
% (n_classes)
)
if self.priors is None:
self.priors_ = torch.bincount(y) / float(n_samples)
else:
self.priors_ = self.priors
cov = None
store_covariance = self.store_covariance
if store_covariance:
cov = []
means = []
scalings = []
rotations = []
for ind in range(n_classes):
Xg = X[y == ind, :]
meang = Xg.mean(0)
means.append(meang)
if Xg.shape[0] == 1:
raise ValueError(
"y has only 1 sample in class %s, covariance is ill defined."
% str(self.classes_[ind])
)
Xgc = Xg - meang
_, S, Vt = torch.linalg.svd(Xgc, full_matrices=False)
rank = torch.sum(S > self.tol)
if rank < n_features:
warnings.warn("Variables are collinear")
S2 = (S ** 2) / ((Xg.shape[0]) - 1)
S2 = ((1 - self.reg_param) * S2) + self.reg_param
if self.store_covariance or store_covariance:
cov.append((S2 * Vt.T) @ Vt)
scalings.append(S2)
rotations.append(Vt.T)
if self.store_covariance or store_covariance:
self.covariance_ = cov
self.means_ = torch.stack(means)
self.scalings_ = scalings
self.rotations_ = rotations
return self
def _decision_function(self, X: torch.Tensor):
norm2 = []
for i in range(self.classes_.shape[0]):
R = self.rotations_[i]
S = self.scalings_[i]
Xm = X - self.means_[i]
X2 = Xm @ (R * (S ** (-0.5)))
norm2.append(torch.sum(X2 ** 2, dim=1))
norm2 = torch.stack(norm2).T
u = torch.tensor(
[torch.sum(torch.log(s)) for s in self.scalings_], dtype=X.dtype
)
return -0.5 * (norm2 + u) + torch.log(self.priors_)
def decision_function(self, X: torch.Tensor):
"""
## Description
Apply decision function to an array of samples.
## Arguments
* `X` (Tensor) - Input data.
## Example
~~~python
qda = QuadraticDiscriminantAnalysis()
qda.fit(X_train, y_train)
qda_dec_func = qda.decision_function(X_test)
~~~
"""
dec_func = self._decision_function(X)
# handle special case of two classes
if self.classes_.shape[0] == 2:
return dec_func[:, 1] - dec_func[:, 0]
return dec_func
def predict(self, X: torch.Tensor):
"""
## Description
Predict using Quadratic Discriminant Analysis model.
## Arguments
* `X` (Tensor) - Input variates.
## Example
~~~python
qda = QuadraticDiscriminantAnalysis()
qda.fit(X_train, y_train)
qda_pred = qda.predict(X_test)
~~~
"""
d = self._decision_function(X)
y_pred = self.classes_.take(d.argmax(1))
return y_pred
def predict_proba(self, X: torch.Tensor):
"""
## Description
Calculate and return posterior probabilities of classification.
## Arguments
* `X` (Tensor) - Input data.
## Example
~~~python
qda = QuadraticDiscriminantAnalysis()
qda.fit(X_train, y_train)
qda_predict_proba = qda.predict_proba(X_test)
~~~
"""
values = self._decision_function(X)
# compute the likelihood of the underlying gaussian models
# up to a multiplicative constant.
likelihood = torch.exp(values - torch.max(values, dim=1)[0][:, None])
# compute posterior probabilities
return likelihood / torch.sum(likelihood, dim=1)[:, None]
def predict_log_proba(self, X: torch.Tensor):
"""
## Description
Calculate and return log of posterior probabilities of classification.
## Arguments
* `X` (Tensor) - Input data.
## Example
~~~python
qda = QuadraticDiscriminantAnalysis()
qda.fit(X_train, y_train)
qda_predict_log_proba = qda.predict_log_proba(X_test)
~~~
"""
probas_ = self.predict_proba(X)
return torch.log(probas_)