|
| 1 | +"""Sum squared of correlation.""" |
| 2 | + |
| 3 | +from numbers import Integral |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +from sklearn.cross_decomposition import CCA |
| 7 | +from sklearn.utils import check_X_y |
| 8 | +from sklearn.utils._param_validation import Interval, validate_params |
| 9 | + |
| 10 | + |
| 11 | +@validate_params( |
| 12 | + { |
| 13 | + "X": ["array-like"], |
| 14 | + "y": ["array-like"], |
| 15 | + }, |
| 16 | + prefer_skip_nested_validation=True, |
| 17 | +) |
| 18 | +def ssc(X, y): |
| 19 | + """Sum of the squared canonical correlation coefficients. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + X : array-like of shape (n_samples, n_features) |
| 24 | + Feature matrix. |
| 25 | +
|
| 26 | + y : array-like of shape (n_samples, n_outputs) |
| 27 | + Target matrix. |
| 28 | +
|
| 29 | + Returns |
| 30 | + ------- |
| 31 | + ssc : float |
| 32 | + Sum of the squared canonical correlation coefficients. |
| 33 | +
|
| 34 | + Examples |
| 35 | + -------- |
| 36 | + >>> from fastcan import ssc |
| 37 | + >>> X = [[1], [-1], [0]] |
| 38 | + >>> y = [[0], [1], [-1]] |
| 39 | + >>> ssc(X, y) |
| 40 | + np.float64(0.25) |
| 41 | + """ |
| 42 | + X, y = check_X_y( |
| 43 | + X, y, dtype=float, ensure_2d=True, multi_output=True, ensure_min_samples=2 |
| 44 | + ) |
| 45 | + n_components = min(X.shape[1], y.shape[1]) |
| 46 | + cca = CCA(n_components=n_components) |
| 47 | + X_c, y_c = cca.fit_transform(X, y) |
| 48 | + corrcoef = np.diagonal(np.corrcoef(X_c, y_c, rowvar=False), offset=n_components) |
| 49 | + return sum(corrcoef**2) |
| 50 | + |
| 51 | + |
| 52 | +@validate_params( |
| 53 | + { |
| 54 | + "X": ["array-like"], |
| 55 | + "y": ["array-like"], |
| 56 | + "t": [Interval(Integral, 1, None, closed="left")], |
| 57 | + }, |
| 58 | + prefer_skip_nested_validation=True, |
| 59 | +) |
| 60 | +def ols(X, y, t=1): |
| 61 | + """Orthogonal least-squares |
| 62 | +
|
| 63 | + Parameters |
| 64 | + ---------- |
| 65 | + X : array-like of shape (n_samples, n_features) |
| 66 | + Feature matrix. |
| 67 | +
|
| 68 | + y : array-like of shape (n_samples,) |
| 69 | + Target vector. |
| 70 | +
|
| 71 | + t : int, default=1 |
| 72 | + The parameter is the absolute number of features to select. |
| 73 | +
|
| 74 | + Returns |
| 75 | + ------- |
| 76 | + indices : ndarray of shape (n_features_to_select,), dtype=int |
| 77 | + The indices of the selected features. The order of the indices |
| 78 | + is corresponding to the feature selection process. |
| 79 | +
|
| 80 | + scores : ndarray of shape (n_features_to_select,), dtype=float |
| 81 | + The scores of selected features. The order of |
| 82 | + the scores is corresponding to the feature selection process. |
| 83 | + """ |
| 84 | + X, y = check_X_y( |
| 85 | + X, y, dtype=float, ensure_2d=True |
| 86 | + ) |
| 87 | + n_features = X.shape[1] |
| 88 | + w = X / np.linalg.norm(X, axis=0) |
| 89 | + v = y / np.linalg.norm(y) |
| 90 | + mask = np.zeros(n_features, dtype=bool) |
| 91 | + r2 = np.zeros(n_features) |
| 92 | + indices = np.zeros(t, dtype=int) |
| 93 | + scores = np.zeros(t, dtype=float) |
| 94 | + |
| 95 | + for i in range(t): |
| 96 | + for j in range(n_features): |
| 97 | + if not mask[j]: |
| 98 | + r = w[:, j] @ v |
| 99 | + r2[j] = r**2 |
| 100 | + d = np.argmax(r2) |
| 101 | + indices[i] = d |
| 102 | + scores[i] = r2[d] |
| 103 | + if i == t-1: |
| 104 | + return indices, scores |
| 105 | + mask[d] = True |
| 106 | + r2[d] = 0 |
| 107 | + for j in range(n_features): |
| 108 | + if not mask[j]: |
| 109 | + w[:, j] = w[:, j] - w[:, d]*(w[:, d] @ w[:, j]) |
| 110 | + w[:, j] /= np.linalg.norm(w[:, j], axis=0) |
0 commit comments