Retrieve the coefficients of an estimator ending with a Linear Model. This is typically useful to retrieve "spatial filters" or "spatial patterns" of decoding models :footcite:`HaufeEtAl2014`. Parameters ---------- estimator : object | None An estimator from scikit-lear
(
estimator, attr="filters_", inverse_transform=False, *, step_name=None, verbose=None
)
| 685 | |
| 686 | @verbose |
| 687 | def get_coef( |
| 688 | estimator, attr="filters_", inverse_transform=False, *, step_name=None, verbose=None |
| 689 | ): |
| 690 | """Retrieve the coefficients of an estimator ending with a Linear Model. |
| 691 | |
| 692 | This is typically useful to retrieve "spatial filters" or "spatial |
| 693 | patterns" of decoding models :footcite:`HaufeEtAl2014`. |
| 694 | |
| 695 | Parameters |
| 696 | ---------- |
| 697 | estimator : object | None |
| 698 | An estimator from scikit-learn. |
| 699 | attr : str |
| 700 | The name of the coefficient attribute to retrieve, typically |
| 701 | ``'filters_'`` (default) or ``'patterns_'``. |
| 702 | inverse_transform : bool |
| 703 | If True, returns the coefficients after inverse transforming them with |
| 704 | the transformer steps of the estimator. |
| 705 | step_name : str | None |
| 706 | Name of the sklearn's pipeline step to get the coef from. |
| 707 | If inverse_transform is True, the inverse transformations |
| 708 | will be applied using transformers before this step. |
| 709 | If None, the last step will be used. Defaults to None. |
| 710 | |
| 711 | .. versionadded:: 1.11 |
| 712 | %(verbose)s |
| 713 | |
| 714 | Returns |
| 715 | ------- |
| 716 | coef : array |
| 717 | The coefficients. |
| 718 | |
| 719 | References |
| 720 | ---------- |
| 721 | .. footbibliography:: |
| 722 | """ |
| 723 | # Get the coefficients of the last estimator in case of nested pipeline |
| 724 | est = estimator |
| 725 | logger.debug(f"Getting coefficients from estimator: {est.__class__.__name__}") |
| 726 | |
| 727 | if step_name is not None: |
| 728 | if not hasattr(estimator, "named_steps"): |
| 729 | raise ValueError("step_name can only be used with a pipeline estimator.") |
| 730 | try: |
| 731 | est = est.get_params(deep=True)[step_name] |
| 732 | except KeyError: |
| 733 | raise ValueError(f"Step '{step_name}' is not part of the pipeline.") |
| 734 | else: |
| 735 | while hasattr(est, "steps"): |
| 736 | est = est.steps[-1][1] |
| 737 | |
| 738 | squeeze_first_dim = False |
| 739 | |
| 740 | # If SlidingEstimator, loop across estimators |
| 741 | if hasattr(est, "estimators_"): |
| 742 | coef = list() |
| 743 | for ei, this_est in enumerate(est.estimators_): |
| 744 | if ei == 0: |