<commit_before>"""Weight Boosting

This module contains weight boosting estimators for both classification and
regression.

The module structure is the following:

- The ``BaseAdaBoost`` base class implements a common ``fit`` method
  for all the estimators in the module. Regression and classification
  only differ the the concrete ``LossFunction`` used.

- ``AdaBoostClassifier`` implements adaptive boosting (AdaBoost-SAMME) for
  classification problems.

- ``AdaBoostRegressor`` implements adaptive boosting (AdaBoost.R2) for
  regression problems.
"""

# Authors: <NAME>
# License: BSD Style

import numpy as np

from .base import BaseEnsemble
from ..base import ClassifierMixin, RegressorMixin, \
                   WeightedClassifierMixin, WeightedRegressorMixin
from ..metrics import weighted_r2_score
from ..tree import DecisionTreeClassifier, DecisionTreeRegressor
from ..tree._tree import DTYPE
from ..utils import array2d, check_arrays


__all__ = [
    'AdaBoostClassifier',
    'AdaBoostRegressor',
]


class BaseWeightBoosting(BaseEnsemble):
    """Abstract base class for weight boosting.

    Parameters
    ----------
    base_estimator : object, optional (default=DecisionTreeClassifier)
        The base estimator from which the ensemble is built.

    n_estimators : integer, optional (default=10)
        The maximum number of estimators at which boosting is terminated.

    learn_rate : float, optional (default=0.5)
        learning rate shrinks the contribution of each classifier by
        `learn_rate`. There is a trade-off between learn_rate and n_estimators.

    compute_importances : boolean, optional (default=False)
        Whether feature importances are computed and stored into the
        ``feature_importances_`` attribute when calling fit.

    Attributes
    ----------
    `feature_importances_` : array of shape = [n_features]
        The feature importances (the higher, the more important the feature).
        The importance I(f) of a feature f is computed as the (normalized)
        total reduction of error brought by that feature. It is also known as
        the Gini importance [1]_.

    References
    ----------

    .. [1] <NAME>, and A. Cutler, "Random Forests",
           http://www.stat.berkeley.edu/~breiman/RandomForests/cc_home.htm
    """
    def __init__(self, base_estimator=None,
                 n_estimators=10,
                 learn_rate=.5,
                 compute_importances=False):
        self.boost_weights_ = []
        self.errs_ = []
        self.learn_rate = learn_rate
        self.compute_importances = compute_importances
        self.feature_importances_ = None

        if base_estimator is None:
            if isinstance(self, ClassifierMixin):
                base_estimator = DecisionTreeClassifier(max_depth=3)
            else:
                base_estimator = DecisionTreeRegressor(max_depth=3)
        elif (isinstance(self, ClassifierMixin)
              and not isinstance(base_estimator, ClassifierMixin)):
            raise TypeError("base_estimator must be a "
                            "subclass of ClassifierMixin")
        elif (isinstance(self, RegressorMixin)
              and not isinstance(base_estimator, RegressorMixin)):
            raise TypeError("base_estimator must be a "
                            "subclass of ClassifierMixin")

        super(BaseWeightBoosting, self).__init__(
            base_estimator=base_estimator,
            n_estimators=n_estimators)

    def fit(self, X, y, sample_weight=None):
        """Build a boosted classifier/regressor from the training set (X, y).

        Parameters
        ----------
        X : array-like of shape = [n_samples, n_features]
            The training input samples.

        y : array-like of shape = [n_samples]
            The target values (integers that correspond to classes in
            classification, real numbers in regression).

        sample_weight : array-like of shape = [n_samples], optional
            Sample weights

        Returns
        -------
        self : object
            Returns self.
        """
        # Check parameters
        if self.learn_rate <= 0:
            raise ValueError("learn_rate must be greater than 0")

        if self.compute_importances:
            self.base_estimator.set_params(compute_importances=True)

        # Format data
        X, y = check_arrays(X, y, sparse_format='dense')
        X = np.asfortranarray(X, dtype=DTYPE)

        if sample_weight is None:
            # initialize weights to 1/N
            sample_weight = np.ones(X.shape[0], dtype=np.float64) / X.shape[0]
        else:
            sample_weight = np.copy(sample_weight)
            # normalize
            sample_weight /= sample_weight.sum()

        # Clear any previous fit results
        self.estimators_ = []<commit_msg>DOC: edited some docstrings<commit_after>"""Weight Boosting

This module contains weight boosting estimators for both classification and
regression.

The module structure is the following:

- The ``BaseAdaBoost`` base class implements a common ``fit`` method
  for all the estimators in the module. Regression and classification
  only differ from each other in the loss function that is optimized.

- ``AdaBoostClassifier`` implements adaptive boosting (AdaBoost-SAMME) for
  classification problems.

- ``AdaBoostRegressor`` implements adaptive boosting (AdaBoost.R2) for
  regression problems.
"""

# Authors: <NAME>, <NAME>
# License: BSD Style

import numpy as np

from .base import BaseEnsemble
from ..base import ClassifierMixin, RegressorMixin, \
                   WeightedClassifierMixin, WeightedRegressorMixin
from ..metrics import weighted_r2_score
from ..tree import DecisionTreeClassifier, DecisionTreeRegressor
from ..tree._tree import DTYPE
from ..utils import array2d, check_arrays


__all__ = [
    'AdaBoostClassifier',
    'AdaBoostRegressor',
]


class BaseWeightBoosting(BaseEnsemble):
    """Abstract base class for weight boosting.

    Parameters
    ----------
    base_estimator : object, optional (default=DecisionTreeClassifier)
        The base estimator from which the ensemble is built.

    n_estimators : integer, optional (default=10)
        The maximum number of estimators at which boosting is terminated.

    learn_rate : float, optional (default=0.5)
        learning rate shrinks the contribution of each classifier by
        `learn_rate`. There is a trade-off between learn_rate and n_estimators.

    compute_importances : boolean, optional (default=False)
        Whether feature importances are computed and stored into the
        ``feature_importances_`` attribute when calling fit.

    Attributes
    ----------
    `feature_importances_` : array of shape = [n_features]
        The feature importances (the higher, the more important the feature).
        The importance I(f) of a feature f is computed as the (normalized)
        total reduction of error brought by that feature. It is also known as
        the Gini importance [1]_.

    References
    ----------

    .. [1] <NAME>, and <NAME>, "Random Forests",
           http://www.stat.berkeley.edu/~breiman/RandomForests/cc_home.htm
    """
    def __init__(self, base_estimator=None,
                 n_estimators=10,
                 learn_rate=.5,
                 compute_importances=False):
        self.boost_weights_ = []
        self.errs_ = []
        self.learn_rate = learn_rate
        self.compute_importances = compute_importances
        self.feature_importances_ = None

        if base_estimator is None:
            if isinstance(self, ClassifierMixin):
                base_estimator = DecisionTreeClassifier(max_depth=3)
            else:
                base_estimator = DecisionTreeRegressor(max_depth=3)
        elif (isinstance(self, ClassifierMixin)
              and not isinstance(base_estimator, ClassifierMixin)):
            raise TypeError("base_estimator must be a "
                            "subclass of ClassifierMixin")
        elif (isinstance(self, RegressorMixin)
              and not isinstance(base_estimator, RegressorMixin)):
            raise TypeError("base_estimator must be a "
                            "subclass of ClassifierMixin")

        super(BaseWeightBoosting, self).__init__(
            base_estimator=base_estimator,
            n_estimators=n_estimators)

    def fit(self, X, y, sample_weight=None):
        """Build a boosted classifier/regressor from the training set (X, y).

        Parameters
        ----------
        X : array-like of shape = [n_samples, n_features]
            The training input samples.

        y : array-like of shape = [n_samples]
            The target values (integers that correspond to classes in
            classification, real numbers in regression).

        sample_weight : array-like of shape = [n_samples], optional
            Sample weights.

        Returns
        -------
        self : object
            Returns self.
        """
        # Check parameters
        if self.learn_rate <= 0:
            raise ValueError("learn_rate must be greater than 0")

        if self.compute_importances:
            self.base_estimator.set_params(compute_importances=True)

        # Format data
        X, y = check_arrays(X, y, sparse_format='dense')
        X = np.asfortranarray(X, dtype=DTYPE)

        if sample_weight is None:
            # initialize weights to 1/N
            sample_weight = np.ones(X.shape[0], dtype=np.float64) / X.shape[0]
        else:
            sample_weight = np.copy(sample_weight)
            # normalize
            sample_weight /= sample_weight.sum()

        # Clear any previous fit results
        self.estimators_ = []
{'fraction_non_alphanumeric': 0.08127572016460906, 'fraction_numerical': 0.004320987654320987, 'mean_word_length': 2.431344864101659, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7570966', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2549, 'n_words': 806}