dotnet / machinelearning-samples

Samples for ML.NET, an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
4.49k stars 2.69k forks source link

Auto MultiClassification is not Run;Help me me #997

Open wuzxc1230123 opened 1 year ago

wuzxc1230123 commented 1 year ago

mycode

using CsvHelper; using Microsoft.ML; using Microsoft.ML.AutoML; using Microsoft.ML.Data; using Microsoft.ML.Trainers.LightGbm; using System.Globalization; using static Microsoft.ML.DataOperationsCatalog;

var list= new List(); for (int i = 1; i < 1000; i++) { list.Add(new TaxiTrip() { A = (float)i, Label ="A" }); } for (int i = 1000; i < 2000; i++) { list.Add(new TaxiTrip() { A = (float)i, Label = "B" }); } using (var writer = new StreamWriter("data.csv")) using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) { csv.WriteRecords(list); }

MLContext ctx = new MLContext();

var dataPath = TrainDataPath;

// Infer column information ColumnInferenceResults columnInference = ctx.Auto().InferColumns(dataPath, labelColumnName: "Label", groupColumns: false);

// Create text loader TextLoader loader = ctx.Data.CreateTextLoader(columnInference.TextLoaderOptions);

// Load data into IDataView IDataView data = loader.Load(dataPath);

// Split into train (80%), validation (20%) sets TrainTestData trainValidationData = ctx.Data.TrainTestSplit(data, testFraction: 0.2);

var context = new MLContext(1);

var experiment = context.Auto().CreateExperiment(); var pipeline = context.Auto().Featurizer(data, columnInformation: columnInference.ColumnInformation) //.Append(context.Transforms.Conversion.MapKeyToValue(label, label)) .Append(context.Transforms.Conversion.MapValueToKey(outputColumnName: @"Label", inputColumnName: @"Label")) .Append(context.Transforms.Conversion.MapKeyToValue(outputColumnName: @"PredictedLabel", inputColumnName: @"PredictedLabel")) .Append(context.Auto().MultiClassification());

experiment.SetDataset(data, 5) .SetMulticlassClassificationMetric(MulticlassClassificationMetric.MacroAccuracy, @"Label") .SetPipeline(pipeline) .SetTrainingTimeInSeconds(60);

var result = await experiment.RunAsync();

var predictionEngine = ctx.Model.CreatePredictionEngine<TaxiTrip, TaxiTripFarePrediction>(result.Model);

var testTaxiTrip = new TaxiTrip { A=888, }; var prediction = predictionEngine.Predict(testTaxiTrip);

var testTaxiTrip2 = new TaxiTrip { A = 1888, }; var prediction2 = predictionEngine.Predict(testTaxiTrip2);

var testTaxiTrip3 = new TaxiTrip { A = 555, }; var prediction3 = predictionEngine.Predict(testTaxiTrip3); //Console.WriteLine(prediction.FareAmount); Console.WriteLine();

public class TaxiTrip { [ColumnName("A")] public float A { get; set; }

[ColumnName("Label")]
public string Label { get; set; }

}

public class TaxiTripFarePrediction {

[ColumnName(@"Label")]
public uint Label { get; set; }

[ColumnName(@"PredictedLabel")]
public string PredictedLabel { get; set; }

}

feiyun0112 commented 1 year ago

The pipelines are in the wrong order

var pipeline = context.Auto().Featurizer(data, columnInformation: columnInference.ColumnInformation)
//.Append(context.Transforms.Conversion.MapKeyToValue(label, label))
.Append(context.Transforms.Conversion.MapValueToKey(outputColumnName: @"Label", inputColumnName: @"Label"))

.Append(context.Auto().MultiClassification()
.Append(context.Transforms.Conversion.MapKeyToValue(outputColumnName: @"PredictedLabel", inputColumnName: @"PredictedLabel")));