I am a beginner trying to learn Machine Learning using C#, and I asked ChatGPT to provide me with an example that I can study.
It gave me this code:
using System;
using System.Collections.Generic;
using Microsoft.ML;
using Microsoft.ML.Data;
class Program
{
static void Main(string[] args)
{
// Sample data for demonstration
List<IrisData> trainingData = new List<IrisData>
{
new IrisData { SepalLength = 5.1f, SepalWidth = 3.5f, PetalLength = 1.4f, PetalWidth = 0.2f, Label = "Setosa" },
new IrisData { SepalLength = 4.9f, SepalWidth = 3.0f, PetalLength = 1.4f, PetalWidth = 0.2f, Label = "Setosa" },
// Add more data points...
};
// Create ML context
MLContext mlContext = new MLContext();
// Convert training data to IDataView
IDataView dataView = mlContext.Data.LoadFromEnumerable(trainingData);
// Define data preprocessing pipeline
var pipeline = mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
.Append(mlContext.Transforms.Conversion.MapValueToKey("Label"))
.Append(mlContext.Transforms.NormalizeMinMax("Features"));
// Train the model
var trainedModel = pipeline.Fit(dataView);
// Create a prediction engine
var predictionEngine = mlContext.Model.CreatePredictionEngine<IrisData, IrisPrediction>(trainedModel);
// Sample input for prediction
var testIris = new IrisData { SepalLength = 5.1f, SepalWidth = 3.5f, PetalLength = 1.4f, PetalWidth = 0.2f };
// Make a prediction
var prediction = predictionEngine.Predict(testIris);
// Output predicted label
Console.WriteLine($"Predicted flower type: {prediction.PredictedLabel}");
Console.Read();
}
}
// Define data classes
public class IrisData
{
[LoadColumn(0)]
public float SepalLength;
[LoadColumn(1)]
public float SepalWidth;
[LoadColumn(2)]
public float PetalLength;
[LoadColumn(3)]
public float PetalWidth;
[LoadColumn(4)]
public string Label { get; set; }
}
public class IrisPrediction
{
[ColumnName("PredictedLabel")]
public string PredictedLabel { get; set; }
}
No errors or warnings. I still don’t know why it only displays the string “Predicted flower type:” every time I run it.
I don’t know if this matters, but I provided a getter and a setter for the Label and PredictedLabel and still the PredictedLabel doesn’t display