machinelearning icon indicating copy to clipboard operation
machinelearning copied to clipboard

Get cluster items

Open AdamShakhabov opened this issue 8 years ago • 5 comments

Sorry for my English :)

I am using sample Clustering Iris Data. Is there anyway except cluster Id also get items which were put in the same cluster?

That is, I got prediction:

var prediction1 = model.Predict(TestIrisData.Setosa1);

and I can get cluster Id by prediction1.SelectedClusterId. But I also need to know data about another items with their features SepalLength, SepalWidth, PetalLength, PetalWidth.

AdamShakhabov avatar Jul 02 '18 20:07 AdamShakhabov

That sounds like a MATLAB-like API: clustering returns clusters ID of all data items in the given data set.

@adamshakhabov I don't know if ML.NET can return all the cluster IDs immediately after the call to the Train method, so let me to speculate how we can get cluster items ourselves.

First, it seems possible to add the feature fields to the prediction classes themselves and populate them from the feature columns. Then, to use LINQ's Where or GroupBy method.

Second, we can create mapping from data items to the cluster IDs. Use LINQ's ToDictionary method. Something like that (syntax might be not correct):

flowers.ToDictionary(
    keySelector: f => f,
    elementSelector: f => model.Predict(flower).SelectedClusterId)

Or, you may create the mapping in reverse: from cluster ID to the data items. Use LINQ's ToLookup method. Something like that (again, syntax might not compile):

IEnumerable<IrisData> flowers = ...;
var lookup = flowers.ToLookup(
    keySelector: f => model.Predict(flower).SelectedClusterId,
    elementSelector: f => f);

IEnumerable<IrisData> flowersInClusterOne = lookup[1];

Hope that will help you.

pkulikov avatar Jul 03 '18 20:07 pkulikov

Great, many thanks @pkulikov.

AdamShakhabov avatar Jul 03 '18 21:07 AdamShakhabov

Somewhat similar to the topic. I'm testing ML.NET on Iris data as well for clustering. For K means, I want to find out the optimum number for K (3 in this dataset but for others we'd need somewhat of an optimisation algorithm). Is there a way to know how many clusters the data is separated into after it is trained? i.e. after PredictionModel<IrisData, ClusterPrediction> model = Train();

To find out how many clusters there are, I'm going through the same data again and using the predict function to find out what cluster it's in, which is very inefficient. Any method to find the count of clusters would be awesome. Thanks

subash774 avatar Jul 24 '18 15:07 subash774

DRI RESPONSE: This issue looks like good candidate for cook book.

Ivanidzo4ka avatar Oct 18 '18 23:10 Ivanidzo4ka

Here's how you can find the centeroids info (the full example is here):

// Get the cluster centroids and the number of clusters k from
// KMeansModelParameters.
VBuffer<float>[] centroids = default;

var modelParams = model.Model;
modelParams.GetClusterCentroids(ref centroids, out int k);
Console.WriteLine(
    $"The first 3 coordinates of the first centroid are: " +
    string.Join(", ", centroids[0].GetValues().ToArray().Take(3)));

Console.WriteLine(
    $"The first 3 coordinates of the second centroid are: " +
    string.Join(", ", centroids[1].GetValues().ToArray().Take(3)));

// Expected output:
//   The first 3 coordinates of the first centroid are: (0.5840713, 0.5678288, 0.6221277)
//   The first 3 coordinates of the second centroid are: (0.3705794, 0.4289133, 0.4001645)

shmoradims avatar May 20 '19 23:05 shmoradims