Estimate the coefficients of the linear model. Save the coefficients in the attribute ``filters_`` and computes the attribute ``patterns_``. Parameters ---------- X : array, shape (n_samples, n_features) The training input samples to estimate the
(self, X, y, **fit_params)
| 512 | pass |
| 513 | |
| 514 | def fit(self, X, y, **fit_params): |
| 515 | """Estimate the coefficients of the linear model. |
| 516 | |
| 517 | Save the coefficients in the attribute ``filters_`` and |
| 518 | computes the attribute ``patterns_``. |
| 519 | |
| 520 | Parameters |
| 521 | ---------- |
| 522 | X : array, shape (n_samples, n_features) |
| 523 | The training input samples to estimate the linear coefficients. |
| 524 | y : array, shape (n_samples, [n_targets]) |
| 525 | The target values. |
| 526 | **fit_params : dict of string -> object |
| 527 | Parameters to pass to the fit method of the estimator. |
| 528 | |
| 529 | Returns |
| 530 | ------- |
| 531 | self : instance of LinearModel |
| 532 | Returns the modified instance. |
| 533 | """ |
| 534 | self._validate_params(X) |
| 535 | X, y = validate_data(self, X, y, multi_output=True) |
| 536 | |
| 537 | # fit the Model |
| 538 | self.model_ = ( |
| 539 | clone(self.model) |
| 540 | if self.model is not None |
| 541 | else LogisticRegression(solver="liblinear") |
| 542 | ) |
| 543 | self.model_.fit(X, y, **fit_params) |
| 544 | |
| 545 | # Computes patterns using Haufe's trick: A = Cov_X . W . Precision_Y |
| 546 | inv_Y = 1.0 |
| 547 | X = X - X.mean(0, keepdims=True) |
| 548 | if y.ndim == 2 and y.shape[1] != 1: |
| 549 | y = y - y.mean(0, keepdims=True) |
| 550 | inv_Y = np.linalg.pinv(np.cov(y.T)) |
| 551 | self.patterns_ = np.cov(X.T).dot(self.filters_.T.dot(inv_Y)).T |
| 552 | |
| 553 | return self |
| 554 | |
| 555 | @property |
| 556 | def filters_(self): |