var kMeans = new KMeans(3); // create a KMeans object with 3 clusters kMeans.Learn(data); // learn the model on data // add a new data point to the first cluster var clusterIndex = kMeans.Clusters.Nearest(dataPoint); kMeans.Clusters[clusterIndex].Add(dataPoint);
var context = new MLContext(); var dataView = context.Data.LoadFromEnumerable(data); // load data into a DataView var clustering = context.Clustering.Trainers.KMeans(3); // create a KMeans clustering algorithm with 3 clusters var model = clustering.Fit(dataView); // learn the model on data // add a new data point to the first cluster var predictions = model.Transform(dataView); var clusterIndex = predictions.GetColumn("PredictedCluster").First(); model.Model.GetClusterableCollection()[clusterIndex].Add(new VBuffer (new[] { dataPoint[0], dataPoint[1]}));
var context = new MLContext(); var dataView = context.Data.LoadFromEnumerable(data); // load data into a DataView var clustering = new KMeansPlusPlusClusterer(context, 3); // create a KMeans clustering algorithm with 3 clusters var model = clustering.Train(dataView); // learn the model on data // add a new data point to the first cluster var predictions = model.Transform(dataView); var clusterIndex = predictions.GetClusterIds().First(); clustering.Clusters.ElementAt(clusterIndex).Add(new[] { dataPoint[0], dataPoint[1] });In this example, we used the KMeansPlusPlusClusterer algorithm from Microsoft.ML to cluster data. Then we added a new data point to the cluster using the Cluster Add method. Package library: Microsoft.ML.KMeansClusteringAlgorithm