Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto MultiClassification is not Run;Help me me #997

Open
wuzxc1230123 opened this issue Jul 7, 2023 · 1 comment
Open

Auto MultiClassification is not Run;Help me me #997

wuzxc1230123 opened this issue Jul 7, 2023 · 1 comment

Comments

@wuzxc1230123
Copy link

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
Copy link
Contributor

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")));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants