Naive Bayes

BernoulliNaiveBayes

class DLL.MachineLearning.SupervisedLearning.NaiveBayes.BernoulliNaiveBayes[source]

Bases: object

The BernoulliNaiveBayes classifier model. Applies the Bayes theorem to classify samples with binary features.

n_features

The number of features. Available after fitting.

Type:

int

n_classes

The number of classes. Available after fitting.

Type:

int

fit(X, y, alpha=1)[source]

Fits the BernoulliNaiveBayes model to the input data by calculating the prior probabilities and likelihoods.

Parameters:
  • X (torch.Tensor of shape (n_samples, n_features)) – The input data, where each row is a sample and each column is a feature. Must contain only binary values.

  • y (torch.Tensor of shape (n_samples,)) – The labels corresponding to each sample. Every element must be in [0, …, n_classes - 1].

  • alpha (float | int) – Laplacian smoothing parameter. Must be non-negative. For no smoothing, alpha is set to zero. Defaults to 1.

Returns:

None

Raises:
  • TypeError – If the input matrix or the label matrix is not a PyTorch tensor.

  • ValueError – If the input matrix or the label matrix is not the correct shape.

predict(X)[source]

Applies the fitted BernoulliNaiveBayes model to the input data, predicting the labels.

Parameters:

X (torch.Tensor of shape (n_samples, n_features)) – The input data to be classified.

Returns:

The predicted target values corresponding to each sample.

Return type:

labels (torch.Tensor of shape (n_samples,))

Raises:
  • NotFittedError – If the BernoulliNaiveBayes model has not been fitted before predicting.

  • TypeError – If the input matrix is not a PyTorch tensor.

  • ValueError – If the input matrix is not the correct shape.

predict_proba(X)[source]

Applies the fitted BernoulliNaiveBayes model to the input data, predicting the labels.

Parameters:

X (torch.Tensor of shape (n_samples, n_features)) – The input data to be classified.

Returns:

The predicted target values corresponding to each sample.

Return type:

labels (torch.Tensor of shape (n_samples,))

Raises:
  • NotFittedError – If the BernoulliNaiveBayes model has not been fitted before predicting.

  • TypeError – If the input matrix is not a PyTorch tensor.

  • ValueError – If the input matrix is not the correct shape.

GaussianNaiveBayes module

class DLL.MachineLearning.SupervisedLearning.NaiveBayes.GaussianNaiveBayes[source]

Bases: object

The GaussianNaiveBayes classifier model. Applies the Bayes theorem to classify samples with real number features.

n_features

The number of features. Available after fitting.

Type:

int

n_classes

The number of classes. Available after fitting.

Type:

int

fit(X, y)[source]

Fits the GaussianNaiveBayes model to the input data by calculating the prior probabilities and likelihoods.

Parameters:
  • X (torch.Tensor of shape (n_samples, n_features)) – The input data, where each row is a sample and each column is a feature.

  • y (torch.Tensor of shape (n_samples,)) – The labels corresponding to each sample. Every element must be in [0, …, n_classes - 1].

Returns:

None

Raises:
  • TypeError – If the input matrix or the label matrix is not a PyTorch tensor.

  • ValueError – If the input matrix or the label matrix is not the correct shape.

predict(X)[source]

Applies the fitted GaussianNaiveBayes model to the input data, predicting the labels.

Parameters:

X (torch.Tensor of shape (n_samples, n_features)) – The input data to be classified.

Returns:

The predicted target values corresponding to each sample.

Return type:

labels (torch.Tensor of shape (n_samples,))

Raises:
  • NotFittedError – If the GaussianNaiveBayes model has not been fitted before predicting.

  • TypeError – If the input matrix is not a PyTorch tensor.

  • ValueError – If the input matrix is not the correct shape.

predict_proba(X)[source]

Applies the fitted GaussianNaiveBayes model to the input data, predicting the probabilities.

Parameters:

X (torch.Tensor of shape (n_samples, n_features)) – The input data to be classified.

Returns:

The predicted target values corresponding to each sample.

Return type:

labels (torch.Tensor of shape (n_samples,))

Raises:
  • NotFittedError – If the GaussianNaiveBayes model has not been fitted before predicting.

  • TypeError – If the input matrix is not a PyTorch tensor.

  • ValueError – If the input matrix is not the correct shape.

MultinomialNaiveBayes module

class DLL.MachineLearning.SupervisedLearning.NaiveBayes.MultinomialNaiveBayes[source]

Bases: object

The MultinomialNaiveBayes classifier model. Applies the Bayes theorem to classify samples with positive integer features.

n_features

The number of features. Available after fitting.

Type:

int

n_classes

The number of classes. Available after fitting.

Type:

int

fit(X, y, alpha=1)[source]

Fits the MultinomialNaiveBayes model to the input data by calculating the prior probabilities and likelihoods.

Parameters:
  • X (torch.Tensor of shape (n_samples, n_features)) – The input data, where each row is a sample and each column is a feature. Must contain only non-negative integers.

  • y (torch.Tensor of shape (n_samples,)) – The labels corresponding to each sample. Every element must be in [0, …, n_classes - 1].

  • alpha (float | int) – Laplacian smoothing parameter. Must be non-negative. For no smoothing, alpha is set to zero. Defaults to 1.

Returns:

None

Raises:
  • TypeError – If the input matrix or the label matrix is not a PyTorch tensor.

  • ValueError – If the input matrix or the label matrix is not the correct shape.

predict(X)[source]

Applies the fitted MultinomialNaiveBayes model to the input data, predicting the labels.

Parameters:

X (torch.Tensor of shape (n_samples, n_features)) – The input data to be classified.

Returns:

The predicted target values corresponding to each sample.

Return type:

labels (torch.Tensor of shape (n_samples,))

Raises:
  • NotFittedError – If the MultinomialNaiveBayes model has not been fitted before predicting.

  • TypeError – If the input matrix is not a PyTorch tensor.

  • ValueError – If the input matrix is not the correct shape.

predict_proba(X)[source]

Applies the fitted MultinomialNaiveBayes model to the input data, predicting the labels.

Parameters:

X (torch.Tensor of shape (n_samples, n_features)) – The input data to be classified.

Returns:

The predicted target values corresponding to each sample.

Return type:

labels (torch.Tensor of shape (n_samples,))

Raises:
  • NotFittedError – If the MultinomialNaiveBayes model has not been fitted before predicting.

  • TypeError – If the input matrix is not a PyTorch tensor.

  • ValueError – If the input matrix is not the correct shape.