Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MachineLearningInCsharpEngine\MachineLearningInCsharpEngine.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ public class Program
public static void Main()
{
var modelBuilder = new ModelBuilder();
modelBuilder.LoadModel("trainedModel.zip");
modelBuilder.LoadModel(Path.Combine(AppContext.BaseDirectory, "trainedModel.zip"));

var modelInput = new ModelInput()
{
Age = 300,
CreditAmount = 100000,
Duration = 120
Age = 22,
CreditAmount = 15000,
Duration = 60
};

var prediction = modelBuilder?.Predict(modelInput);
Console.WriteLine($"\nExample input class is {prediction?.Prediction.ToUpper()}!");
var prediction = modelBuilder.Predict(modelInput);
Console.WriteLine($"\nExample input class is {prediction.Prediction.ToUpper()}!");

string? inputString;
float inputValue;

Console.WriteLine("\nInput data:");

Console.Write("Age in months:\t\t");
Console.Write("Age in years:\t\t");
inputString = Console.ReadLine();
while (string.IsNullOrEmpty(inputString) || !float.TryParse(inputString, out inputValue))
{
Expand All @@ -51,7 +51,7 @@ public static void Main()
}
modelInput.Duration = inputValue;

prediction = modelBuilder?.Predict(modelInput);
Console.WriteLine($"\nYour credit application class is {prediction?.Prediction.ToUpper()}!");
prediction = modelBuilder.Predict(modelInput);
Console.WriteLine($"\nYour credit application class is {prediction.Prediction.ToUpper()}!");
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace MachineLearningInCsharpEngine.DataModels.MulticlassClassification;
public class ModelBuilder
{
private MLContext _mlContext = new MLContext(seed: 0);
private PredictionEngine<ModelInput, ModelOutput> _predictionEngine;
private IDataView _trainingDataView;
private IDataView _testDataView;
private ITransformer _mlModel;
private PredictionEngine<ModelInput, ModelOutput> _predictionEngine = null!;
private IDataView _trainingDataView = null!;
private IDataView _testDataView = null!;
private ITransformer _mlModel = null!;

public void CreateModel(string dataFilePath, string savingPath)
{
Expand All @@ -27,7 +27,8 @@ public void CreateModel(string dataFilePath, string savingPath)
public void LoadModel(string path)
{
_mlModel = _mlContext.Model.Load(path, out _);
CreatePredictionEngine(); }
CreatePredictionEngine();
}

private void LoadAndSplitData(string dataFilePath)
{
Expand Down Expand Up @@ -69,6 +70,7 @@ public void EvaluateModel()
Console.WriteLine($"- MacroAccuracy:\t{testMetrics.MacroAccuracy:0.###}");
Console.WriteLine($"- LogLoss:\t\t{testMetrics.LogLoss:#.###}");
Console.WriteLine($"- LogLossReduction:\t{testMetrics.LogLossReduction:#.###}");
Console.WriteLine(testMetrics.ConfusionMatrix.GetFormattedConfusionTable());
}

private void SaveModel(string saveModelPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public class ModelInput
public float Age { get; set; }

[ColumnName("class"), LoadColumn(20)]
public string Class { get; set; }
public string Class { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace MachineLearningInCsharpEngine.DataModels.MulticlassClassification;
public class ModelOutput
{
[ColumnName("PredictedLabel")]
public string Prediction { get; set; }
public string Prediction { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.ML" Version="2.0.1" />
<PackageReference Include="Microsoft.ML" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.3" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,62 @@
using MachineLearningInCsharpEngine.DataModels.MulticlassClassification;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using MachineLearningInCsharpEngine.DataModels.MulticlassClassification;

namespace MachineLearningInCsharpTests;

[TestClass]
public class MulticlassClassificationTests
{
private ModelBuilder? _modelBuilder;
private string _savedModelFilename = "multiclassCreditClassificationModel.zip";
private const string SavedModelFilename = "multiclassCreditClassificationModel.zip";
private static ModelBuilder _modelBuilder = null!;

public MulticlassClassificationTests()
[ClassInitialize]
public static void TrainModelOnce(TestContext _)
{
if (_modelBuilder is null)
{
_modelBuilder = new ModelBuilder();
var currentDir = Directory.GetCurrentDirectory();
var parentDir = Directory.GetParent(currentDir)?.Parent?.Parent;
_modelBuilder.CreateModel(Path.Combine(parentDir == null ? string.Empty : parentDir.FullName, @"DataSets/credit_customers.csv"),
Path.Combine(currentDir, _savedModelFilename));
}
_modelBuilder = new ModelBuilder();
_modelBuilder.CreateModel(
Path.Combine(AppContext.BaseDirectory, "DataSets", "credit_customers.csv"),
Path.Combine(AppContext.BaseDirectory, SavedModelFilename));
}

[TestMethod]
public void WhenPredictGoodCreditClass_ThanSuccess()
public void WhenPredictGoodCreditClass_ThenSuccess()
{
var prediction = _modelBuilder?.Predict(new ModelInput()
var prediction = _modelBuilder.Predict(new ModelInput
{
Age = 340,
CreditAmount = 25000,
Duration = 80
Age = 35,
CreditAmount = 2000,
Duration = 12
});

Assert.IsTrue(prediction?.Prediction.Equals("good"));
Assert.AreEqual("good", prediction.Prediction);
}

[TestMethod]
public void WhenPredictBadCreditClass_ThanSuccess()
public void WhenPredictBadCreditClass_ThenSuccess()
{
var prediction = _modelBuilder?.Predict(new ModelInput()
var prediction = _modelBuilder.Predict(new ModelInput
{
Age = 240,
CreditAmount = 45000,
Duration = 480
Age = 22,
CreditAmount = 15000,
Duration = 60
});

Assert.IsTrue(prediction?.Prediction.Equals("bad"));
Assert.AreEqual("bad", prediction.Prediction);
}

[TestMethod]
public void WhenPredictUsingSavedModel_ThanSuccess()
public void WhenPredictUsingSavedModel_ThenSuccess()
{
var loadedModelBuilder = new ModelBuilder();
var currentDir = Directory.GetCurrentDirectory();
loadedModelBuilder.LoadModel(Path.Combine(currentDir, _savedModelFilename));
loadedModelBuilder.LoadModel(Path.Combine(AppContext.BaseDirectory, SavedModelFilename));

var prediction = loadedModelBuilder?.Predict(new ModelInput()
var prediction = loadedModelBuilder.Predict(new ModelInput
{
Age = 340,
CreditAmount = 25000,
Duration = 80
Age = 35,
CreditAmount = 2000,
Duration = 12
});

Assert.IsTrue(prediction?.Prediction.Equals("good"));
Assert.AreEqual("good", prediction.Prediction);
}
}
Loading