Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Incompatible with RFECV #189

Open
jcrudy opened this issue Nov 27, 2018 · 1 comment
Open

Incompatible with RFECV #189

jcrudy opened this issue Nov 27, 2018 · 1 comment
Labels

Comments

@jcrudy
Copy link
Collaborator

jcrudy commented Nov 27, 2018

The Earth model seems to be unusable as the estimator in RFECV. See #188 (comment).

@jcrudy jcrudy added the bug label Nov 27, 2018
@hdinh
Copy link

hdinh commented May 11, 2019

The issue is that Earth's coef_ is in the shape of pruned basis, whereas RFECV expects the classifier to have coef_ the shape of num_features.

RFECV will also inspect feature_importances_ if it's available. So using Earth with any of sklearn's feature_selection APIs could work if coef_ was renamed to something like basis_coef_ along with computing feature_importances_

https://github.com/scikit-learn/scikit-learn/blob/ab2f539a32b8099a941cefc598c9625e830ecfe4/sklearn/feature_selection/rfe.py#L186

For example, this hacky proof of concept could work for an example similar to #188 (comment)

from pyearth import Earth
from sklearn.datasets.samples_generator import make_regression
from sklearn.feature_selection import RFECV

class EarthWrapper(object):
    def __init__(self, **kwargs):
        self.earth = Earth(**kwargs)

    def __getstate__(self):
        return self.__dict__

    def __setstate__(self, d):
        self.__dict__ = d

    def __getattr__(self, name):
        if name == 'coef_':
            raise AttributeError # hide from RFECV so that it uses feature_importances_
        elif name == 'earth':
            return self.earth
        elif hasattr(self.earth, name):
            return getattr(self.earth, name)
        raise AttributeError

X, y = make_regression(n_features=2)

model = RFECV(
    estimator=EarthWrapper(
        feature_importance_type='gcv', # feature_importances_ needs to be computed
    ),
    cv=3,
)

model.fit(X, y)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants