0
0

Add Evaluate method

This commit is contained in:
Serghei Cebotari 2024-01-12 00:00:57 +03:00
parent 327251d5e8
commit ac86f7ff28

View File

@ -1,21 +1,31 @@
using Microsoft.ML; using Microsoft.ML;
using Microsoft.ML.Data;
namespace RhSolutions.ML.Lib; namespace RhSolutions.ML.Lib;
public class RhSolutionsMLBuilder public class RhSolutionsMLBuilder
{ {
private static string _appPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) ?? "."; private static string _appPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) ?? ".";
private static string _testDataPath = Path.Combine(_appPath, "..", "..", "..", "..", "TestData", "*");
private static MLContext _mlContext = new MLContext(seed: 0); private static MLContext _mlContext = new MLContext(seed: 0);
private static ITransformer? _trainedModel = null;
public static void RebuildModel() public static void RebuildModel()
{ {
var _trainDataView = _mlContext.Data.LoadFromTextFile<Product>( var _trainDataView = _mlContext.Data.LoadFromTextFile<Product>(
Path.Combine(_appPath, "..", "..", "..", "..", "TrainData", "*"), hasHeader: false); Path.Combine(_appPath, "..", "..", "..", "..", "TrainData", "*"), hasHeader: false);
var pipeline = ProcessData(); var pipeline = ProcessData();
BuildAndTrainModel(_trainDataView, pipeline, out ITransformer trainedModel); BuildAndTrainModel(_trainDataView, pipeline, out ITransformer _trainedModel);
SaveModelAsFile(_mlContext, _trainDataView.Schema, trainedModel); SaveModelAsFile(_mlContext, _trainDataView.Schema, _trainedModel);
} }
public static MulticlassClassificationMetrics? EvaluateModel()
{
var testDataView = _mlContext.Data.LoadFromTextFile<Product>(_testDataPath, hasHeader: false, separatorChar: ';');
return _mlContext.MulticlassClassification.Evaluate(_trainedModel?.Transform(testDataView));
}
private static IEstimator<ITransformer> ProcessData() private static IEstimator<ITransformer> ProcessData()
{ {
var pipeline = _mlContext.Transforms.Conversion.MapValueToKey(inputColumnName: "Type", outputColumnName: "Label") var pipeline = _mlContext.Transforms.Conversion.MapValueToKey(inputColumnName: "Type", outputColumnName: "Label")