private static void Main(string[] args) { // Create ML.NET context/environment using (var env = new LocalEnvironment()) { // Create DataReader with data schema mapped to file's columns var reader = new TextLoader(env, 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), } }); //Load training data IDataView trainingDataView = reader.Read(new MultiFileSource(DataPath)); // Transform your data and add a learner // Add a learning algorithm to the pipeline. e.g.(What are characteristics of iris is this?) // Convert the Label back into original text (after converting to number in step 3) var pipeline = new ConcatEstimator(env, "Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth") .Append(new KMeansPlusPlusTrainer(env, "Features", clustersCount: 3)); // Create and train the model Console.WriteLine("=============== Create and Train the Model ==============="); var model = pipeline.Fit(trainingDataView); Console.WriteLine("=============== End of training ==============="); Console.WriteLine(); // Test with one sample text var sampleIrisData = new IrisData() { SepalLength = 3.3f, SepalWidth = 1.6f, PetalLength = 0.2f, PetalWidth = 5.1f, }; var prediction = model.MakePredictionFunction <IrisData, IrisPrediction>(env).Predict( sampleIrisData); Console.WriteLine($"Clusters assigned for setosa flowers:" + prediction.SelectedClusterId); // Save model to .ZIP file SaveModelAsFile(env, model); // Predict again but now testing the model loading from the .ZIP file PredictWithModelLoadedFromFile(sampleIrisData); Console.WriteLine("=============== End of process, hit any key to finish ==============="); Console.ReadKey(); } }
private static void PredictWithModelLoadedFromFile(IrisData sampleData) { // Test with Loaded Model from .zip file using (var env = new LocalEnvironment()) { ITransformer loadedModel; using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { loadedModel = TransformerChain.LoadFrom(env, stream); } // Create prediction engine and make prediction. var prediction = loadedModel.MakePredictionFunction <IrisData, IrisPrediction>(env).Predict( new IrisData() { SepalLength = 3.3f, SepalWidth = 1.6f, PetalLength = 0.2f, PetalWidth = 5.1f, }); Console.WriteLine(); Console.WriteLine($"Clusters assigned for setosa flowers:" + prediction.SelectedClusterId); } }
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 fullData = dataLoader.GetDataView(DataPath); (IDataView trainingDataView, IDataView testingDataView) = mlContext.Clustering.TrainTestSplit(fullData, testFraction: 0.2); //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(testingDataView); Common.ConsoleHelper.PrintClusteringMetrics(trainer.ToString(), 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(); }
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 configuration IDataView fullData = mlContext.Data.ReadFromTextFile(path: DataPath, columns: new[] { new TextLoader.Column(DefaultColumnNames.Label, DataKind.R4, 0), new TextLoader.Column(nameof(IrisData.SepalLength), DataKind.R4, 1), new TextLoader.Column(nameof(IrisData.SepalWidth), DataKind.R4, 2), new TextLoader.Column(nameof(IrisData.PetalLength), DataKind.R4, 3), new TextLoader.Column(nameof(IrisData.PetalWidth), DataKind.R4, 4), }, hasHeader: true, separatorChar: '\t'); //Split dataset in two parts: TrainingDataset (80%) and TestDataset (20%) (IDataView trainingDataView, IDataView testingDataView) = mlContext.Clustering.TrainTestSplit(fullData, testFraction: 0.2); //STEP 2: Process data transformations in pipeline var dataProcessPipeline = mlContext.Transforms.Concatenate(DefaultColumnNames.Features, nameof(IrisData.SepalLength), nameof(IrisData.SepalWidth), nameof(IrisData.PetalLength), nameof(IrisData.PetalWidth)); // (Optional) Peek data in training DataView after applying the ProcessPipeline's transformations Common.ConsoleHelper.PeekDataViewInConsole <IrisData>(mlContext, trainingDataView, dataProcessPipeline, 10); Common.ConsoleHelper.PeekVectorColumnDataInConsole(mlContext, DefaultColumnNames.Features, trainingDataView, dataProcessPipeline, 10); // STEP 3: Create and train the model var trainer = mlContext.Clustering.Trainers.KMeans(featureColumn: DefaultColumnNames.Features, clustersCount: 3); var trainingPipeline = dataProcessPipeline.Append(trainer); var trainedModel = trainingPipeline.Fit(trainingDataView); // STEP4: Evaluate accuracy of the model IDataView predictions = trainedModel.Transform(testingDataView); var metrics = mlContext.Clustering.Evaluate(predictions, score: DefaultColumnNames.Score, features: DefaultColumnNames.Features); ConsoleHelper.PrintClusteringMetrics(trainer.ToString(), metrics); // STEP5: Save/persist the model as a .ZIP file using (var fs = new FileStream(ModelPath, FileMode.Create, FileAccess.Write, FileShare.Write)) mlContext.Model.Save(trainedModel, fs); 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, }; using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { ITransformer model = mlContext.Model.Load(stream); // Create prediction engine related to the loaded trained model var predEngine = model.CreatePredictionEngine <IrisData, IrisPrediction>(mlContext); //Score var resultprediction = predEngine.Predict(sampleIrisData); Console.WriteLine($"Cluster assigned for setosa flowers:" + resultprediction.SelectedClusterId); } Console.WriteLine("=============== End of process, hit any key to finish ==============="); Console.ReadKey(); }