-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Iris Clustering sample updated to v0.7 and common-code approach
- Loading branch information
1 parent
4487efc
commit 8caa168
Showing
18 changed files
with
234 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
samples/csharp/getting-started/Clustering_Iris/Clustering_Iris.csproj
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
28 changes: 28 additions & 0 deletions
28
...ng-started/Clustering_Iris/IrisClustering/IrisClusteringConsoleApp/Clustering_Iris.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\..\..\..\common\ConsoleHelper.cs" Link="Common\ConsoleHelper.cs" /> | ||
<Compile Include="..\..\..\..\common\ModelBuilder.cs" Link="Common\ModelBuilder.cs" /> | ||
<Compile Include="..\..\..\..\common\ModelScorer.cs" Link="Common\ModelScorer.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.ML" Version="0.7.0-preview-27031-8" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="datasets\iris-full.txt"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Common\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
42 changes: 42 additions & 0 deletions
42
...arp/getting-started/Clustering_Iris/IrisClustering/IrisClusteringConsoleApp/DataLoader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Microsoft.ML; | ||
using Microsoft.ML.Core.Data; | ||
using Microsoft.ML.Runtime.Data; | ||
using Microsoft.ML.Transforms; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Clustering_Iris | ||
{ | ||
class DataLoader | ||
{ | ||
MLContext _mlContext; | ||
private TextLoader _loader; | ||
|
||
public DataLoader(MLContext mlContext) | ||
{ | ||
_mlContext = mlContext; | ||
|
||
// Create the TextLoader by defining the data columns and where to find (column position) them in the text file. | ||
_loader = mlContext.Data.TextReader(new TextLoader.Arguments() | ||
{ | ||
Separator = "\t", | ||
HasHeader = true, | ||
Column = new[] | ||
{ | ||
new TextLoader.Column("Label", DataKind.R4, 0), | ||
new TextLoader.Column("SepalLength", DataKind.R4, 1), | ||
new TextLoader.Column("SepalWidth", DataKind.R4, 2), | ||
new TextLoader.Column("PetalLength", DataKind.R4, 3), | ||
new TextLoader.Column("PetalWidth", DataKind.R4, 4), | ||
} | ||
}); | ||
} | ||
|
||
public IDataView GetDataView(string filePath) | ||
{ | ||
return _loader.Read(filePath); | ||
} | ||
} | ||
} | ||
|
23 changes: 23 additions & 0 deletions
23
.../getting-started/Clustering_Iris/IrisClustering/IrisClusteringConsoleApp/DataProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Microsoft.ML; | ||
using Microsoft.ML.Core.Data; | ||
using Microsoft.ML.Runtime.Data; | ||
using Microsoft.ML.Transforms; | ||
using Microsoft.ML.Transforms.Categorical; | ||
using Microsoft.ML.Transforms.PCA; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Clustering_Iris | ||
{ | ||
public class DataProcessor | ||
{ | ||
public IEstimator<ITransformer> DataProcessPipeline { get; private set; } | ||
|
||
public DataProcessor(MLContext mlContext) | ||
{ | ||
// Configure data transformations in the DataProcess pipeline | ||
DataProcessPipeline = mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...tarted/Clustering_Iris/IrisClustering/IrisClusteringConsoleApp/DataStructures/IrisData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Microsoft.ML.Runtime.Api; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Clustering_Iris.DataStructures | ||
{ | ||
public class IrisData | ||
{ | ||
[Column("0")] | ||
public float Label; | ||
|
||
[Column("1")] | ||
public float SepalLength; | ||
|
||
[Column("2")] | ||
public float SepalWidth; | ||
|
||
[Column("3")] | ||
public float PetalLength; | ||
|
||
[Column("4")] | ||
public float PetalWidth; | ||
|
||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../Clustering_Iris/IrisClustering/IrisClusteringConsoleApp/DataStructures/IrisPrediction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Microsoft.ML.Runtime.Api; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Clustering_Iris.DataStructures | ||
{ | ||
// IrisPrediction is the result returned from prediction operations | ||
public class IrisPrediction | ||
{ | ||
[ColumnName("PredictedLabel")] | ||
public uint SelectedClusterId; | ||
|
||
[ColumnName("Score")] | ||
public float[] Distance; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...csharp/getting-started/Clustering_Iris/IrisClustering/IrisClusteringConsoleApp/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.IO; | ||
|
||
using Microsoft.ML; | ||
using Common; | ||
using Clustering_Iris.DataStructures; | ||
|
||
namespace Clustering_Iris | ||
{ | ||
internal static class Program | ||
{ | ||
private static string AppPath => Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]); | ||
|
||
private static string BaseDatasetsLocation = @"../../../../Data"; | ||
private static string DataPath = $"{BaseDatasetsLocation}/iris-full.txt"; | ||
|
||
private static string BaseModelsPath = @"../../../../MLModels"; | ||
private static string ModelPath = $"{BaseModelsPath}/IrisModel.zip"; | ||
|
||
private static void Main(string[] args) | ||
{ | ||
//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 | ||
DataLoader dataLoader = new DataLoader(mlContext); | ||
var trainingDataView = dataLoader.GetDataView(DataPath); | ||
|
||
//STEP 2: Process data transformations in pipeline | ||
var dataProcessor = new DataProcessor(mlContext); | ||
var dataProcessPipeline = dataProcessor.DataProcessPipeline; | ||
|
||
// (Optional) Peek data in training DataView after applying the ProcessPipeline's transformations | ||
Common.ConsoleHelper.PeekDataViewInConsole<IrisData>(mlContext, trainingDataView, dataProcessPipeline, 10); | ||
Common.ConsoleHelper.PeekVectorColumnDataInConsole(mlContext, "Features", trainingDataView, dataProcessPipeline, 10); | ||
|
||
// STEP 3: Create and train the model | ||
var modelBuilder = new ModelBuilder<IrisData, IrisPrediction>(mlContext, dataProcessPipeline); | ||
var trainer = mlContext.Clustering.Trainers.KMeans(features: "Features", clustersCount: 3); | ||
modelBuilder.AddTrainer(trainer); | ||
var trainedModel = modelBuilder.Train(trainingDataView); | ||
|
||
// STEP4: Evaluate accuracy of the model | ||
var metrics = modelBuilder.EvaluateClusteringModel(trainingDataView); | ||
Common.ConsoleHelper.PrintClusteringMetrics("KMeans", metrics); | ||
|
||
// STEP5: Save/persist the model as a .ZIP file | ||
modelBuilder.SaveModelAsFile(ModelPath); | ||
|
||
Console.WriteLine("=============== End of training process ==============="); | ||
|
||
Console.WriteLine("=============== Predict a cluster for a single case (Single Iris data sample) ==============="); | ||
|
||
// Test with one sample text | ||
var sampleIrisData = new IrisData() | ||
{ | ||
SepalLength = 3.3f, | ||
SepalWidth = 1.6f, | ||
PetalLength = 0.2f, | ||
PetalWidth = 5.1f, | ||
}; | ||
|
||
//Create the clusters: Create data files and plot a chart | ||
var modelScorer = new ModelScorer<IrisData, IrisPrediction>(mlContext); | ||
modelScorer.LoadModelFromZipFile(ModelPath); | ||
|
||
var prediction = modelScorer.PredictSingle(sampleIrisData); | ||
|
||
Console.WriteLine($"Cluster assigned for setosa flowers:"+prediction.SelectedClusterId); | ||
|
||
Console.WriteLine("=============== End of process, hit any key to finish ==============="); | ||
Console.ReadKey(); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} |
Oops, something went wrong.