Generated reference › Multinomial Naive Bayes — Machine Learning/Classical Models
kind: generated#block#machine-learning-classical-models

Multinomial Naive Bayes — Machine Learning/Classical Models

Machine_Learning/Classical_Models/Multinomial_Naive_Bayes · 1 input / 2 output port(s) at insert · exports to Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text

Description#

The block's own DESCRIPTION_HTML, rendered verbatim — the same text the config dialog's info panel and the library navigator show. Fix a wrong sentence in the block's .cpp (R-D9), never here.

Multinomial Naive Bayes

Machine Learning / Classical Models

A fitted multinomial naive Bayes classifier, evaluated one sample at a time. Each class scores the sample as a weighted count, and the highest score wins:

logp[k] = logprior[k] + Σj u[j]·logtheta[k][j]

This is sklearn.naive_bayes.MultinomialNB with class_log_prior_ and feature_log_prob_ pasted in. Both are already logarithms in sklearn and are taken as logarithms here, so nothing is re-derived and the score is a plain dot product plus a constant.

It is the counterpart of Gaussian Naive Bayes, and the choice between them is about the features rather than the classifier: continuous measurements get a Gaussian each, counts or frequencies get this one.

Ports

  • u – the sample, a [d,1] column of counts or frequencies, where d is the number of COLUMNS of Feature Log Probabilities. One sample per step; it is checked rather than broadcast.
  • idx[1,1], the winning class, plus Index Base.
  • logp[K,1], every class's log-score in class order. The winner's own score is logp[idx]; feed the whole vector to Softmax for posteriors, or to Confidence Gate or Top K for a decision with a threshold.

Parameters

  • Feature Log Probabilitieslogtheta, a [K,d] matrix, one class per ROW: sklearn's feature_log_prob_ orientation, so it pastes in without transposing. These are LOGARITHMS of probabilities and are therefore normally negative; a row of them corresponds to one class's feature distribution. Its shape decides both K and d.
  • Class Log Priors[K,1], sklearn's class_log_prior_ – again already logarithms. Adding the same constant to every entry changes no decision, since the scores shift together.
  • Index Base – what the first class is numbered, spelled exactly as Argmax Decision, K Means Assign and Gaussian Naive Bayes spell it:
    • Zero-based (PyTorch, numpy) – the first class is 0.
    • One-based (MATLAB) – the first class is 1.
  • Sampling Time (s) – zero or less inherits the solver's rate; a positive value runs the block at that period.

Code export

All ten targets: Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog and PLC Structured Text. Every class's score is unrolled at export time into one multiply-add per feature with the log-probability as a literal, then a comparison chain.

The three HDL targets are genuine synthesizable Q16.16: there is no logarithm, no division and no exponential in the datapath, because the logarithms were taken when the model was fitted.

Simulink bridge

None. Simulink's classifier blocks live in the Statistics and Machine Learning Toolbox, which is not installed on this machine, so there is no predict block here to map onto and a bridge could not be verified even if it were written. Those blocks also take a fitted model OBJECT rather than parameter matrices, which no configuration value can carry. The block therefore has no parity testbench, which is the documented consequence of Support::None. Code export verification still covers it across all ten languages.

Notes

  • Stateless and algebraic.
  • The score is LINEAR in the input, so this block is Dense Layer(Linear) followed by Argmax Decision in disguise – and it exists as its own block anyway, because a user pasting feature_log_prob_ and class_log_prior_ straight out of sklearn should not have to know that, nor rearrange them into a weight matrix and a bias.
  • No state space: the block ends in a comparison, which is not linear.
  • Ties go to the LOWEST class indexArgmax Decision's rule, spelled identically in every backend.
  • The index is a DISCONTINUOUS function of the input, so the three HDL targets can rank two classes within one Q16.16 quantum (about 1.5×10−5) the other way round. The logp output is continuous and does not have that property.
  • Counts, not measurements. Fed continuous features it will still compute, but the model behind it assumed counts; for continuous features use Gaussian Naive Bayes.

Code facts#

FactValue
registered typeMachine_Learning/Classical_Models/Multinomial_Naive_Bayes
familyMachine_Learning/Classical_Models
solver environment classICoreBlock_0_Machine_Learning_1_Classical_Models_2_Multinomial_Naive_Bayes
sourcesrc/ICoreSDK/ICoreBlockLibrary/Blocks/Machine_Learning/Classical_Models/Multinomial_Naive_Bayes/ICoreBlock_0_Machine_Learning_1_Classical_Models_2_Multinomial_Naive_Bayes.cpp
headersrc/ICoreSDK/ICoreBlockLibrary/Blocks/Machine_Learning/Classical_Models/Multinomial_Naive_Bayes/ICoreBlock_0_Machine_Learning_1_Classical_Models_2_Multinomial_Naive_Bayes.h
default size on canvas140 × 88 px
ports at insert1 in, 2 out
code generators implementedPython, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text

Ports#

#DirectionSignal typeDescription label
1inICoreDoubleu
2outICoreDoubleidx
3outICoreDoublelogp

Ports the constructor creates. A block whose port list changes with its configuration adds or removes ports at load time; the count above is the one a freshly inserted block has.

Configuration variables#

Config variableDefaultSimulink parameter
Feature Log Probabilities[-0.69314718055994531 -1.2039728043259361 -1.609437912434…
Class Log Priors[-0.51082562376599072; -0.91629073187415511]
Index BaseZero-based (PyTorch, numpy)%~%One-based (MATLAB)~~Zero-ba…

Every block also carries Sampling Time (s) from ICoreBlockSolverEnvironment: zero or less inherits the solver's rate, a positive value runs the block at that period.

supportSupport::None
Simulink path
port-count rulePortsParam::None
SampleTime parameteryes

Caveat (shown to the user): no Simulink equivalent available here: its naive Bayes predict block lives in the Statistics and Machine Learning Toolbox, which is not installed on this machine, so no mapping could be verified; and those blocks take a fitted model OBJECT rather than the log-probability tables this block configures

Catalog contract: src/ICoreSDK/ICoreCoder/ICoreCommandSystem/SimulinkBridge/ICoreSimulinkBlockCatalog.h

Description vs code#

The checker has a blind spot here — it could not resolve something (a grouped port bullet, a computed config name), which is reported and never counted as a pass. A reader has to settle it:

  • B0 every stimulus in the sample errored — cross-checks skipped

The verdict above is tools/docs/check_block_descriptions.py (P7.1), which compares LISTS. It cannot read a sentence: "stateless" on a block with a state, an initial-value semantic the recursion does not implement, a "not synthesizable" caveat the HDL banner contradicts. That is the agent audit (P7.3) on BLOCK_DESCRIPTION_AUDIT.md, and this tool's green is not a substitute for one.

File banner (developer view)#

The top comment of the block's .cpp — the maths, the realization and the export strategy, addressed to whoever changes it. It must not contradict the description above (P7.5).

Multinomial Naive Bayes — a fitted MultinomialNB evaluated at inference logp[k] = logprior[k] + SUM_j u[j] * logtheta[k][j] idx = argmax_k logp[k] + base

Read the header before this file. The short version: both config tables are ALREADY logarithms (sklearn exposes them that way), so nothing is folded, nothing is re-derived, and the score is a dot product plus a constant -- which is why the three HDL targets stay genuine Q16.16.

The scan, the two-output shape and the tie rule are Gaussian_Naive_Bayes's, which are Argmax_Decision's. The two blocks differ in exactly one expression.

Sample results#

No stimulus produced a sampled output in this rig — Invalid input size at: ICore Blocks/Home/Multinomial Naive Bayes. That is a fact about the single-block rig, not a verdict on the block: an offline batch fit, a block whose output only appears at onSolverFinish, or one that needs a driven environment cannot be exercised alone.

Category unsampled · sample time 0.1 · 60 steps · commit ccf005c8 · produced by docsSample --out <folder> --steps 60

Sample data: docs/generated/samples/Machine_Learning__Classical_Models__Multinomial_Naive_Bayes.json