(self, X, y, sample_weight=None, minority_target=None)
return self
Train model based on input data.

        Parameters
        ----------
        X : array-like, shape = [n_total_samples, n_features]
            Holds the majority and minority samples.
        y : array-like, shape = [n_total_samples]
            Holds the class targets for samples.
        sample_weight : array-like of shape = [n_samples], optional
            Sample weights multiplier. If None, the multiplier is 1.
        minority_target : int, optional (default=None)
            Minority class label.
Train model based on input data.
Train model based on input data .
def fit(self, X, y, sample_weight=None, minority_target=None):
        """Train model based on input data.

        Parameters
        ----------
        X : array-like, shape = [n_total_samples, n_features]
            Holds the majority and minority samples.
        y : array-like, shape = [n_total_samples]
            Holds the class targets for samples.
        sample_weight : array-like of shape = [n_samples], optional
            Sample weights multiplier. If None, the multiplier is 1.
        minority_target : int, optional (default=None)
            Minority class label.
        """
        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

        self.X_min = X[y == self.minority_target]
        self.n_minority_samples, self.n_features = self.X_min.shape

        neigh_1 = NearestNeighbors(n_neighbors=self.k_neighbors_1 + 1)
        neigh_1.fit(X)
        nn = neigh_1.kneighbors(self.X_min, return_distance=False)[:, 1:]

        if sample_weight is None:
            sample_weight_min = np.ones(shape=(len(self.minority_target)))
        else:
            assert(len(y) == len(sample_weight))
            sample_weight_min = sample_weight[y == self.minority_target]

        self.r = np.zeros(shape=(self.n_minority_samples))
        for i in range(self.n_minority_samples):
            majority_neighbors = 0
            for n in nn[i]:
                if y[n] != self.minority_target:
                    majority_neighbors += 1

            self.r[i] = 1. / (1 + np.exp(-self.alpha * majority_neighbors))

        self.r = (self.r * sample_weight_min).reshape(1, -1)
        self.r = np.squeeze(normalize(self.r, axis=1, norm='l1'))

        # Learn nearest neighbors.
        self.neigh_2 = NearestNeighbors(n_neighbors=self.k_neighbors_2 + 1)
        self.neigh_2.fit(self.X_min)

        return self
def fit ( self , X , y , sample_weight = None , minority_target = None ) : 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 self . X_min = X [ y == self . minority_target ] self . n_minority_samples , self . n_features = self . X_min . shape neigh_1 = NearestNeighbors ( n_neighbors = self . k_neighbors_1 + 1 ) neigh_1 . fit ( X ) nn = neigh_1 . kneighbors ( self . X_min , return_distance = False ) [ : , 1 : ] if sample_weight is None : sample_weight_min = np . ones ( shape = ( len ( self . minority_target ) ) ) else : assert ( len ( y ) == len ( sample_weight ) ) sample_weight_min = sample_weight [ y == self . minority_target ] self . r = np . zeros ( shape = ( self . n_minority_samples ) ) for i in range ( self . n_minority_samples ) : majority_neighbors = 0 for n in nn [ i ] : if y [ n ] != self . minority_target : majority_neighbors += 1 self . r [ i ] = 1. / ( 1 + np . exp ( - self . alpha * majority_neighbors ) ) self . r = ( self . r * sample_weight_min ) . reshape ( 1 , - 1 ) self . r = np . squeeze ( normalize ( self . r , axis = 1 , norm = 'l1' ) ) # Learn nearest neighbors. self . neigh_2 = NearestNeighbors ( n_neighbors = self . k_neighbors_2 + 1 ) self . neigh_2 . fit ( self . X_min ) return self