Skip to content

Commit

Permalink
1.14.28.0: Add code for computing beta metric
Browse files Browse the repository at this point in the history
  • Loading branch information
hafniz committed Feb 27, 2020
1 parent de5f17e commit 9d75d4f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
19 changes: 19 additions & 0 deletions MLCore/Algorithm/KNNContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,24 @@ public double GetAlphaValue(Instance testingInstance)
}
return alphas;
}

public double GetBetaValue(Instance testingInstance)
{
int homoCount = TrainingInstances.Count(i => i.LabelValue == testingInstance.LabelValue);
IEnumerable<Instance> neighbors = GetNeighbors(testingInstance, homoCount - 1);
double c = neighbors.Where(i => i.LabelValue == testingInstance.LabelValue).Sum(i => 1.0 / EuclideanDistance(i, testingInstance));
double d = TrainingInstances.Where(i => i != testingInstance).Sum(i => 1.0 / EuclideanDistance(i, testingInstance));
return c / d;
}

public List<(Instance, double)> GetAllBetaValues()
{
List<(Instance, double)> betas = new List<(Instance, double)>();
foreach (Instance trainingInstance in TrainingInstances)
{
betas.Add((trainingInstance, GetBetaValue(trainingInstance)));
}
return betas;
}
}
}
2 changes: 1 addition & 1 deletion MLCore/MLCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Nullable>enable</Nullable>
<StartupObject>MLCore.Program</StartupObject>
<ApplicationIcon>TextTemplate.ico</ApplicationIcon>
<Version>1.14.26.0</Version>
<Version>1.14.28.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
33 changes: 21 additions & 12 deletions MLCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#define NOOP
#define BETA_EXPR
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MLCore.Algorithm;

namespace MLCore
Expand Down Expand Up @@ -519,19 +519,28 @@ static void Main()
#endif
#endregion

static void Main()
#region BETA_EXPR
#if BETA_EXPR
public static int finishedCount = 0;

public static void Main()
{
Parallel.ForEach(Directory.EnumerateFiles("C:\\Users\\CHENH\\source\\repos\\MachineLearning\\Dataset\\A270\\original\\a270-raw"), filename => CalcBeta(filename));
Parallel.ForEach(Directory.EnumerateFiles("C:\\Users\\CHENH\\source\\repos\\MachineLearning\\Dataset\\UCI\\ECOC8030\\ECOC8030-raw"), filename => CalcBeta(filename));
}

public static void CalcBeta(string filename)
{
int i = 1;
StringBuilder sb = new StringBuilder("filename,majority%\r\n");
foreach (string filename in Directory.EnumerateFiles("C:\\Users\\CHENH\\source\\repos\\MachineLearning\\Dataset\\UCI\\ECOC8030\\ECOC8030-raw"))
List<Instance> instances = CSV.ReadFromCsv(filename, null);
StringBuilder sb = new StringBuilder($"{string.Join(',', instances.First().Features.Select(f => f.Name))},label,beta\r\n");
foreach ((Instance instance, double beta) in new KNNContext(instances).GetAllBetaValues())
{
List<string> labels = CSV.ReadFromCsv(filename, true).SelectColumn(^1);
int majorityCount = Math.Max(labels.Count(s => s == "0.0"), labels.Count(s => s == "1.0"));
sb.AppendLine($"{Path.GetFileNameWithoutExtension(filename)},{ majorityCount / (double)labels.Count}");
Console.WriteLine(i++);
sb.AppendLine($"{instance.Serialize()},{beta}");
}
using StreamWriter sw = new StreamWriter("C:\\Users\\CHENH\\Desktop\\CBstats.csv");
sw.Write(sb);
File.WriteAllText($"C:\\Users\\CHENH\\Desktop\\beta\\{Path.GetFileName(filename)}", sb.ToString());
Console.WriteLine(++finishedCount);
}
#endif
#endregion
}
}

0 comments on commit 9d75d4f

Please sign in to comment.