Skip to content

Commit 2eb2337

Browse files
Baseline for Fraud Detection in Credit Card transactions
1 parent 3c1f82e commit 2eb2337

File tree

16 files changed

+1133
-0
lines changed

16 files changed

+1133
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Remove="Assets\**" />
9+
<EmbeddedResource Remove="Assets\**" />
10+
<None Remove="Assets\**" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.ML" Version="$(MicrosoftMLVersion)" />
15+
</ItemGroup>
16+
17+
18+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using AnomalyDetection.Common.DataModels;
2+
using Microsoft.ML.Runtime.Api;
3+
using Microsoft.ML.Runtime.Data;
4+
using System;
5+
using System.IO;
6+
using System.IO.Compression;
7+
using System.Linq;
8+
9+
namespace AnomalyDetection.Common
10+
{
11+
public static class ConsoleHelpers
12+
{
13+
public static void ConsoleWriteHeader(params string[] lines)
14+
{
15+
var defaultColor = Console.ForegroundColor;
16+
Console.ForegroundColor = ConsoleColor.Yellow;
17+
Console.WriteLine(" ");
18+
foreach (var line in lines)
19+
{
20+
Console.WriteLine(line);
21+
}
22+
var maxLength = lines.Select(x => x.Length).Max();
23+
Console.WriteLine(new string('#', maxLength));
24+
Console.ForegroundColor = defaultColor;
25+
}
26+
27+
public static void ConsoleWriterSection(params string[] lines)
28+
{
29+
var defaultColor = Console.ForegroundColor;
30+
Console.ForegroundColor = ConsoleColor.Blue;
31+
Console.WriteLine(" ");
32+
foreach (var line in lines)
33+
{
34+
Console.WriteLine(line);
35+
}
36+
var maxLength = lines.Select(x => x.Length).Max();
37+
Console.WriteLine(new string('-', maxLength));
38+
Console.ForegroundColor = defaultColor;
39+
}
40+
41+
public static void ConsolePressAnyKey()
42+
{
43+
var defaultColor = Console.ForegroundColor;
44+
Console.ForegroundColor = ConsoleColor.Green;
45+
Console.WriteLine(" ");
46+
Console.WriteLine("Press any key to finish.");
47+
Console.ReadKey();
48+
}
49+
50+
public static void ConsoleWriteException(params string[] lines)
51+
{
52+
var defaultColor = Console.ForegroundColor;
53+
Console.ForegroundColor = ConsoleColor.Red;
54+
const string exceptionTitle = "EXCEPTION";
55+
Console.WriteLine(" ");
56+
Console.WriteLine(exceptionTitle);
57+
Console.WriteLine(new string('#', exceptionTitle.Length));
58+
Console.ForegroundColor = defaultColor;
59+
foreach (var line in lines)
60+
{
61+
Console.WriteLine(line);
62+
}
63+
}
64+
65+
public static void ConsoleWriteWarning(params string[] lines)
66+
{
67+
var defaultColor = Console.ForegroundColor;
68+
Console.ForegroundColor = ConsoleColor.DarkMagenta;
69+
const string warningTitle = "WARNING";
70+
Console.WriteLine(" ");
71+
Console.WriteLine(warningTitle);
72+
Console.WriteLine(new string('#', warningTitle.Length));
73+
Console.ForegroundColor = defaultColor;
74+
foreach (var line in lines)
75+
{
76+
Console.WriteLine(line);
77+
}
78+
}
79+
80+
public static string GetAssetsPath(params string[] paths)
81+
{
82+
83+
FileInfo _dataRoot = new FileInfo(typeof(ConsoleHelpers).Assembly.Location);
84+
if (paths == null || paths.Length == 0)
85+
return null;
86+
87+
return Path.Combine(paths.Prepend(_dataRoot.Directory.FullName).ToArray());
88+
}
89+
90+
public static string DeleteAssets(params string[] paths)
91+
{
92+
var location = GetAssetsPath(paths);
93+
94+
if (!string.IsNullOrWhiteSpace(location) && File.Exists(location))
95+
File.Delete(location);
96+
return location;
97+
}
98+
99+
public static void InspectData(LocalEnvironment env, IDataView data)
100+
{
101+
// lets inspect data
102+
//ConsoleWriteHeader("Show 4");
103+
ShowVectorModel(env, data, label: true);
104+
ShowVectorModel(env, data, label: false);
105+
}
106+
107+
public static void InspectScoredData(LocalEnvironment env, IDataView data)
108+
{
109+
// lets inspect data
110+
//ConsoleWriteHeader("Show 4");
111+
ShowEstimatorModel(env, data, label: true);
112+
ShowEstimatorModel(env, data, label: false);
113+
}
114+
115+
public static void ShowVectorModel(LocalEnvironment env, IDataView data, bool label = true, int count = 2)
116+
{
117+
data
118+
// Convert to an enumerable of user-defined type.
119+
.AsEnumerable<TransactionVectorModel>(env, reuseRowObject: false)
120+
.Where(x => x.Label == label)
121+
// Take a couple values as an array.
122+
.Take(count)
123+
.ToList()
124+
// print to console
125+
.ForEach(row => { row.PrintToConsole(); });
126+
}
127+
128+
public static void ShowEstimatorModel(LocalEnvironment env, IDataView data, bool label = true, int count = 2)
129+
{
130+
data
131+
// Convert to an enumerable of user-defined type.
132+
.AsEnumerable<TransactionEstimatorModel>(env, reuseRowObject: false)
133+
.Where(x => x.Label == label)
134+
// Take a couple values as an array.
135+
.Take(count)
136+
.ToList()
137+
// print to console
138+
.ForEach(row => { row.PrintToConsole(); });
139+
}
140+
141+
public static void UnZipDataSet(string zipDataSet, string destinationFile)
142+
{
143+
if (!File.Exists(destinationFile))
144+
{
145+
var destinationDirectory = Path.GetDirectoryName(destinationFile);
146+
ZipFile.ExtractToDirectory(zipDataSet, $"{destinationDirectory}");
147+
}
148+
}
149+
}
150+
151+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using Microsoft.ML.Runtime.Api;
2+
using System;
3+
using System.Collections.Generic;
4+
using static Microsoft.ML.Runtime.Data.RoleMappedSchema;
5+
6+
namespace AnomalyDetection.Common.DataModels
7+
{
8+
9+
public interface IModelEntity {
10+
void PrintToConsole();
11+
}
12+
13+
public class TransactionVectorModel : IModelEntity
14+
{
15+
public bool Label;
16+
public float V1;
17+
public float V2;
18+
public float V3;
19+
public float V4;
20+
public float V5;
21+
public float V6;
22+
public float V7;
23+
public float V8;
24+
public float V9;
25+
public float V10;
26+
public float V11;
27+
public float V12;
28+
public float V13;
29+
public float V14;
30+
public float V15;
31+
public float V16;
32+
public float V17;
33+
public float V18;
34+
public float V19;
35+
public float V20;
36+
public float V21;
37+
public float V22;
38+
public float V23;
39+
public float V24;
40+
public float V25;
41+
public float V26;
42+
public float V27;
43+
public float V28;
44+
public float Amount;
45+
46+
public void PrintToConsole() {
47+
Console.WriteLine($"Label: {Label}");
48+
Console.WriteLine($"Features: [V1] {V1} [V2] {V2} [V3] {V3} ... [V28] {V28} Amount: {Amount}");
49+
}
50+
51+
public static List<KeyValuePair<ColumnRole, string>> Roles() {
52+
return new List<KeyValuePair<ColumnRole, string>>() {
53+
new KeyValuePair<ColumnRole, string>(ColumnRole.Label, "Label"),
54+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V1"),
55+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V2"),
56+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V3"),
57+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V4"),
58+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V5"),
59+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V6"),
60+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V7"),
61+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V8"),
62+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V9"),
63+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V10"),
64+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V11"),
65+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V12"),
66+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V13"),
67+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V14"),
68+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V15"),
69+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V16"),
70+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V17"),
71+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V18"),
72+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V19"),
73+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V20"),
74+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V21"),
75+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V22"),
76+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V23"),
77+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V24"),
78+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V25"),
79+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V26"),
80+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V27"),
81+
new KeyValuePair<ColumnRole, string>(ColumnRole.Feature, "V28"),
82+
new KeyValuePair<ColumnRole, string>(new ColumnRole("Amount"), ""),
83+
84+
};
85+
}
86+
}
87+
88+
public class TransactionEstimatorModel : IModelEntity
89+
{
90+
public bool Label;
91+
public bool PredictedLabel;
92+
public float Score;
93+
public float Probability;
94+
95+
public void PrintToConsole()
96+
{
97+
Console.WriteLine($"Predicted Label: {PredictedLabel} [{Label}]");
98+
Console.WriteLine($"Probability: {Probability} ({Score})");
99+
}
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using Microsoft.ML.Core.Data;
2+
using Microsoft.ML.Runtime.Data;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
7+
namespace AnomalyDetection.Common
8+
{
9+
public static class ConsoleExtensions
10+
{
11+
12+
public static void ToConsole(this MultiClassClassifierEvaluator.Result result)
13+
{
14+
Console.WriteLine($"Acuracy macro: {result.AccuracyMacro}");
15+
Console.WriteLine($"Acuracy micro: {result.AccuracyMicro}");
16+
Console.WriteLine($"Log loss: {result.LogLoss}");
17+
Console.WriteLine($"Log loss reduction: {result.LogLossReduction}");
18+
Console.WriteLine($"Per class log loss: ");
19+
int count = 0;
20+
foreach (var logLossClass in result.PerClassLogLoss)
21+
{
22+
Console.WriteLine($"\t [{count++}]: {logLossClass}");
23+
}
24+
Console.WriteLine($"Top K: {result.TopK}");
25+
Console.WriteLine($"Top K accuracy: {result.TopKAccuracy}");
26+
27+
}
28+
29+
public static void ToConsole(this BinaryClassifierEvaluator.CalibratedResult result)
30+
{
31+
Console.WriteLine($"Area under ROC curve: {result.Auc}");
32+
Console.WriteLine($"Area under the precision/recall curve: {result.Auprc}");
33+
Console.WriteLine($"Entropy: {result.Entropy}");
34+
Console.WriteLine($"F1 Score: {result.F1Score}");
35+
Console.WriteLine($"Log loss: {result.LogLoss}");
36+
Console.WriteLine($"Log loss reduction: {result.LogLossReduction}");
37+
Console.WriteLine($"Negative precision: {result.NegativePrecision}");
38+
Console.WriteLine($"Positive precision: {result.PositivePrecision}");
39+
Console.WriteLine($"Positive recall: {result.PositiveRecall}");
40+
41+
}
42+
43+
public static void ToConsole(this RegressionEvaluator.Result result)
44+
{
45+
Console.WriteLine($"L1: {result.L1}");
46+
Console.WriteLine($"L2: {result.L2}");
47+
Console.WriteLine($"Loss function: {result.LossFn}");
48+
Console.WriteLine($"Root mean square of the L2 loss: {result.Rms}");
49+
Console.WriteLine($"R scuared: {result.RSquared}");
50+
}
51+
52+
public static IEnumerable<string> GetColumnNames(this ISchema schema)
53+
{
54+
for (int i = 0; i < schema.ColumnCount; i++)
55+
{
56+
if (!schema.IsHidden(i))
57+
yield return schema.GetColumnName(i);
58+
}
59+
}
60+
61+
public static void SaveModel(this ITransformer model, LocalEnvironment env, string modelSavePath)
62+
{
63+
using (var stream = File.Create(modelSavePath))
64+
{
65+
// Saving and loading happens to 'dynamic' models, so the static typing is lost in the process.
66+
model.SaveTo(env, stream);
67+
}
68+
}
69+
70+
public static ITransformer ReadModel(this LocalEnvironment env, string modelLocation)
71+
{
72+
ITransformer model;
73+
using (var file = File.OpenRead(@modelLocation))
74+
{
75+
model = TransformerChain.LoadFrom(env, file);
76+
}
77+
return model;
78+
}
79+
}
80+
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
<LangVersion>7.2</LangVersion>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<None Remove="assets\input\.gitignore" />
11+
<None Remove="assets\output\.gitignore" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.ML" Version="$(MicrosoftMLVersion)" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\AnomalyDetection.Common\AnomalyDetection.Common.csproj" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<Folder Include="assets\input\" />
24+
<Folder Include="assets\output\" />
25+
</ItemGroup>
26+
27+
</Project>

0 commit comments

Comments
 (0)