|
| 1 | +""" |
| 2 | +custom mode basis class. |
| 3 | +""" |
| 4 | +from ._base import InvertibleBasis |
| 5 | +from ._base import MatrixMixin |
| 6 | + |
| 7 | +class Custom(InvertibleBasis, MatrixMixin): |
| 8 | + """ |
| 9 | +<<<<<<< HEAD |
| 10 | + Use a custom transformation to maps input features to |
| 11 | +======= |
| 12 | + Generate a custom transformation which maps input features to |
| 13 | +>>>>>>> 1ccd23fafed0ca39dac6cde204efa228d133df1c |
| 14 | + custom modes. |
| 15 | +
|
| 16 | + Assumes the data has already been centered (to have mean 0). |
| 17 | +
|
| 18 | + Parameters |
| 19 | + ---------- |
| 20 | + n_basis_modes : int, optional (default 10) |
| 21 | + Number of basis modes to retain. Cannot be larger than |
| 22 | + the number of features ``n_features``, or the number of examples |
| 23 | + ``n_examples``. |
| 24 | + U: The custom basis matrix |
| 25 | +
|
| 26 | + Attributes |
| 27 | + ---------- |
| 28 | + basis_matrix_ : numpy ndarray, shape (n_features, n_basis_modes) |
| 29 | + The top n_basis_modes left singular vectors of the training data. |
| 30 | +
|
| 31 | + """ |
| 32 | + |
| 33 | + def __init__(self, U, n_basis_modes=10, **kwargs): |
| 34 | + if isinstance(n_basis_modes, int) and n_basis_modes > 0: |
| 35 | + super(Custom, self).__init__()#n_components=n_basis_modes, **kwargs |
| 36 | + self._n_basis_modes = n_basis_modes |
| 37 | + self.custom_basis_ = U |
| 38 | + else: |
| 39 | + raise ValueError("n_basis_modes must be a positive integer.") |
| 40 | + |
| 41 | + def fit(self, X): |
| 42 | + """ |
| 43 | + Parameters |
| 44 | + ---------- |
| 45 | + X : array-like, shape (n_samples, n_features) |
| 46 | + The training data. |
| 47 | +
|
| 48 | + Returns |
| 49 | + ------- |
| 50 | + self : instance |
| 51 | + """ |
| 52 | + # self.basis_matrix_ = self.custom_basis_[:,: self.n_basis_modes] @ self.custom_basis_[:,: self.n_basis_modes].T @ X[: self.n_basis_modes, :].T.copy() |
| 53 | + # self.basis_matrix_ = self.custom_basis_ @ self.custom_basis_.T @ X[: self.n_basis_modes, :].T.copy() |
| 54 | + self.basis_matrix_ = self.custom_basis_[:,:self.n_basis_modes] |
| 55 | + # self.basis_matrix_ = (X @ self.custom_basis_[:,:self.n_basis_modes] @ self.custom_basis_[:,:self.n_basis_modes].T)[:self.n_basis_modes,:].T |
| 56 | + |
| 57 | + # self.basis_matrix_ = ((X @ self.custom_basis_).T)[:,:self.n_basis_modes] |
| 58 | + # self.basis_matrix_ = ((X @ self.custom_basis_ @ self.custom_basis_.T).T)[:,:self.n_basis_modes] |
| 59 | + return self |
| 60 | + |
| 61 | + def matrix_inverse(self, n_basis_modes=None): |
| 62 | + """ |
| 63 | + Get the inverse matrix mapping from measurement space to |
| 64 | + coordinates with respect to the basis. |
| 65 | +
|
| 66 | + Note that this is not the inverse of the matrix returned by |
| 67 | + ``self.matrix_representation``. It is the (pseudo) inverse of |
| 68 | + the matrix whose columns are the basis modes. |
| 69 | +
|
| 70 | + Parameters |
| 71 | + ---------- |
| 72 | + n_basis_modes : positive int, optional (default None) |
| 73 | + Number of basis modes to be used to compute inverse. |
| 74 | +
|
| 75 | + Returns |
| 76 | + ------- |
| 77 | + B : numpy ndarray, shape (n_basis_modes, n_features) |
| 78 | + The inverse matrix. |
| 79 | + """ |
| 80 | + n_basis_modes = self._validate_input(n_basis_modes) |
| 81 | + |
| 82 | + return self.basis_matrix_[:, :n_basis_modes].T |
| 83 | + |
| 84 | + @property |
| 85 | + def n_basis_modes(self): |
| 86 | + """Number of basis modes.""" |
| 87 | + return self._n_basis_modes |
| 88 | + |
| 89 | + @n_basis_modes.setter |
| 90 | + def n_basis_modes(self, n_basis_modes): |
| 91 | + self._n_basis_modes = n_basis_modes |
| 92 | + self.n_components = n_basis_modes |
0 commit comments