(self, X, y)
return self
Compute the necessary statistics from the training set.

        Parameters
        ----------
        X : ndarray of shape (n_samples, n_features), dtype=np.int32
            The input data. The data are expected to be encoded with a
            :class:`~sklearn.preprocessing.OrdinalEncoder`.

        y : ndarray of shape (n_features,)
            The target.

        Returns
        -------
        self : object
            Return the instance itself.
Compute the necessary statistics from the training set.
Compute the necessary statistics from the training set .
def fit(self, X, y):
        """Compute the necessary statistics from the training set.

        Parameters
        ----------
        X : ndarray of shape (n_samples, n_features), dtype=np.int32
            The input data. The data are expected to be encoded with a
            :class:`~sklearn.preprocessing.OrdinalEncoder`.

        y : ndarray of shape (n_features,)
            The target.

        Returns
        -------
        self : object
            Return the instance itself.
        """
        check_consistent_length(X, y)
        X, y = self._validate_data(X, y, reset=True, dtype=np.int32)

        if isinstance(self.n_categories, str) and self.n_categories == "auto":
            # categories are expected to be encoded from 0 to n_categories - 1
            self.n_categories_ = X.max(axis=0) + 1
        else:
            if len(self.n_categories) != self.n_features_in_:
                raise ValueError(
                    f"The length of n_categories is not consistent with the "
                    f"number of feature in X. Got {len(self.n_categories)} "
                    f"elements in n_categories and {self.n_features_in_} in "
                    f"X."
                )
            self.n_categories_ = np.array(self.n_categories, copy=False)
        classes = unique_labels(y)

        # list of length n_features of ndarray (n_categories, n_classes)
        # compute the counts
        self.proba_per_class_ = [
            np.empty(shape=(n_cat, len(classes)), dtype=np.float64)
            for n_cat in self.n_categories_
        ]
        for feature_idx in range(self.n_features_in_):
            for klass_idx, klass in enumerate(classes):
                self.proba_per_class_[feature_idx][:, klass_idx] = np.bincount(
                    X[y == klass, feature_idx],
                    minlength=self.n_categories_[feature_idx],
                )

        # normalize by the summing over the classes
        with np.errstate(invalid="ignore"):
            # silence potential warning due to in-place division by zero
            for feature_idx in range(self.n_features_in_):
                self.proba_per_class_[feature_idx] /= (
                    self.proba_per_class_[feature_idx].sum(axis=1).reshape(-1, 1)
                )
                np.nan_to_num(self.proba_per_class_[feature_idx], copy=False)

        return self
def fit ( self , X , y ) : check_consistent_length ( X , y ) X , y = self . _validate_data ( X , y , reset = True , dtype = np . int32 ) if isinstance ( self . n_categories , str ) and self . n_categories == "auto" : # categories are expected to be encoded from 0 to n_categories - 1 self . n_categories_ = X . max ( axis = 0 ) + 1 else : if len ( self . n_categories ) != self . n_features_in_ : raise ValueError ( f"The length of n_categories is not consistent with the " f"number of feature in X. Got {len(self.n_categories)} " f"elements in n_categories and {self.n_features_in_} in " f"X." ) self . n_categories_ = np . array ( self . n_categories , copy = False ) classes = unique_labels ( y ) # list of length n_features of ndarray (n_categories, n_classes) # compute the counts self . proba_per_class_ = [ np . empty ( shape = ( n_cat , len ( classes ) ) , dtype = np . float64 ) for n_cat in self . n_categories_ ] for feature_idx in range ( self . n_features_in_ ) : for klass_idx , klass in enumerate ( classes ) : self . proba_per_class_ [ feature_idx ] [ : , klass_idx ] = np . bincount ( X [ y == klass , feature_idx ] , minlength = self . n_categories_ [ feature_idx ] , ) # normalize by the summing over the classes with np . errstate ( invalid = "ignore" ) : # silence potential warning due to in-place division by zero for feature_idx in range ( self . n_features_in_ ) : self . proba_per_class_ [ feature_idx ] /= ( self . proba_per_class_ [ feature_idx ] . sum ( axis = 1 ) . reshape ( - 1 , 1 ) ) np . nan_to_num ( self . proba_per_class_ [ feature_idx ] , copy = False ) return self