OnlineKMeans<T>
Models & Types · AiDotNet.Clustering.Streaming
Online K-Means clustering for streaming data.
Inheritance: ClusteringBase<T> → OnlineKMeans<T>
For Beginners
Online K-Means learns continuously.
Think of it like learning a new skill:
- Each example you see teaches you a little bit
- You don’t need to remember every example
- Your understanding improves over time
The learning rate is like “how much you learn from each example”:
- High: Quick learner, but might forget old lessons
- Low: Slow learner, but very stable knowledge
Use cases:
- Real-time sensor data
- Social media streams
- Financial market data
- Any data that arrives continuously
How It Works
Online K-Means processes data points one at a time, updating cluster centers incrementally. This is suitable for streaming data or when memory is limited.
Algorithm:
- Initialize K cluster centers
- For each incoming point:
a. Find the nearest cluster center b. Move that center slightly toward the point c. Update cluster assignment
- Optionally decay learning rate over time
Example
using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.Clustering.Streaming;
using AiDotNet.Tensors.LinearAlgebra;
var data = new Matrix<double>(6, 2);
double[][] rows = { new[] { 1.0, 1.0 }, new[] { 1.2, 0.9 }, new[] { 1.1, 1.1 },
new[] { 8.0, 8.0 }, new[] { 8.2, 7.9 }, new[] { 7.9, 8.1 } };
for (int i = 0; i < 6; i++) { data[i, 0] = rows[i][0]; data[i, 1] = rows[i][1]; }
var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
.ConfigureModel(new OnlineKMeans<double>())
.ConfigureDataLoader(DataLoaders.FromMatrix(data))
.BuildAsync();
var labels = result.Predict(data);
Console.WriteLine($"OnlineKMeans: clustered {labels.Length} points.");
Constructors
| Constructor | Summary |
|---|---|
OnlineKMeans(OnlineKMeansOptions<T> options) |
Initializes a new Online K-Means instance. |
Properties
| Property | Summary |
|---|---|
double CurrentLearningRate { get; set; } |
Gets the current learning rate. |
long TotalPointsSeen { get; } |
Gets the total number of points seen during training. |
Methods
| Method | Summary |
|---|---|
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) |
|
ModelOptions GetOptions() |
|
Vector<T> FitPredict(Matrix<T> x) |
|
Vector<T> Predict(Matrix<T> x) |
|
int PartialFit(Vector<T> point) |
Processes a single data point (true online/streaming mode). |
void InitializeWithRandom(int numFeatures, T[] minValues, T[] maxValues) |
Initializes centers randomly for streaming mode. |
void Train(Matrix<T> x, Vector<T> y) |