-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced ad implemented Ml.Net for prediction purposes
- Loading branch information
1 parent
4995401
commit 2087215
Showing
8 changed files
with
213 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> | ||
</startup> | ||
<runtime> | ||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<dependentAssembly> | ||
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> | ||
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" /> | ||
</dependentAssembly> | ||
</assemblyBinding> | ||
</runtime> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.ML; | ||
using Microsoft.ML.Data; | ||
|
||
// Define a class to hold your data structure | ||
public class InputData | ||
{ | ||
[LoadColumn(0)] | ||
public float Ip1 { get; set; } | ||
|
||
[LoadColumn(1)] | ||
public float Ip2 { get; set; } | ||
|
||
[LoadColumn(2)] | ||
public float Ip3 { get; set; } | ||
|
||
[LoadColumn(3)] | ||
public float Ip4 { get; set; } | ||
|
||
[LoadColumn(4), ColumnName("Label")] | ||
public bool Fraud { get; set; } | ||
} | ||
|
||
// Define a class to hold your prediction | ||
public class OutputData | ||
{ | ||
[ColumnName("PredictedLabel")] | ||
public bool Prediction { get; set; } | ||
} | ||
|
||
public class Predictor | ||
{ | ||
public static string PredictMovement(InputData inputData) | ||
{ | ||
// Set paths for data and model | ||
var dataPath = Path.Combine(Environment.CurrentDirectory, "Data", "input_data.csv"); | ||
var secondDataPath = Path.Combine(Environment.CurrentDirectory, "Data", "second_input_data.csv"); | ||
var modelPath = Path.Combine(Environment.CurrentDirectory, "Model", "trained_model.zip"); | ||
|
||
// Create a ML.NET context | ||
var mlContext = new MLContext(); | ||
|
||
// Load data | ||
var dataView = mlContext.Data.LoadFromTextFile<InputData>( | ||
path: dataPath, | ||
hasHeader: true, | ||
separatorChar: ','); | ||
|
||
// Split data into training and testing sets | ||
var trainTestSplit = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.3); | ||
var trainData = trainTestSplit.TrainSet; | ||
var testData = trainTestSplit.TestSet; | ||
|
||
// Define data preparation pipeline | ||
var dataProcessPipeline = mlContext.Transforms.Concatenate("Features", "Ip1", "Ip2", "Ip3", "Ip4") | ||
.Append(mlContext.Transforms.NormalizeMinMax("Features")); | ||
|
||
// Create a Logistic Regression model | ||
var trainer = mlContext.BinaryClassification.Trainers.LbfgsLogisticRegression(); | ||
var trainingPipeline = dataProcessPipeline.Append(trainer); | ||
|
||
// Train the model | ||
var model = trainingPipeline.Fit(trainData); | ||
|
||
// Save the trained model | ||
mlContext.Model.Save(model, trainData.Schema, modelPath); | ||
|
||
// Reload the trained model | ||
var reloadedModel = mlContext.Model.Load(modelPath, out var modelInputSchema); | ||
|
||
// Load the second input data file (features only) | ||
var secondDataView = mlContext.Data.LoadFromTextFile<InputData>( | ||
path: secondDataPath, | ||
hasHeader: true, | ||
separatorChar: ','); | ||
var input = new InputData(); | ||
// Make predictions on the second input data | ||
var predictions = mlContext.Model.CreatePredictionEngine<InputData, OutputData>(reloadedModel) | ||
.Predict(input); | ||
|
||
// Display predictions | ||
Console.WriteLine("Predictions for the second input data:"); | ||
//foreach (var prediction in predictions) | ||
//{ | ||
Console.WriteLine($"Prediction: {predictions.Prediction}"); | ||
//} | ||
return predictions.Prediction.ToString(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="Microsoft.Bcl.Numerics" version="8.0.0" targetFramework="net48" /> | ||
<package id="Microsoft.ML" version="3.0.1" targetFramework="net48" /> | ||
<package id="Microsoft.ML.CpuMath" version="3.0.1" targetFramework="net48" /> | ||
<package id="Microsoft.ML.DataView" version="3.0.1" targetFramework="net48" /> | ||
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net48" /> | ||
<package id="System.Buffers" version="4.5.1" targetFramework="net48" /> | ||
<package id="System.CodeDom" version="4.5.0" targetFramework="net48" /> | ||
<package id="System.Collections.Immutable" version="1.5.0" targetFramework="net48" /> | ||
<package id="System.Memory" version="4.5.5" targetFramework="net48" /> | ||
<package id="System.Numerics.Tensors" version="8.0.0" targetFramework="net48" /> | ||
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" /> | ||
<package id="System.Reflection.Emit.Lightweight" version="4.3.0" targetFramework="net48" /> | ||
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net48" /> | ||
<package id="System.Threading.Channels" version="4.7.1" targetFramework="net48" /> | ||
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" /> | ||
</packages> |