Fit the model with X. Samples random projection according to n_features. Parameters ---------- X : array-like, shape (n_samples, n_features) Training data, where `n_samples` is the number of samples and `n_features` is the number of features.
(self, X, y=None)
| 510 | |
| 511 | @_fit_context(prefer_skip_nested_validation=True) |
| 512 | def fit(self, X, y=None): |
| 513 | """Fit the model with X. |
| 514 | |
| 515 | Samples random projection according to n_features. |
| 516 | |
| 517 | Parameters |
| 518 | ---------- |
| 519 | X : array-like, shape (n_samples, n_features) |
| 520 | Training data, where `n_samples` is the number of samples |
| 521 | and `n_features` is the number of features. |
| 522 | |
| 523 | y : array-like, shape (n_samples,) or (n_samples, n_outputs), \ |
| 524 | default=None |
| 525 | Target values (None for unsupervised transformations). |
| 526 | |
| 527 | Returns |
| 528 | ------- |
| 529 | self : object |
| 530 | Returns the instance itself. |
| 531 | """ |
| 532 | X = validate_data(self, X) |
| 533 | random_state = check_random_state(self.random_state) |
| 534 | n_features = X.shape[1] |
| 535 | uniform = random_state.uniform(size=(n_features, self.n_components)) |
| 536 | # transform by inverse CDF of sech |
| 537 | self.random_weights_ = 1.0 / np.pi * np.log(np.tan(np.pi / 2.0 * uniform)) |
| 538 | self.random_offset_ = random_state.uniform(0, 2 * np.pi, size=self.n_components) |
| 539 | |
| 540 | if X.dtype == np.float32: |
| 541 | # Setting the data type of the fitted attribute will ensure the |
| 542 | # output data type during `transform`. |
| 543 | self.random_weights_ = self.random_weights_.astype(X.dtype, copy=False) |
| 544 | self.random_offset_ = self.random_offset_.astype(X.dtype, copy=False) |
| 545 | |
| 546 | self._n_features_out = self.n_components |
| 547 | return self |
| 548 | |
| 549 | def transform(self, X): |
| 550 | """Apply the approximate feature map to X. |