|
| 1 | +""" |
| 2 | +Feature selection with mini-batch |
| 3 | +""" |
| 4 | + |
| 5 | +from copy import deepcopy |
| 6 | +from numbers import Integral |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +from sklearn.utils._openmp_helpers import _openmp_effective_n_threads |
| 10 | +from sklearn.utils._param_validation import Interval, validate_params |
| 11 | +from sklearn.utils.validation import check_X_y |
| 12 | + |
| 13 | +from ._cancorr_fast import _forward_search # type: ignore |
| 14 | +from ._fastcan import FastCan, _prepare_search |
| 15 | + |
| 16 | + |
| 17 | +@validate_params( |
| 18 | + { |
| 19 | + "X": ["array-like"], |
| 20 | + "y": ["array-like"], |
| 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 | + "verbose": ["verbose"], |
| 28 | + }, |
| 29 | + prefer_skip_nested_validation=False, |
| 30 | +) |
| 31 | +def minibatch(X, y, n_features_to_select=1, batch_size=1, verbose=1): |
| 32 | + """FastCan selection with mini batches. |
| 33 | +
|
| 34 | + It is suitable for selecting a very large number of features |
| 35 | + even larger than the number of samples. |
| 36 | +
|
| 37 | + Similar to the correlation filter which selects each feature without considering |
| 38 | + the redundancy, the function selects features in mini-batch and the |
| 39 | + redundancy between the two mini-batches will be ignored. |
| 40 | +
|
| 41 | + Parameters |
| 42 | + ---------- |
| 43 | + X : array-like of shape (n_samples, n_features) |
| 44 | + Feature matrix. |
| 45 | +
|
| 46 | + y : array-like of shape (n_samples, n_outputs) |
| 47 | + Target matrix. |
| 48 | +
|
| 49 | + n_features_to_select : int, default=1 |
| 50 | + The parameter is the absolute number of features to select. |
| 51 | +
|
| 52 | + batch_size : int, default=1 |
| 53 | + The number of features in a mini-batch. |
| 54 | + It is recommended that batch_size be less than n_samples. |
| 55 | +
|
| 56 | + verbose : int, default=1 |
| 57 | + The verbosity level. |
| 58 | +
|
| 59 | + Returns |
| 60 | + ------- |
| 61 | + indices : ndarray of shape (n_features_to_select,), dtype=int |
| 62 | + The indices of the selected features. |
| 63 | +
|
| 64 | + Examples |
| 65 | + -------- |
| 66 | + >>> from fastcan import minibatch |
| 67 | + >>> X = [[1, 1, 0], [0.01, 0, 0], [-1, 0, 1], [0, 0, 0]] |
| 68 | + >>> y = [1, 0, -1, 0] |
| 69 | + >>> indices = minibatch(X, y, 3, batch_size=2, verbose=0) |
| 70 | + >>> print(f"Indices: {indices}") |
| 71 | + Indices: [0 1 2] |
| 72 | + """ |
| 73 | + X, y = check_X_y(X, y, ensure_2d=True, multi_output=True) |
| 74 | + if y.ndim == 1: |
| 75 | + y = y.reshape(-1, 1) |
| 76 | + |
| 77 | + n_features = X.shape[1] |
| 78 | + n_outputs = y.shape[1] |
| 79 | + |
| 80 | + if n_features_to_select > n_features: |
| 81 | + raise ValueError( |
| 82 | + f"n_features_to_select {n_features_to_select} " |
| 83 | + f"must be <= n_features {n_features}." |
| 84 | + ) |
| 85 | + |
| 86 | + n_threads = _openmp_effective_n_threads() |
| 87 | + |
| 88 | + n_to_select_split = np.diff( |
| 89 | + np.linspace( |
| 90 | + 0, n_features_to_select, num=n_outputs + 1, endpoint=True, dtype=int |
| 91 | + ) |
| 92 | + ) |
| 93 | + indices_select = np.zeros(0, dtype=int) |
| 94 | + for i in range(n_outputs): |
| 95 | + y_i = y[:, i] |
| 96 | + batch_split_i = np.diff( |
| 97 | + np.r_[ |
| 98 | + np.arange(n_to_select_split[i], step=batch_size, dtype=int), |
| 99 | + n_to_select_split[i], |
| 100 | + ] |
| 101 | + ) |
| 102 | + for j, batch_size_j in enumerate(batch_split_i): |
| 103 | + if j == 0: |
| 104 | + selector_j = FastCan( |
| 105 | + batch_size_j, indices_exclude=indices_select, verbose=0 |
| 106 | + ).fit(X, y_i) |
| 107 | + X_transformed_ = deepcopy(selector_j.X_transformed_) |
| 108 | + indices = selector_j.indices_ |
| 109 | + else: |
| 110 | + indices, scores, mask = _prepare_search( |
| 111 | + n_features, |
| 112 | + batch_size_j, |
| 113 | + selector_j.indices_include_, |
| 114 | + np.r_[selector_j.indices_exclude_, indices_select], |
| 115 | + ) |
| 116 | + _forward_search( |
| 117 | + X=X_transformed_, |
| 118 | + V=selector_j.y_transformed_, |
| 119 | + t=batch_size_j, |
| 120 | + tol=selector_j.tol, |
| 121 | + num_threads=n_threads, |
| 122 | + verbose=0, |
| 123 | + mask=mask, |
| 124 | + indices=indices, |
| 125 | + scores=scores, |
| 126 | + ) |
| 127 | + indices_select = np.r_[indices_select, indices] |
| 128 | + if verbose == 1: |
| 129 | + print( |
| 130 | + f"Progress: {indices_select.size}/{n_features_to_select}", end="\r" |
| 131 | + ) |
| 132 | + if verbose == 1: |
| 133 | + print() |
| 134 | + return indices_select |
0 commit comments