|
| 1 | +""" |
| 2 | +Extend feature selection |
| 3 | +""" |
| 4 | + |
| 5 | +import math |
| 6 | +from copy import deepcopy |
| 7 | +from numbers import Integral |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +from sklearn.utils._openmp_helpers import _openmp_effective_n_threads |
| 11 | +from sklearn.utils._param_validation import Interval, validate_params |
| 12 | +from sklearn.utils.validation import check_is_fitted |
| 13 | + |
| 14 | +from ._cancorr_fast import _forward_search # type: ignore |
| 15 | +from ._fastcan import FastCan, _prepare_search |
| 16 | + |
| 17 | + |
| 18 | +@validate_params( |
| 19 | + { |
| 20 | + "selector": [FastCan], |
| 21 | + "n_features_to_select": [ |
| 22 | + Interval(Integral, 1, None, closed="left"), |
| 23 | + ], |
| 24 | + "batch_size": [ |
| 25 | + Interval(Integral, 1, None, closed="left"), |
| 26 | + ], |
| 27 | + }, |
| 28 | + prefer_skip_nested_validation=False, |
| 29 | +) |
| 30 | +def extend(selector, n_features_to_select=1, batch_size=1): |
| 31 | + """Extend FastCan with mini batches. |
| 32 | +
|
| 33 | + It is suitable for selecting a very large number of features |
| 34 | + even larger than the number of samples. |
| 35 | +
|
| 36 | + Similar to the correlation filter which selects each feature without considering |
| 37 | + the redundancy, the function selects features in mini-batch and the |
| 38 | + redundancy between the two mini-batches will be ignored. |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + selector : FastCan |
| 43 | + FastCan selector. |
| 44 | +
|
| 45 | + n_features_to_select : int, default=1 |
| 46 | + The parameter is the absolute number of features to select. |
| 47 | +
|
| 48 | + batch_size : int, default=1 |
| 49 | + The number of features in a mini-batch. |
| 50 | +
|
| 51 | + Returns |
| 52 | + ------- |
| 53 | + indices : ndarray of shape (n_features_to_select,), dtype=int |
| 54 | + The indices of the selected features. |
| 55 | +
|
| 56 | + Examples |
| 57 | + -------- |
| 58 | + >>> from fastcan import FastCan, extend |
| 59 | + >>> X = [[1, 1, 0], [0.01, 0, 0], [-1, 0, 1], [0, 0, 0]] |
| 60 | + >>> y = [1, 0, -1, 0] |
| 61 | + >>> selector = FastCan(1, verbose=0).fit(X, y) |
| 62 | + >>> print(f"Indices: {selector.indices_}") |
| 63 | + Indices: [0] |
| 64 | + >>> indices = extend(selector, 3, batch_size=2) |
| 65 | + >>> print(f"Indices: {indices}") |
| 66 | + Indices: [0 2 1] |
| 67 | + """ |
| 68 | + check_is_fitted(selector) |
| 69 | + n_inclusions = selector.indices_include_.size |
| 70 | + n_features = selector.n_features_in_ |
| 71 | + n_to_select = n_features_to_select - selector.n_features_to_select |
| 72 | + batch_size_to_select = batch_size - n_inclusions |
| 73 | + |
| 74 | + if n_features_to_select > n_features: |
| 75 | + raise ValueError( |
| 76 | + f"n_features_to_select {n_features_to_select} " |
| 77 | + f"must be <= n_features {n_features}." |
| 78 | + ) |
| 79 | + if n_to_select <= 0: |
| 80 | + raise ValueError( |
| 81 | + f"The number of features to select ({n_to_select}) ", "is less than 0." |
| 82 | + ) |
| 83 | + if batch_size_to_select <= 0: |
| 84 | + raise ValueError( |
| 85 | + "The size of mini batch without included indices ", |
| 86 | + f"({batch_size_to_select}) is less than 0.", |
| 87 | + ) |
| 88 | + |
| 89 | + X_transformed_ = deepcopy(selector.X_transformed_) |
| 90 | + |
| 91 | + indices_include = selector.indices_include_ |
| 92 | + indices_exclude = selector.indices_exclude_ |
| 93 | + indices_select = selector.indices_[n_inclusions:] |
| 94 | + |
| 95 | + n_threads = _openmp_effective_n_threads() |
| 96 | + |
| 97 | + for i in range(math.ceil(n_to_select / batch_size_to_select)): |
| 98 | + if i == 0: |
| 99 | + batch_size_i = (n_to_select - 1) % batch_size_to_select + 1 + n_inclusions |
| 100 | + else: |
| 101 | + batch_size_i = batch_size |
| 102 | + indices, scores, mask = _prepare_search( |
| 103 | + n_features, |
| 104 | + batch_size_i, |
| 105 | + indices_include, |
| 106 | + np.r_[indices_exclude, indices_select], |
| 107 | + ) |
| 108 | + _forward_search( |
| 109 | + X=X_transformed_, |
| 110 | + V=selector.y_transformed_, |
| 111 | + t=batch_size_i, |
| 112 | + tol=selector.tol, |
| 113 | + num_threads=n_threads, |
| 114 | + verbose=0, |
| 115 | + mask=mask, |
| 116 | + indices=indices, |
| 117 | + scores=scores, |
| 118 | + ) |
| 119 | + indices_select = np.r_[indices_select, indices[n_inclusions:]] |
| 120 | + return np.r_[indices_include, indices_select] |
0 commit comments