-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtransformers.py
More file actions
342 lines (276 loc) · 12.3 KB
/
transformers.py
File metadata and controls
342 lines (276 loc) · 12.3 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# Copyright 2023 D-Wave Systems Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import itertools
import logging
import tempfile
import typing
import warnings
import dimod
import numpy as np
import numpy.typing as npt
from dwave.cloud.exceptions import ConfigFileError, SolverAuthenticationError
from dwave.system import LeapHybridCQMSampler
from sklearn.base import BaseEstimator
from sklearn.feature_selection import SelectorMixin
from sklearn.utils.validation import check_is_fitted
from dwave.plugins.sklearn.utilities import corrcoef
from dwave.plugins.sklearn.nearest_neighbors import calculate_mi
__all__ = ["SelectFromQuadraticModel"]
class SelectFromQuadraticModel(SelectorMixin, BaseEstimator):
"""Select features using a quadratic optimization problem solved on a hybrid solver.
Args:
alpha:
Hyperparameter between 0 and 1 that controls the relative weight of
the relevance and redundancy terms.
``alpha=0`` places no weight on the quality of the features,
therefore the features will be selected as to minimize the
redundancy without any consideration to quality.
``alpha=1`` places the maximum weight on the quality of the features,
and therefore will be equivalent to using
:class:`sklearn.feature_selection.SelectKBest`.
num_features:
The number of features to select.
time_limit:
The time limit for the run on the hybrid solver.
"""
ACCEPTED_METHODS = [
"correlation",
# "mutual information", # todo
]
def __init__(
self,
*,
alpha: float = .5,
method: str = "correlation", # undocumented until there is another supported
num_features: int = 10,
time_limit: typing.Optional[float] = None,
):
if not 0 <= alpha <= 1:
raise ValueError(f"alpha must be between 0 and 1, given {alpha}")
if method not in self.ACCEPTED_METHODS:
raise ValueError(
f"method must be one of {self.ACCEPTED_METHODS}, given {method}"
)
if num_features <= 0:
raise ValueError(f"num_features must be a positive integer, given {num_features}")
self.alpha = alpha
self.method = method
self.num_features = num_features
self.time_limit = time_limit # check this lazily
def __sklearn_is_fitted__(self) -> bool:
# used by `check_is_fitted()`
try:
self._mask
except AttributeError:
return False
return True
def _get_support_mask(self) -> np.ndarray[typing.Any, np.dtype[np.bool_]]:
"""Get the boolean mask indicating which features are selected
Returns:
boolean array of shape [# input features]. An element is True iff its
corresponding feature is selected for retention.
Raises:
RuntimeError: This method will raise an error if it is run before `fit`
"""
check_is_fitted(self)
try:
return self._mask
except AttributeError:
raise RuntimeError("fit hasn't been run yet")
@staticmethod
def correlation_cqm(
X: npt.ArrayLike,
y: npt.ArrayLike,
*,
alpha: float,
num_features: int,
strict: bool = True,
) -> dimod.ConstrainedQuadraticModel:
"""Build a constrained quadratic model for feature selection.
This method is based on maximizing influence and independence as
measured by correlation [Milne et al.]_.
Args:
X:
Feature vectors formatted as a numerical 2D array-like.
y:
Class labels formatted as a numerical 1D array-like.
alpha:
Hyperparameter between 0 and 1 that controls the relative weight of
the relevance and redundancy terms.
``alpha=0`` places no weight on the quality of the features,
therefore the features will be selected as to minimize the
redundancy without any consideration to quality.
``alpha=1`` places the maximum weight on the quality of the features,
and therefore will be equivalent to using
:class:`sklearn.feature_selection.SelectKBest`.
num_features:
The number of features to select.
strict:
If ``False`` the constraint on the number of selected features
is ``<=`` rather than ``==``.
Returns:
A constrained quadratic model.
.. [Milne et al.] Milne, Andrew, Maxwell Rounds, and Phil Goddard. 2017. "Optimal Feature
Selection in Credit Scoring and Classification Using a Quantum Annealer."
1QBit; White Paper.
https://1qbit.com/whitepaper/optimal-feature-selection-in-credit-scoring-classification-using-quantum-annealer
"""
X = np.atleast_2d(np.asarray(X))
y = np.asarray(y)
if X.ndim != 2:
raise ValueError("X must be a 2-dimensional array-like")
if y.ndim != 1:
raise ValueError("y must be a 1-dimensional array-like")
if y.shape[0] != X.shape[0]:
raise ValueError(f"requires: X.shape[0] == y.shape[0] but {X.shape[0]} != {y.shape[0]}")
if not 0 <= alpha <= 1:
raise ValueError(f"alpha must be between 0 and 1, given {alpha}")
if num_features <= 0:
raise ValueError(f"num_features must be a positive integer, given {num_features}")
if X.shape[0] <= 1:
raise ValueError("X must have at least two rows")
cqm = dimod.ConstrainedQuadraticModel()
cqm.add_variables(dimod.BINARY, X.shape[1])
# add the k-hot constraint
cqm.add_constraint(
((v, 1) for v in cqm.variables),
'==' if strict else '<=',
num_features,
label=f"{num_features}-hot",
)
with tempfile.TemporaryFile() as fX, tempfile.TemporaryFile() as fout:
# we make a copy of X because we'll be modifying it in-place within
# some of the functions
X_copy = np.memmap(fX, X.dtype, mode="w+", shape=(X.shape[0], X.shape[1] + 1))
X_copy[:, :-1] = X
X_copy[:, -1] = y
# make the matrix that will hold the correlations
correlations = np.memmap(
fout,
dtype=np.result_type(X, y),
mode="w+",
shape=(X_copy.shape[1], X_copy.shape[1]),
)
# main calculation. It modifies X_copy in-place
corrcoef(X_copy, out=correlations, rowvar=False, copy=False)
# we don't care about the direction of correlation in terms of
# the penalty/quality
np.absolute(correlations, out=correlations)
# our objective
# we multiply by 2 because the matrix is symmetric
np.fill_diagonal(correlations, correlations[:, -1] * (-2 * alpha * num_features))
# Note: the full symmetric matrix (with both upper- and lower-diagonal
# entries for each correlation coefficient) is retained for consistency with
# the original formulation from Milne et al.
it = np.nditer(correlations[:-1, :-1], flags=['multi_index'], op_flags=[['readonly']])
cqm.set_objective((*it.multi_index, x) for x in it if x)
return cqm
@staticmethod
def mutual_information_cqm(
X: npt.ArrayLike,
y: npt.ArrayLike,
*,
alpha: float,
num_features: int,
strict: bool = True,
) -> dimod.ConstrainedQuadraticModel:
cqm = dimod.ConstrainedQuadraticModel()
cqm.add_variables(dimod.BINARY, X.shape[1])
# add the k-hot constraint
cqm.add_constraint(
((v, 1) for v in cqm.variables),
'==' if strict else '<=',
num_features,
label=f"{num_features}-hot",
)
mi_matrix = calculate_mi(X, y)
it = np.nditer(mi_matrix, flags=['multi_index'], op_flags=[['readonly']])
cqm.set_objective((*it.multi_index, x) for x in it if x)
return cqm
def fit(
self,
X: npt.ArrayLike,
y: npt.ArrayLike,
*,
alpha: typing.Optional[float] = None,
num_features: typing.Optional[int] = None,
time_limit: typing.Optional[float] = None,
) -> SelectFromQuadraticModel:
"""Select the features to keep.
Args:
X:
Feature vectors formatted as a numerical 2D array-like.
y:
Class labels formatted as a numerical 1D array-like.
alpha:
Hyperparameter between 0 and 1 that controls the relative weight of
the relevance and redundancy terms.
``alpha=0`` places no weight on the quality of the features,
therefore the features will be selected as to minimize the
redundancy without any consideration to quality.
``alpha=1`` places the maximum weight on the quality of the features,
and therefore will be equivalent to using
:class:`sklearn.feature_selection.SelectKBest`.
num_features:
The number of features to select.
Defaults to the value provided to the constructor.
time_limit:
The time limit for the run on the hybrid solver.
Defaults to the value provided to the constructor.
Returns:
This instance of `SelectFromQuadraticModel`.
"""
X = np.atleast_2d(np.asarray(X))
if X.ndim != 2:
raise ValueError("X must be a 2-dimensional array-like")
# y is checked by the correlation method function
if alpha is None:
alpha = self.alpha
# alpha is checked by the correlation method function
if num_features is None:
num_features = self.num_features
# num_features is checked by the correlation method function
# time_limit is checked by the LeapHybridCQMSampelr
# if we already have fewer features than requested, just return
if num_features >= X.shape[1]:
self._mask = np.ones(X.shape[1], dtype=bool)
return self
if self.method == "correlation":
cqm = self.correlation_cqm(X, y, num_features=num_features, alpha=alpha)
elif self.method == "mutual information":
cqm = self.mutual_information_cqm(X, y, num_features=num_features)
else:
raise ValueError(f"only methods {self.acceptable_methods} are implemented")
try:
sampler = LeapHybridCQMSampler()
except (ConfigFileError, SolverAuthenticationError) as e:
raise RuntimeError(
f"""Instantiation of a Leap hybrid solver failed with an {e} error.
See https://docs.ocean.dwavesys.com/en/stable/overview/sapi.html for configuring
access to Leap’s solvers.
"""
)
sampleset = sampler.sample_cqm(cqm, time_limit=self.time_limit,
label=f"{self.__module__}.{type(self).__qualname__}")
filtered = sampleset.filter(lambda d: d.is_feasible)
if len(filtered) == 0:
raise RuntimeError("no feasible solutions found by the hybrid solver")
lowest = filtered.first.sample
self._mask = np.fromiter((lowest[v] for v in cqm.variables),
count=cqm.num_variables(), dtype=bool)
return self
def unfit(self):
"""Undo a previously executed ``fit`` method."""
del self._mask