Skip to content

System.InvalidOperationException: 'Failed to initialize clusters: too few examples' #41573

Open
@MorneZaayman

Description

@MorneZaayman

Type of issue

Typo

Description

The example iris.data file results in an exception: System.InvalidOperationException: 'Failed to initialize clusters: too few examples' being thrown. This is with Microsoft.ML 3.0.1 (Latest at the time of writing). I have attached a copy of the iris.data file.

Here is my code in Program.cs

using Microsoft.ML;
using IrisClustering;

namespace IrisClustering
{
    internal class Program
    {
        static string _dataPath = Path.Combine(Environment.CurrentDirectory, "Data", "iris.data");
        static string _modelPath = Path.Combine(Environment.CurrentDirectory, "Data", "IrisClusteringModel.zip");

        static void Main(string[] args)
        {
            var mlContext = new MLContext();
            IDataView dataView = mlContext.Data.LoadFromTextFile<IrisData>(_dataPath, hasHeader: false, separatorChar: ',');

            string featuresColumnName = "Features";
            var pipeline = mlContext.Transforms
                .Concatenate(featuresColumnName, "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
                .Append(mlContext.Clustering.Trainers.KMeans(featuresColumnName, numberOfClusters: 3));
            var model = pipeline.Fit(dataView);

            using (var fileStream = new FileStream(_modelPath, FileMode.Create, FileAccess.Write, FileShare.Write))
            {
                mlContext.Model.Save(model, dataView.Schema, fileStream);
            }

            var predictor = mlContext.Model.CreatePredictionEngine<IrisData, ClusterPrediction>(model);

            var prediction = predictor.Predict(TestIrisData.Setosa);
            Console.WriteLine($"Cluster: {prediction.PredictedClusterId}");
            Console.WriteLine($"Distances: {string.Join(" ", prediction.Distances ?? Array.Empty<float>())}");
        }
    }
}

iris.zip

Page URL

https://learn.microsoft.com/en-us/dotnet/machine-learning/tutorials/iris-clustering

Content source URL

https://github.com/dotnet/docs/blob/main/docs/machine-learning/tutorials/iris-clustering.md

Document Version Independent Id

41cafff5-6418-2e54-3a0d-ee29fb1fca1f

Article author

@pkulikov

Metadata

  • ID: 925c8fe9-ba0b-bd2c-cea0-9d851ece8825
  • Service: dotnet-ml

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions