Expand source code
import numpy as np

try:
    from irf.ensemble import wrf
except:
    from sklearn.ensemble import RandomForestClassifier as wrf
from sklearn.base import BaseEstimator


class IRFClassifier(BaseEstimator):
    def __init__(self):
        self.model = wrf()
        self.predict = self.model.predict
        self.predict_proba = self.model.predict_proba
        try:
            import irf
        except:
            raise Warning('irf not installed, defaulting to standard RandomForest. To install, run pip install irf')

    def fit(self, X, y, lambda_reg=0.1, sample_weight=None):
        '''Fit a linear model with integer coefficient and L1 regularization
        
        Params
        ------
        _sample_weight: np.ndarray (n,)
            weight for each individual sample
        '''

        if 'pandas' in str(type(X)):
            X = X.values
        if 'pandas' in str(type(y)):
            y = y.values
        assert type(X) == np.ndarray, 'inputs should be ndarrays'
        assert type(y) == np.ndarray, 'inputs should be ndarrays'

        self.model.fit(X, y, keep_record=False)

Classes

class IRFClassifier

Base class for all estimators in scikit-learn.

Inheriting from this class provides default implementations of:

  • setting and getting parameters used by GridSearchCV and friends;
  • textual and HTML representation displayed in terminals and IDEs;
  • estimator serialization;
  • parameters validation;
  • data validation;
  • feature names validation.

Read more in the :ref:User Guide <rolling_your_own_estimator>.

Notes

All estimators should specify all the parameters that can be set at the class level in their __init__ as explicit keyword arguments (no *args or **kwargs).

Examples

>>> import numpy as np
>>> from sklearn.base import BaseEstimator
>>> class MyEstimator(BaseEstimator):
...     def __init__(self, *, param=1):
...         self.param = param
...     def fit(self, X, y=None):
...         self.is_fitted_ = True
...         return self
...     def predict(self, X):
...         return np.full(shape=X.shape[0], fill_value=self.param)
>>> estimator = MyEstimator(param=2)
>>> estimator.get_params()
{'param': 2}
>>> X = np.array([[1, 2], [2, 3], [3, 4]])
>>> y = np.array([1, 0, 1])
>>> estimator.fit(X, y).predict(X)
array([2, 2, 2])
>>> estimator.set_params(param=3).fit(X, y).predict(X)
array([3, 3, 3])
Expand source code
class IRFClassifier(BaseEstimator):
    def __init__(self):
        self.model = wrf()
        self.predict = self.model.predict
        self.predict_proba = self.model.predict_proba
        try:
            import irf
        except:
            raise Warning('irf not installed, defaulting to standard RandomForest. To install, run pip install irf')

    def fit(self, X, y, lambda_reg=0.1, sample_weight=None):
        '''Fit a linear model with integer coefficient and L1 regularization
        
        Params
        ------
        _sample_weight: np.ndarray (n,)
            weight for each individual sample
        '''

        if 'pandas' in str(type(X)):
            X = X.values
        if 'pandas' in str(type(y)):
            y = y.values
        assert type(X) == np.ndarray, 'inputs should be ndarrays'
        assert type(y) == np.ndarray, 'inputs should be ndarrays'

        self.model.fit(X, y, keep_record=False)

Ancestors

  • sklearn.base.BaseEstimator
  • sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
  • sklearn.utils._metadata_requests._MetadataRequester

Methods

def fit(self, X, y, lambda_reg=0.1, sample_weight=None)

Fit a linear model with integer coefficient and L1 regularization

Params

_sample_weight: np.ndarray (n,) weight for each individual sample

Expand source code
def fit(self, X, y, lambda_reg=0.1, sample_weight=None):
    '''Fit a linear model with integer coefficient and L1 regularization
    
    Params
    ------
    _sample_weight: np.ndarray (n,)
        weight for each individual sample
    '''

    if 'pandas' in str(type(X)):
        X = X.values
    if 'pandas' in str(type(y)):
        y = y.values
    assert type(X) == np.ndarray, 'inputs should be ndarrays'
    assert type(y) == np.ndarray, 'inputs should be ndarrays'

    self.model.fit(X, y, keep_record=False)
def set_fit_request(self: IRFClassifier, *, lambda_reg: Union[bool, ForwardRef(None), str] = '$UNCHANGED$', sample_weight: Union[bool, ForwardRef(None), str] = '$UNCHANGED$') ‑> IRFClassifier

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see :func:sklearn.set_config). Please see :ref:User Guide <metadata_routing> on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version: 1.3

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a :class:~sklearn.pipeline.Pipeline. Otherwise it has no effect.

Parameters

lambda_reg : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for lambda_reg parameter in fit.
sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for sample_weight parameter in fit.

Returns

self : object
The updated object.
Expand source code
def func(*args, **kw):
    """Updates the request for provided parameters

    This docstring is overwritten below.
    See REQUESTER_DOC for expected functionality
    """
    if not _routing_enabled():
        raise RuntimeError(
            "This method is only available when metadata routing is enabled."
            " You can enable it using"
            " sklearn.set_config(enable_metadata_routing=True)."
        )

    if self.validate_keys and (set(kw) - set(self.keys)):
        raise TypeError(
            f"Unexpected args: {set(kw) - set(self.keys)} in {self.name}. "
            f"Accepted arguments are: {set(self.keys)}"
        )

    # This makes it possible to use the decorated method as an unbound method,
    # for instance when monkeypatching.
    # https://github.com/scikit-learn/scikit-learn/issues/28632
    if instance is None:
        _instance = args[0]
        args = args[1:]
    else:
        _instance = instance

    # Replicating python's behavior when positional args are given other than
    # `self`, and `self` is only allowed if this method is unbound.
    if args:
        raise TypeError(
            f"set_{self.name}_request() takes 0 positional argument but"
            f" {len(args)} were given"
        )

    requests = _instance._get_metadata_request()
    method_metadata_request = getattr(requests, self.name)

    for prop, alias in kw.items():
        if alias is not UNCHANGED:
            method_metadata_request.add_request(param=prop, alias=alias)
    _instance._metadata_request = requests

    return _instance