Skip to content
Branch: master
Find file History
bamurtaugh and CESARDELATORRE Migration/v1.3.1 (#597)
* Add anomaly detection example to solution

* Updated label/score printing for anomaly detect

With ML.NET v1.3.0, fixed issue where Predicted Label was always true. No longer need "hack" of comparing score to 0.2

* Update build props nuget versions

ML.NET v1.3.1, ML Preview 0.15.0

* Renamed solution to match v1.3.1

* Update C# readmes to v1.3.1

* Update F# E2E readme

* Update F# getting started readmes

Change to v1.3.1

* Rename F# solution to v1.3.1

* Update to preview v0.15.1

* Changed to ML from MLPreview

Update TimeSeries to v1.3.1 instead of preview

* Update timeseries from preview to regular v1.3.1

* Change TimeSeries from preview to regular v1.3.1

* Update TensorFlow from Preview to regular v1.3.1

* Update TensorFlow from preview to regular v1.3.1
Latest commit 2feb479 Aug 7, 2019

Clustering Iris Data

ML.NET version API type Status App Type Data type Scenario ML Task Algorithms
v1.3.1 Dynamic API Up-to-date Console app .txt file Clustering Iris flowers Clustering K-means++

In this introductory sample, you'll see how to use ML.NET to divide iris flowers into different groups that correspond to different types of iris. In the world of machine learning, this task is known as clustering.


To demonstrate clustering API in action, we will use three types of iris flowers: setosa, versicolor, and virginica. All of them are stored in the same dataset. Even though the type of these flowers is known, we will not use it and run clustering algorithm only on flower parameters such as petal length, petal width, etc. The task is to group all flowers into three different clusters. We would expect the flowers of different types belong to different clusters.

The inputs of the model are following iris parameters:

  • petal length
  • petal width
  • sepal length
  • sepal width

ML task - Clustering

The generalized problem of clustering is to group a set of objects in such a way that objects in the same group are more similar to each other than to those in other groups.

Some other examples of clustering:

  • group news articles into topics: sports, politics, tech, etc.
  • group customers by purchase preferences.
  • divide a digital image into distinct regions for border detection or object recognition.

Clustering can look similar to multiclass classification, but the difference is that for clustering tasks we don't know the answers for the past data. So there is no "tutor"/"supervisor" that can tell if our algorithm's prediction was right or wrong. This type of ML task is called unsupervised learning.


To solve this problem, first we will build and train an ML model. Then we will use trained model for predicting a cluster for iris flowers.

1. Build model

Building a model includes: uploading data (iris-full.txt with TextLoader), transforming the data so it can be used effectively by an ML algorithm (with Concatenate), and choosing a learning algorithm (KMeans). All of those steps are stored in trainingPipeline:

//Create the MLContext to share across components for deterministic results
MLContext mlContext = new MLContext(seed: 1);  //Seed set to any number so you have a deterministic environment

// STEP 1: Common data loading configuration
IDataView fullData = mlContext.Data.LoadFromTextFile(path: DataPath,
                                                                new TextLoader.Column(DefaultColumnNames.Label, DataKind.Single, 0),
                                                                new TextLoader.Column(nameof(IrisData.SepalLength), DataKind.Single, 1),
                                                                new TextLoader.Column(nameof(IrisData.SepalWidth), DataKind.Single, 2),
                                                                new TextLoader.Column(nameof(IrisData.PetalLength), DataKind.Single, 3),
                                                                new TextLoader.Column(nameof(IrisData.PetalWidth), DataKind.Single, 4),
//Split dataset in two parts: TrainingDataset (80%) and TestDataset (20%)
DataOperationsCatalog.TrainTestData trainTestData = mlContext.Data.TrainTestSplit(fullData, testFraction: 0.2);
trainingDataView = trainTestData.TrainSet;
testingDataView = trainTestData.TestSet;

//STEP 2: Process data transformations in pipeline
var dataProcessPipeline = mlContext.Transforms.Concatenate("Features", nameof(IrisData.SepalLength), nameof(IrisData.SepalWidth), nameof(IrisData.PetalLength), nameof(IrisData.PetalWidth));

// STEP 3: Create and train the model     
var trainer = mlContext.Clustering.Trainers.KMeans(featureColumnName: "Features", numberOfClusters: 3);
var trainingPipeline = dataProcessPipeline.Append(trainer);

2. Train model

Training the model is a process of running the chosen algorithm on the given data. To perform training you need to call the Fit() method.

var trainedModel = trainingPipeline.Fit(trainingDataView);

3. Consume model

After the model is build and trained, we can use the Predict() API to predict the cluster for an iris flower and calculate the distance from given flower parameters to each cluster (each centroid of a cluster).

                // Test with one sample text 
                var sampleIrisData = new IrisData()
                    SepalLength = 3.3f,
                    SepalWidth = 1.6f,
                    PetalLength = 0.2f,
                    PetalWidth = 5.1f,

                // Create prediction engine related to the loaded trained model
                var predEngine = mlContext.Model.CreatePredictionEngine<IrisData, IrisPrediction>(model);

                var resultprediction = predEngine.Predict(sampleIrisData);
                Console.WriteLine($"Cluster assigned for setosa flowers:" + resultprediction.SelectedClusterId);
You can’t perform that action at this time.