GaussianNB
Module: strata.naive_bayes • Kind: struct • Traits: Classifier, Copyable, Movable, Serializable
Source: strata/naive_bayes/gaussian.mojo
struct GaussianNB[compute_dtype: DType = DType.float64](Classifier, Copyable, Movable, Serializable)from strata.naive_bayes import GaussianNBGaussian Naive Bayes classifier for continuous feature modeling.
Assumes that the likelihood of continuous features within each class is normally distributed:
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, var_smoothing: Scalar[Self.compute_dtype] = 1e-9, priors: List[Scalar[Self.compute_dtype]] = List[ Scalar[Self.compute_dtype] ](),)Initialize the GaussianNB classifier.
| Argument | Type | Description |
|---|---|---|
var_smoothing |
Scalar[Self.compute_dtype] |
Portion of maximum variance added for numerical stability. Must be non-negative. Default 1e-9. |
priors |
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_prior_ |
Probability of each class. |
theta_ |
Mean of each feature per class of shape . |
var_ |
Variance of each feature per class with variance smoothing added, of shape . |
epsilon_ |
Absolute additive variance smoothing factor. |
is_fitted |
Boolean flag indicating if estimator has been fitted. |
Methods Overview
Section titled “Methods Overview”| Method | Description |
|---|---|
GaussianNB.fit() |
Fit Gaussian Naive Bayes according to X, y. |
GaussianNB.predict_log_proba() |
Predict class log probabilities for samples in X. |
GaussianNB.predict_proba() |
Predict class probability distributions for samples in X. |
GaussianNB.predict() |
Predict class labels for samples in X. |
GaussianNB.serialize() |
Serializes GaussianNB state into BufferWriter. |
GaussianNB.deserialize() |
Deserializes GaussianNB from BufferReader. |
Method Details
Section titled “Method Details”GaussianNB.fit()
Section titled “GaussianNB.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, dataset: Dataset[feat_dtype, target_dtype],)Overload Note: This method accepts either standard
(X, y)inputs or a unifieddataset: Datasetcontainer.
Fit Gaussian Naive Bayes according to X, 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)) |
GaussianNB.predict_log_proba()
Section titled “GaussianNB.predict_log_proba()”def predict_log_proba[feat_dtype: DType]( self, X: Matrix[feat_dtype],) -> Matrix[feat_dtype]Predict class log probabilities for samples in X.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Feature matrix of shape . |
Returns: Matrix[feat_dtype] — Matrix[feat_dtype]: Log-probability matrix of shape .
GaussianNB.predict_proba()
Section titled “GaussianNB.predict_proba()”def predict_proba[feat_dtype: DType]( self, X: Matrix[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 probability distributions for samples in X.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Feature matrix of shape . |
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] — Matrix[feat_dtype]: Normalized probability matrix of shape , where each row sums to 1.0.
GaussianNB.predict()
Section titled “GaussianNB.predict()”def predict[feat_dtype: DType](self, X: Matrix[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 X.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Feature matrix of shape . |
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] — List[Int]: Predicted class labels of length .
GaussianNB.serialize()
Section titled “GaussianNB.serialize()”def serialize(self, mut writer: BufferWriter)Serializes GaussianNB state into BufferWriter.
GaussianNB.deserialize()
Section titled “GaussianNB.deserialize()”def deserialize(mut reader: BufferReader) -> SelfDeserializes GaussianNB from BufferReader.
Returns: Self
Section titled “Returns: Self”Example
Section titled “Example”from strata.naive_bayes import GaussianNBfrom strata.core import Matrix
var gnb = GaussianNB[DType.float64](var_smoothing=1e-9)gnb.fit(X_train, y_train)var preds = gnb.predict(X_test)var probs = gnb.predict_proba(X_test)