@kanaries/ml

CategoricalNB

API and practical guide for CategoricalNB in @kanaries/ml, including when to use it in JavaScript and TypeScript ML workflows.

Bayes.CategoricalNB

Naive Bayes classifier for features with a finite number of discrete categories. It counts how often each feature value appears in each class and uses additive smoothing to estimate the conditional probabilities.

interface CategoricalNBProps {
    alpha?: number;
    forceAlpha?: boolean;
    fitPrior?: boolean;
    classPrior?: number[] | null;
    minCategories?: number | number[] | null;
}
constructor(props: CategoricalNBProps = {})

Parameters

  • alpha — Smoothing parameter used when computing category probabilities. Larger values make the model less sensitive to missing observations.
  • forceAlpha — If true, ensures that alpha is strictly positive even when a small value is provided.
  • fitPrior — Whether to learn class prior probabilities from data. When false, class priors are assumed to be uniform.
  • classPrior — Optional array of prior probabilities for each class. Overrides the data-derived priors when provided.
  • minCategories — Minimum number of categories assumed for each feature. Can be a single number or an array specifying the value per feature.

Example

const clf = new CategoricalNB();
clf.fit(trainX, trainY);
const result = clf.predict(testX);

Practical guide: CategoricalNB in JavaScript and TypeScript

CategoricalNB is designed for discrete categorical features encoded as integer category IDs.

When to use CategoricalNB

  • Inputs are naturally categorical and not meaningful on a numeric distance scale.
  • You need fast classification with probabilistic interpretation.
  • You want a robust baseline for tabular categoricals in pure JavaScript.

Implementation workflow

  1. Encode every categorical column to stable integer category indices.
  2. Fit on labeled rows and validate per-class calibration quality.
  3. Monitor unseen-category behavior and keep category mapping versioned.

JavaScript deployment notes

  • Prefer feature scaling for distance-based and gradient-based algorithms to improve stability.
  • In browser apps, run heavy training in Web Workers to keep UI interactions smooth.
  • Keep a simple baseline from the same module as a fallback model for comparison.

Search intents this page targets

  • CategoricalNB JavaScript
  • CategoricalNB TypeScript
  • CategoricalNB browser machine learning
  • @kanaries/ml CategoricalNB

FAQ

What problem does CategoricalNB solve in JavaScript machine learning projects?

CategoricalNB helps teams implement production-ready ML workflows in browser and Node.js environments with a familiar scikit-learn-style API.

When should I choose CategoricalNB instead of other Bayes algorithms?

Use CategoricalNB when it best matches your data shape, labeling strategy, and runtime constraints. Benchmark against at least one alternative in the same module before finalizing defaults.

Can I run CategoricalNB in both browser and Node.js with @kanaries/ml?

Yes. @kanaries/ml is designed for JavaScript and TypeScript runtimes across browser applications, server-side Node.js services, and edge-friendly workflows.