Generated reference › K Means Assign — Machine Learning/Classical Models
kind: generated#block#machine-learning-classical-models

K Means Assign — Machine Learning/Classical Models

Machine_Learning/Classical_Models/K_Means_Assign · 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.

K Means Assign

Machine Learning / Classical Models

Assigns the input to the nearest of K fitted cluster centroids, and reports how far away it was:

c = Σj (uj − Ccj, idx = argmincc + base, d² = mincc

This is the inference half of k-means – KMeans.predict with cluster_centers_ pasted in. The distance output is what makes it usable as a monitor rather than only a classifier: a sample far from every centroid still gets an index, and only the distance says the model has never seen anything like it.

Ports

  • u – the sample, a column [d,1]. d must equal the number of COLUMNS of Centroids, and it is checked rather than broadcast.
  • idx – a scalar [1,1]: which centroid won, plus Index Base.
  • d2 – a scalar [1,1]: the squared distance to that centroid. See Notes – the square root is deliberately not taken here.

Parameters

  • CentroidsC, a [K,d] matrix with one centroid per row. That is KMeans.cluster_centers_'s own orientation, so it pastes in without transposing. K is the number of clusters and d the feature count.
  • Index Base – what the first cluster is called:
    • Zero-based (sklearn) – the first cluster is 0. The default, and what KMeans.predict returns.
    • One-based (MATLAB) – the first cluster 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 centroid is unrolled and baked in as literals at full setprecision(17); there is no tunable parameter, because a fitted centroid is not something to retune on the target.

The three HDL targets are ordinary Q16.16 fixed point and are offered for synthesis: squares, sums and comparisons only.

Simulink bridge

None. Simulink has no k-means block – clustering lives in the Statistics and Machine Learning Toolbox as MATLAB functions, not as library blocks, so there is nothing with parameters for this to map onto. The bridge reports the block rather than dropping it silently, and it has no parity testbench, which is the documented consequence of Support::None rather than a gap.

Notes

  • Algebraic and stateless: the outputs depend only on the current input, so the block cannot break an algebraic loop.
  • Nonlinear and discontinuous, and deliberately carries no state space.
  • Why the distance is squared. Ranking by squared distance and by distance give the same winner – the square root is monotone – so omitting it changes no decision and keeps every backend exact and the HDL targets synthesizable. If you want the Euclidean distance, put a Sqrt block on d2 and pay for the root only where you need it.
  • Ties go to the lowest index, the same rule Argmax Decision uses, mirrored for a minimum: the scan replaces its running best only on a strictly smaller distance. Two blocks that both reduce a vector to a decision must agree about this or a graph using both contradicts itself.
  • At a near-tie the HDL targets can assign differently, for the reason Argmax Decision's description spells out: they compare Q16.16 values where the in-app run compares doubles, so two centroids nearly equidistant can rank the other way and the index output then differs by a whole cluster. That is inherent to a discrete decision in fixed point, not a codegen defect.

Code facts#

FactValue
registered typeMachine_Learning/Classical_Models/K_Means_Assign
familyMachine_Learning/Classical_Models
solver environment classICoreBlock_0_Machine_Learning_1_Classical_Models_2_K_Means_Assign
sourcesrc/ICoreSDK/ICoreBlockLibrary/Blocks/Machine_Learning/Classical_Models/K_Means_Assign/ICoreBlock_0_Machine_Learning_1_Classical_Models_2_K_Means_Assign.cpp
headersrc/ICoreSDK/ICoreBlockLibrary/Blocks/Machine_Learning/Classical_Models/K_Means_Assign/ICoreBlock_0_Machine_Learning_1_Classical_Models_2_K_Means_Assign.h
default size on canvas126 × 84 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
3outICoreDoubled2

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
Centroids[0.4 -0.3; -0.6 0.8; 1.1 0.2]
Index BaseZero-based (sklearn)%~%One-based (MATLAB)~~Zero-based (sk…

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: clustering lives in the Statistics and Machine Learning Toolbox as MATLAB functions rather than as library blocks, so there is no block with parameters this one could map onto

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).

K Means Assign — nearest fitted centroid, and how far away it was d2_c = sum_j (u_j - C[c][j])^2, idx = argmin_c d2_c + base, d2 = min_c d2_c

Fully unrolled at export time: K and d are known once the config is loaded, so every backend gets straight-line multiply-accumulate and comparison, with no loop bound and no index type. Nothing here is transcendental and nothing divides, so the three HDL targets are genuine Q16.16 -- see the header for why the distance is left SQUARED to keep it that way.

Sample results#

No stimulus produced a sampled output in this rig — Invalid input size at: ICore Blocks/Home/K Means Assign. 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__K_Means_Assign.json