<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ibielov</title>
    <description>The latest articles on DEV Community by ibielov (@ibielov).</description>
    <link>https://dev.to/ibielov</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4272947%2F21d3f753-01be-451c-8056-72d29692d8c1.png</url>
      <title>DEV Community: ibielov</title>
      <link>https://dev.to/ibielov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ibielov"/>
    <language>en</language>
    <item>
      <title>Batched BIRCH: Set-Wise Clustering of Embedding Vectors with NumPy</title>
      <dc:creator>ibielov</dc:creator>
      <pubDate>Sun, 29 Nov 2026 20:46:33 +0000</pubDate>
      <link>https://dev.to/ibielov/batched-birch-set-wise-clustering-of-embedding-vectors-with-numpy-5722</link>
      <guid>https://dev.to/ibielov/batched-birch-set-wise-clustering-of-embedding-vectors-with-numpy-5722</guid>
      <description>&lt;p&gt;&lt;strong&gt;Batched BIRCH&lt;/strong&gt; is a NumPy-based implementation of incremental clustering that processes observations in waves instead of inserting them individually.&lt;/p&gt;

&lt;p&gt;The implementation uses vectorized similarity scoring, deterministic candidate selection, joint clustering-feature updates, and optional parallel scoring.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://github.com/ibielov/batched-birch" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Package:&lt;/strong&gt; &lt;a href="https://pypi.org/project/batched-birch/" rel="noopener noreferrer"&gt;PyPI&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;License:&lt;/strong&gt; Apache-2.0&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. Implementation
&lt;/h2&gt;

&lt;p&gt;Traditional BIRCH inserts observations sequentially. Each insertion searches an existing CF-tree, updates clustering features, and may trigger structural changes.&lt;/p&gt;

&lt;p&gt;Batched BIRCH evaluates an entire wave against a fixed snapshot of existing groups.&lt;/p&gt;

&lt;p&gt;Processing consists of seven operations:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Compute similarities between incoming vectors and group centroids.&lt;/li&gt;
&lt;li&gt;Select the top-k candidate groups for each vector.&lt;/li&gt;
&lt;li&gt;Evaluate similarity, dispersion, and capacity constraints.&lt;/li&gt;
&lt;li&gt;Select acceptable assignments using deterministic tie-breaking.&lt;/li&gt;
&lt;li&gt;Merge accepted observations into their destination groups jointly.&lt;/li&gt;
&lt;li&gt;Consolidate unassigned observations into new groups.&lt;/li&gt;
&lt;li&gt;Split groups exceeding capacity or dispersion limits.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Existing groups are not updated during candidate evaluation. Updates are applied after assignment decisions have been calculated.&lt;/p&gt;

&lt;p&gt;The current implementation maintains &lt;strong&gt;flat clustering-feature groups&lt;/strong&gt;. It does not implement hierarchical CF-tree routing.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Vectorized Candidate Evaluation
&lt;/h2&gt;

&lt;p&gt;For a wave containing &lt;code&gt;W&lt;/code&gt; vectors of dimension &lt;code&gt;D&lt;/code&gt;, and &lt;code&gt;G&lt;/code&gt; group centroids, scoring uses matrix multiplication:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt; @ &lt;span class="no"&gt;C&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output has shape &lt;code&gt;W × G&lt;/code&gt;. Input vectors are unit-normalized for similarity scoring; the implementation applies its cosine-based candidate criterion.&lt;/p&gt;

&lt;p&gt;Candidates are selected using &lt;code&gt;argpartition&lt;/code&gt;, followed by deterministic ordering. Boundary ties are resolved by group index.&lt;/p&gt;

&lt;p&gt;Candidate acceptance applies three constraints:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;min_similarity&lt;/code&gt;&lt;/strong&gt; — minimum similarity for joining a group.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;max_dispersion&lt;/code&gt;&lt;/strong&gt; — maximum permitted dispersion after insertion.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;max_children&lt;/code&gt;&lt;/strong&gt; — maximum group capacity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Acceptable candidates are ranked by resulting dispersion, similarity, and group index.&lt;/p&gt;

&lt;p&gt;With multiple workers, centroid rows are divided into blocks. Each worker calculates local scores and top-k candidates, which are merged into the global top-k without materializing the entire wave-by-group score matrix.&lt;/p&gt;

&lt;p&gt;Tests cover candidate selection consistency across one, two, and four workers. Minor floating-point differences between BLAS kernels are possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Clustering-Feature Representation
&lt;/h2&gt;

&lt;p&gt;The implementation stores numerically stable clustering features:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CF = (n, μ, S)

n = number of observations
μ = mean vector
S = sum of squared deviations from the mean

dispersion = S / n
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Statistics use Welford-style accumulation and Chan-style group merging, following established numerical methods also used in BETULA.&lt;/p&gt;

&lt;p&gt;For two groups &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;n = nA + nB
δ = μB - μA

μ = μA + δ × (nB / n)

S = SA + SB + ||δ||² × (nA × nB / n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This representation avoids cancellation errors from calculating dispersion by subtracting large raw sums of squares. It also supports joint updates: observations assigned to the same group can be aggregated independently and merged with the existing group's statistics.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Benchmarks
&lt;/h2&gt;

&lt;p&gt;Controlled benchmarks were recorded on &lt;strong&gt;November 25, 2026&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Environment:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AMD EPYC 9D25, approximately 1.5 shared CPUs&lt;/li&gt;
&lt;li&gt;Python 3.12.3&lt;/li&gt;
&lt;li&gt;NumPy 2.5.5&lt;/li&gt;
&lt;li&gt;scikit-learn 1.9.1&lt;/li&gt;
&lt;li&gt;Single-threaded BLAS for the reported controlled arms&lt;/li&gt;
&lt;li&gt;Imports and data loading excluded from fit timing&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Synthetic Dataset: 100,000 Observations
&lt;/h3&gt;

&lt;p&gt;100,000 synthetic observations, 384 dimensions, 50 generated blobs.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Implementation&lt;/th&gt;
&lt;th&gt;Fit time&lt;/th&gt;
&lt;th&gt;Groups&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Batched BIRCH, wave 256&lt;/td&gt;
&lt;td&gt;43.6 s&lt;/td&gt;
&lt;td&gt;18,824&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sklearn BIRCH, threshold 0.5&lt;/td&gt;
&lt;td&gt;211.1 s&lt;/td&gt;
&lt;td&gt;94,303&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The implementations use different acceptance rules and produce substantially different partitions. &lt;strong&gt;This is not an equivalent-clustering speedup comparison.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  WordNet Embeddings: 31,388 Observations
&lt;/h3&gt;

&lt;p&gt;31,388 selected WordNet synsets represented using 384-dimensional MiniLM embeddings.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Implementation&lt;/th&gt;
&lt;th&gt;Fit time&lt;/th&gt;
&lt;th&gt;Groups&lt;/th&gt;
&lt;th&gt;Coherence&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Batched BIRCH, wave 256&lt;/td&gt;
&lt;td&gt;5.07 s&lt;/td&gt;
&lt;td&gt;4,796&lt;/td&gt;
&lt;td&gt;0.504&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sklearn BIRCH, threshold 0.5&lt;/td&gt;
&lt;td&gt;15.62 s&lt;/td&gt;
&lt;td&gt;20,312&lt;/td&gt;
&lt;td&gt;0.718&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sklearn BIRCH, threshold 0.865&lt;/td&gt;
&lt;td&gt;4.85 s&lt;/td&gt;
&lt;td&gt;5,278&lt;/td&gt;
&lt;td&gt;0.373&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Coherence is the pair-weighted mean intra-cluster cosine similarity.&lt;/p&gt;

&lt;p&gt;The final sklearn configuration approximates Batched BIRCH's group count but does not match its clustering semantics. At this granularity, sklearn was slightly faster.&lt;/p&gt;

&lt;p&gt;The exact WordNet subset cannot be reconstructed solely from public source data. The repository documents a method-level regeneration procedure. The synthetic benchmarks are reproducible from the published scripts and seeds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Parallel Scoring
&lt;/h3&gt;

&lt;p&gt;Measurements on an eight-core AMD Ryzen 7 7735HS laptop, using 100,000 synthetic vectors:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Workers&lt;/th&gt;
&lt;th&gt;Runtime&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;45.9 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;35.1 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;31.5 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;All configurations produced 18,824 groups.&lt;/p&gt;

&lt;p&gt;The one-worker measurement used an earlier code revision than the later worker measurements. These figures are preliminary scaling evidence, &lt;strong&gt;not a controlled same-revision speedup comparison&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Parallel scoring does not eliminate sequential consolidation and splitting costs.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Wave-Size Behavior
&lt;/h2&gt;

&lt;p&gt;Wave size affects clustering results because observations within one wave are evaluated against a fixed group snapshot.&lt;/p&gt;

&lt;p&gt;Measured group counts on the WordNet dataset:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Wave size&lt;/th&gt;
&lt;th&gt;Groups&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;4,497&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;td&gt;4,689&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;256&lt;/td&gt;
&lt;td&gt;4,796&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1,024&lt;/td&gt;
&lt;td&gt;4,965&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;wave_size=1&lt;/code&gt; provides sequential insertion behavior within this implementation, but does not reproduce sklearn's hierarchical BIRCH algorithm.&lt;/p&gt;

&lt;p&gt;For reproducibility, fix the input ordering, wave size, and configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Limitations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Scoring Complexity
&lt;/h3&gt;

&lt;p&gt;Candidate scoring costs &lt;strong&gt;&lt;code&gt;O(W × G × D)&lt;/code&gt; per wave&lt;/strong&gt;. The flat group structure requires scoring incoming observations against existing group centroids.&lt;/p&gt;

&lt;p&gt;Without hierarchical routing, total processing cost may approach quadratic growth as the number of groups increases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Memory
&lt;/h3&gt;

&lt;p&gt;The implementation retains normalized &lt;code&gt;float64&lt;/code&gt; observation vectors for group splitting.&lt;/p&gt;

&lt;p&gt;For one million observations with 384 dimensions, retained vector history requires approximately &lt;strong&gt;3.07 GB&lt;/strong&gt;, excluding additional allocations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Partition Compatibility
&lt;/h3&gt;

&lt;p&gt;Batched BIRCH is &lt;strong&gt;not a drop-in replacement&lt;/strong&gt; for &lt;code&gt;sklearn.cluster.Birch&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The implementations differ in group structure, similarity metric, acceptance rules, insertion semantics, and resulting partitions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Semantic Correctness
&lt;/h3&gt;

&lt;p&gt;Embedding similarity does not guarantee semantic equivalence.&lt;/p&gt;

&lt;p&gt;The WordNet evaluation identified clusters combining unrelated senses of the same word. Applications requiring semantic correctness need additional validation.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Installation and Usage
&lt;/h2&gt;

&lt;p&gt;Requires Python 3.10 or later and NumPy 1.24 or later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip install batched-birch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;batched_birch&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;BirchConfig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;BirchTree&lt;/span&gt;

&lt;span class="no"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"vectors.npy"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BirchConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;min_similarity&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;max_dispersion&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;max_children&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;candidate_count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;workers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;tree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BirchTree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wave_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;256&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;n_groups&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;labels_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Incremental processing is available through &lt;code&gt;partial_fit()&lt;/code&gt;. Lower-level interfaces expose wave placement, clustering-feature tables, and group splitting.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Source and Reproducibility
&lt;/h2&gt;

&lt;p&gt;Batched BIRCH 0.1.0 is published under the Apache-2.0 license.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Source code:&lt;/strong&gt; &lt;a href="https://github.com/ibielov/batched-birch" rel="noopener noreferrer"&gt;https://github.com/ibielov/batched-birch&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PyPI:&lt;/strong&gt; &lt;a href="https://pypi.org/project/batched-birch/" rel="noopener noreferrer"&gt;https://pypi.org/project/batched-birch/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Benchmark methodology:&lt;/strong&gt; &lt;a href="https://github.com/ibielov/batched-birch/blob/main/benchmarks/REPRODUCING.md" rel="noopener noreferrer"&gt;https://github.com/ibielov/batched-birch/blob/main/benchmarks/REPRODUCING.md&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recorded results:&lt;/strong&gt; &lt;a href="https://github.com/ibielov/batched-birch/tree/main/results" rel="noopener noreferrer"&gt;https://github.com/ibielov/batched-birch/tree/main/results&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The repository contains benchmark scripts, recorded measurements, tests, and implementation documentation.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>machinelearning</category>
      <category>opensource</category>
      <category>python</category>
    </item>
  </channel>
</rss>
