dotnet / dotnet/docs

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

Open
#41,573 0 comments 0 reactions 0 assignees View on GitHub
:watch: Not Triaged dotnet-ml/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
19h 10m
Merged PRs (30d)
268

Description

### 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(_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(model);

var prediction = predictor.Predict(TestIrisData.Setosa);
Console.WriteLine($"Cluster: {prediction.PredictedClusterId}");
Console.WriteLine($"Distances: {string.Join(" ", prediction.Distances ?? Array.Empty())}");
}
}
}
```
[iris.zip](https://github.com/user-attachments/files/16010037/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**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.