Docs · Machine learning
nb: Gaussian naive Bayes.
Gaussian naive Bayes, the equivalent of Spark MLlib's NaiveBayes with modelType gaussian and scikit-learn's GaussianNB. One paged pass computes class priors, per-class feature means and smoothed variances.
- Module
- ml_nb
- Header
- clio_ml/naive_bayes.h
- Rows
- features, then class label
What it does
A single pass accumulates per-class counts, sums and sums of squares. Variances are smoothed by var_smoothing * max variance, as scikit-learn does. The result is bit-reproducible across cache sizes and block counts.
Rows
n_features feature columns followed by the class label in 0..num_classes-1. clio_ml.COLUMNS["nb"] is 1.
Python
python
import numpy as np, clio_ml as cm
rng = np.random.default_rng(1)
X = rng.normal(size=(4096, 7)).astype(np.float32)
y = (X[:, 0] + X[:, 1] > 0).astype(np.float32)
data = np.column_stack([X, y]).astype(np.float32)
r = cm.fit_nb(data, num_classes=2)
m = r.model
print(m.prior, np.asarray(m.mean).reshape(m.C, m.nf))
print(cm.predict("nb", r, data[0, :7])) # predicted classfit_nb(X, **options).- Options (
NaiveBayesOptions):num_classes(2),var_smoothing(1e-9),threads(256). - Result
NaiveBayesResult:modelandstats.model(NaiveBayesModel) hasnf,C,prior(C),mean(C*nf) andvar(C*nf, smoothed). predict("nb", r, x)returns the class index.
C++
cpp
struct NaiveBayesOptions {
u32 num_classes = 2;
float var_smoothing = 1e-9f;
u32 threads = 256;
ClusterExchange cluster;
};
struct NaiveBayesModel {
u32 nf = 0, C = 0;
std::vector<float> prior; // C
std::vector<float> mean; // C*nf
std::vector<float> var; // C*nf (smoothed)
std::vector<float> Flatten() const;
};
struct NaiveBayesResult { NaiveBayesModel model; VecStats stats; };
NaiveBayesResult FitNaiveBayes(Dataset &ds, const NaiveBayesOptions &opt);
NaiveBayesResult FitNaiveBayesPlain(const float *data, u64 n_rows, u32 nf,
u64 elems_per_page, u32 nblocks,
const NaiveBayesOptions &opt);
NaiveBayesResult FitNaiveBayesReference(/* same arguments */);
u32 NaiveBayesPredict(const NaiveBayesModel &m, const float *x);The ChiMod is module ml_nb.
Related: k-nearest neighbours, logistic regression, decision tree.