(self, X, y, sample_weight=None, minority_target=None)
return self
Build a boosted classifier/regressor from the training set (X, y),
        performing random undersampling during each boosting step.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. COO, DOK, and LIL are converted to CSR. The dtype is
            forced to DTYPE from tree._tree if the base classifier of this
            ensemble weighted boosting classifier is a tree or forest.
        y : array-like of shape = [n_samples]
            The target values (class labels in classification, real numbers in
            regression).
        sample_weight : array-like of shape = [n_samples], optional
            Sample weights. If None, the sample weights are initialized to
            1 / n_samples.
        minority_target : int
            Minority class label.

        Returns
        -------
        self : object
            Returns self.

        Notes
        -----
        Based on the scikit-learn v0.18 AdaBoostClassifier and
        BaseWeightBoosting `fit` methods.
Build a boosted classifier/regressor from the training set (X, y),
        performing random undersampling during each boosting step.
Build a boosted classifier / regressor from the training set ( X y ) performing random undersampling during each boosting step .
def fit(self, X, y, sample_weight=None, minority_target=None):
        """Build a boosted classifier/regressor from the training set (X, y),
        performing random undersampling during each boosting step.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape = [n_samples, n_features]
            The training input samples. Sparse matrix can be CSC, CSR, COO,
            DOK, or LIL. COO, DOK, and LIL are converted to CSR. The dtype is
            forced to DTYPE from tree._tree if the base classifier of this
            ensemble weighted boosting classifier is a tree or forest.
        y : array-like of shape = [n_samples]
            The target values (class labels in classification, real numbers in
            regression).
        sample_weight : array-like of shape = [n_samples], optional
            Sample weights. If None, the sample weights are initialized to
            1 / n_samples.
        minority_target : int
            Minority class label.

        Returns
        -------
        self : object
            Returns self.

        Notes
        -----
        Based on the scikit-learn v0.18 AdaBoostClassifier and
        BaseWeightBoosting `fit` methods.
        """
        # Check that algorithm is supported.
        if self.algorithm not in ('SAMME', 'SAMME.R'):
            raise ValueError("algorithm %s is not supported" % self.algorithm)

        # Check parameters.
        if self.learning_rate <= 0:
            raise ValueError("learning_rate must be greater than zero")

        if (self.base_estimator is None or
                isinstance(self.base_estimator, (BaseDecisionTree,
                                                 BaseForest))):
            DTYPE = np.float64  # from fast_dict.pxd
            dtype = DTYPE
            accept_sparse = 'csc'
        else:
            dtype = None
            accept_sparse = ['csr', 'csc']

        X, y = check_X_y(X, y, accept_sparse=accept_sparse, dtype=dtype,
                         y_numeric=is_regressor(self))

        if sample_weight is None:
            # Initialize weights to 1 / n_samples.
            sample_weight = np.empty(X.shape[0], dtype=np.float64)
            sample_weight[:] = 1. / X.shape[0]
        else:
            sample_weight = check_array(sample_weight, ensure_2d=False)
            # Normalize existing weights.
            sample_weight = sample_weight / sample_weight.sum(dtype=np.float64)

            # Check that the sample weights sum is positive.
            if sample_weight.sum() <= 0:
                raise ValueError(
                    "Attempting to fit with a non-positive "
                    "weighted number of samples.")

        if minority_target is None:
            # Determine the minority class label.
            stats_c_ = Counter(y)
            maj_c_ = max(stats_c_, key=stats_c_.get)
            min_c_ = min(stats_c_, key=stats_c_.get)
            self.minority_target = min_c_
        else:
            self.minority_target = minority_target

        # Check parameters.
        self._validate_estimator()

        # Clear any previous fit results.
        self.estimators_ = []
        self.estimator_weights_ = np.zeros(self.n_estimators, dtype=np.float64)
        self.estimator_errors_ = np.ones(self.n_estimators, dtype=np.float64)

        random_state = check_random_state(self.random_state)

        for iboost in range(self.n_estimators):
            # RAMO step.
            self.ramo.fit(X, y, sample_weight=sample_weight)
            X_syn = self.ramo.sample(self.n_samples)
            y_syn = np.full(X_syn.shape[0], fill_value=self.minority_target,
                            dtype=np.int64)

            # Combine the minority and majority class samples.
            X = np.vstack((X, X_syn))
            y = np.append(y, y_syn)

            # Normalize synthetic sample weights based on current training set.
            sample_weight_syn = np.empty(X_syn.shape[0], dtype=np.float64)
            sample_weight_syn[:] = 1. / X.shape[0]

            # Combine the weights.
            sample_weight = \
                np.append(sample_weight, sample_weight_syn).reshape(-1, 1)
            sample_weight = \
                np.squeeze(normalize(sample_weight, axis=0, norm='l1'))

            # X, y, sample_weight = shuffle(X, y, sample_weight,
            #                              random_state=random_state)

            # Boosting step.
            sample_weight, estimator_weight, estimator_error = self._boost(
                iboost,
                X, y,
                sample_weight,
                random_state)

            # Early termination.
            if sample_weight is None:
                break

            self.estimator_weights_[iboost] = estimator_weight
            self.estimator_errors_[iboost] = estimator_error

            # Stop if error is zero.
            if estimator_error == 0:
                break

            sample_weight_sum = np.sum(sample_weight)

            # Stop if the sum of sample weights has become non-positive.
            if sample_weight_sum <= 0:
                break

            if iboost < self.n_estimators - 1:
                # Normalize.
                sample_weight /= sample_weight_sum

        return self
def fit ( self , X , y , sample_weight = None , minority_target = None ) : # Check that algorithm is supported. if self . algorithm not in ( 'SAMME' , 'SAMME.R' ) : raise ValueError ( "algorithm %s is not supported" % self . algorithm ) # Check parameters. if self . learning_rate <= 0 : raise ValueError ( "learning_rate must be greater than zero" ) if ( self . base_estimator is None or isinstance ( self . base_estimator , ( BaseDecisionTree , BaseForest ) ) ) : DTYPE = np . float64 # from fast_dict.pxd dtype = DTYPE accept_sparse = 'csc' else : dtype = None accept_sparse = [ 'csr' , 'csc' ] X , y = check_X_y ( X , y , accept_sparse = accept_sparse , dtype = dtype , y_numeric = is_regressor ( self ) ) if sample_weight is None : # Initialize weights to 1 / n_samples. sample_weight = np . empty ( X . shape [ 0 ] , dtype = np . float64 ) sample_weight [ : ] = 1. / X . shape [ 0 ] else : sample_weight = check_array ( sample_weight , ensure_2d = False ) # Normalize existing weights. sample_weight = sample_weight / sample_weight . sum ( dtype = np . float64 ) # Check that the sample weights sum is positive. if sample_weight . sum ( ) <= 0 : raise ValueError ( "Attempting to fit with a non-positive " "weighted number of samples." ) if minority_target is None : # Determine the minority class label. stats_c_ = Counter ( y ) maj_c_ = max ( stats_c_ , key = stats_c_ . get ) min_c_ = min ( stats_c_ , key = stats_c_ . get ) self . minority_target = min_c_ else : self . minority_target = minority_target # Check parameters. self . _validate_estimator ( ) # Clear any previous fit results. self . estimators_ = [ ] self . estimator_weights_ = np . zeros ( self . n_estimators , dtype = np . float64 ) self . estimator_errors_ = np . ones ( self . n_estimators , dtype = np . float64 ) random_state = check_random_state ( self . random_state ) for iboost in range ( self . n_estimators ) : # RAMO step. self . ramo . fit ( X , y , sample_weight = sample_weight ) X_syn = self . ramo . sample ( self . n_samples ) y_syn = np . full ( X_syn . shape [ 0 ] , fill_value = self . minority_target , dtype = np . int64 ) # Combine the minority and majority class samples. X = np . vstack ( ( X , X_syn ) ) y = np . append ( y , y_syn ) # Normalize synthetic sample weights based on current training set. sample_weight_syn = np . empty ( X_syn . shape [ 0 ] , dtype = np . float64 ) sample_weight_syn [ : ] = 1. / X . shape [ 0 ] # Combine the weights. sample_weight = np . append ( sample_weight , sample_weight_syn ) . reshape ( - 1 , 1 ) sample_weight = np . squeeze ( normalize ( sample_weight , axis = 0 , norm = 'l1' ) ) # X, y, sample_weight = shuffle(X, y, sample_weight, #                              random_state=random_state) # Boosting step. sample_weight , estimator_weight , estimator_error = self . _boost ( iboost , X , y , sample_weight , random_state ) # Early termination. if sample_weight is None : break self . estimator_weights_ [ iboost ] = estimator_weight self . estimator_errors_ [ iboost ] = estimator_error # Stop if error is zero. if estimator_error == 0 : break sample_weight_sum = np . sum ( sample_weight ) # Stop if the sum of sample weights has become non-positive. if sample_weight_sum <= 0 : break if iboost < self . n_estimators - 1 : # Normalize. sample_weight /= sample_weight_sum return self