BernoulliNB
Module: strata.naive_bayes • Kind: struct • Traits: Classifier, Copyable, Movable, Serializable
Source: strata/naive_bayes/bernoulli.mojo
struct BernoulliNB[compute_dtype: DType = DType.float64](Classifier, Copyable, Movable, Serializable)from strata.naive_bayes import BernoulliNBBernoulli Naive Bayes classifier for multivariate Bernoulli models.
Suitable for discrete data where features are independent binary variables
(e.g., word occurrence in text classification). Real-valued data can be
binarized using the binarize threshold.
Parameters (Compile-Time)
Section titled “Parameters (Compile-Time)”| Parameter | Description |
|---|---|
compute_dtype |
Computational precision data type. Default DType.float64. |
Constructors
Section titled “Constructors”def __init__( out self, alpha: Scalar[Self.compute_dtype] = 1.0, binarize: Scalar[Self.compute_dtype] = 0.0, use_binarize: Bool = True, fit_prior: Bool = True, class_prior: List[Scalar[Self.compute_dtype]] = List[ Scalar[Self.compute_dtype] ](),)Initialize the BernoulliNB classifier.
| Argument | Type | Description |
|---|---|---|
alpha |
Scalar[Self.compute_dtype] |
Additive smoothing parameter (>= 0). Default 1.0. |
binarize |
Scalar[Self.compute_dtype] |
Threshold for binarizing features (features > threshold = 1). Default 0.0. |
use_binarize |
Bool |
Whether to apply binarization threshold. Default True. |
fit_prior |
Bool |
Whether to learn class prior probabilities. Default True. |
class_prior |
List[Scalar[Self.compute_dtype]] |
Prior probabilities of the classes. Default empty. |
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
classes_ |
Unique class labels observed during fit. |
class_count_ |
Number of training samples observed in each class. |
class_log_prior_ |
Smoothed empirical log-probability of each class. |
feature_count_ |
Number of samples encountered for each (class, feature) pair where feature value was 1, of shape . |
feature_log_prob_ |
Empirical log probability of features given a class, , of shape . |
neg_feature_log_prob_ |
Empirical log probability of absent features given a class, , of shape . |
n_features_in_ |
Number of features seen during fit. |
is_fitted |
Boolean flag indicating if estimator has been fitted. |
Methods Overview
Section titled “Methods Overview”| Method | Description |
|---|---|
BernoulliNB.copy() |
Returns a deep copy of the BernoulliNB instance. |
BernoulliNB.fit() |
Fit Bernoulli Naive Bayes using dense feature matrix X and target labels y. |
BernoulliNB.predict_log_proba() |
Predict class log probabilities for dense matrix X. |
BernoulliNB.predict_proba() |
Predict class probabilities for dense matrix X. |
BernoulliNB.predict() |
Predict class labels for samples in dense Matrix X. |
BernoulliNB.serialize() |
Serializes BernoulliNB parameters and fitted state into BufferWriter. |
BernoulliNB.deserialize() |
Deserializes BernoulliNB from BufferReader. |
Method Details
Section titled “Method Details”BernoulliNB.copy()
Section titled “BernoulliNB.copy()”def copy(self) -> SelfReturns a deep copy of the BernoulliNB instance.
Returns: Self
BernoulliNB.fit()
Section titled “BernoulliNB.fit()”def fit[feat_dtype: DType, target_dtype: DType]( mut self, X: Matrix[feat_dtype], y: List[Scalar[target_dtype]],)
def fit[feat_dtype: DType, target_dtype: DType]( mut self, X: CSRMatrix[feat_dtype], y: List[Scalar[target_dtype]],)
def fit[feat_dtype: DType, target_dtype: DType]( mut self, dataset: Dataset[feat_dtype, target_dtype],)Overload Note: This method accepts either standard
(X, y)inputs or a unifieddataset: Datasetcontainer.
Fit Bernoulli Naive Bayes using dense feature matrix X and target labels y.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Training feature matrix of shape . |
y |
List[Scalar[target_dtype]] |
Target class labels of length . |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y)) |
BernoulliNB.predict_log_proba()
Section titled “BernoulliNB.predict_log_proba()”def predict_log_proba[feat_dtype: DType]( self, X: Matrix[feat_dtype],) -> Matrix[feat_dtype]
def predict_log_proba[feat_dtype: DType]( self, X: CSRMatrix[feat_dtype],) -> Matrix[feat_dtype]Overload Note: This method accepts either standard
(X, y)inputs or a unifieddataset: Datasetcontainer.
Predict class log probabilities for dense matrix X.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Feature matrix. |
Returns: Matrix[feat_dtype]
BernoulliNB.predict_proba()
Section titled “BernoulliNB.predict_proba()”def predict_proba[feat_dtype: DType]( self, X: Matrix[feat_dtype],) -> Matrix[feat_dtype]
def predict_proba[feat_dtype: DType]( self, X: CSRMatrix[feat_dtype],) -> Matrix[feat_dtype]
def predict_proba[feat_dtype: DType, target_dtype: DType]( self, dataset: Dataset[feat_dtype, target_dtype],) -> Matrix[ feat_dtype ]Overload Note: This method accepts either standard
(X, y)inputs or a unifieddataset: Datasetcontainer.
Predict class probabilities for dense matrix X.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Feature matrix. |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y)) |
Returns: Matrix[feat_dtype]
BernoulliNB.predict()
Section titled “BernoulliNB.predict()”def predict[feat_dtype: DType](self, X: Matrix[feat_dtype]) -> List[Int]
def predict[feat_dtype: DType](self, X: CSRMatrix[feat_dtype]) -> List[Int]
def predict[feat_dtype: DType, target_dtype: DType]( self, dataset: Dataset[feat_dtype, target_dtype],) -> List[Int]Overload Note: This method accepts either standard
(X, y)inputs or a unifieddataset: Datasetcontainer.
Predict class labels for samples in dense Matrix X.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Feature matrix. |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y)) |
Returns: List[Int]
BernoulliNB.serialize()
Section titled “BernoulliNB.serialize()”def serialize(self, mut writer: BufferWriter)Serializes BernoulliNB parameters and fitted state into BufferWriter.
BernoulliNB.deserialize()
Section titled “BernoulliNB.deserialize()”def deserialize(mut reader: BufferReader) -> SelfDeserializes BernoulliNB from BufferReader.
Returns: Self
Section titled “Returns: Self”Example
Section titled “Example”from strata.naive_bayes import BernoulliNBfrom strata.core import Matrix
var bnb = BernoulliNB[DType.float64](alpha=1.0, binarize=0.0)bnb.fit(X_train, y_train)var preds = bnb.predict(X_test)var probs = bnb.predict_proba(X_test)