Nasty.Operations.Classification.NaiveBayes (Nasty v0.3.0)

View Source

Generic Naive Bayes classifier for text classification.

Implements:

  • Multinomial Naive Bayes algorithm
  • Laplace (add-one) smoothing for unseen features
  • Log probabilities to avoid numerical underflow
  • Softmax for probability normalization

Summary

Functions

Evaluates a model on test data.

Predicts class probabilities for a feature vector.

Trains a Naive Bayes model from labeled feature vectors.

Functions

evaluate(model, test_data, language)

@spec evaluate(Nasty.AST.ClassificationModel.t(), [{map(), atom()}], atom()) :: map()

Evaluates a model on test data.

Returns accuracy and per-class precision, recall, and F1 metrics.

predict(model, feature_vector, language)

Predicts class probabilities for a feature vector.

Uses log probabilities and softmax for numerical stability.

Returns

List of Classification structs sorted by confidence (highest first)

train(labeled_features, opts \\ [])

@spec train(
  [{map(), atom()}],
  keyword()
) :: Nasty.AST.ClassificationModel.t()

Trains a Naive Bayes model from labeled feature vectors.

Arguments

  • labeled_features - List of {feature_vector, class} tuples
  • opts - Training options

Options

  • :smoothing - Laplace smoothing parameter alpha (default: 1.0)
  • :feature_types - List of feature types used (for metadata)

Returns

ClassificationModel struct with learned parameters