Data science / Machine learning is all about statistics/probability, graphs and linear algebra.
Here are few basics stats functions in c# :
static decimal GetMean(decimal[] inputs)
{
int count = inputs.Length;
decimal avg = 0;
for (int n = 0; n < count; n++)
{
avg += inputs[n];
}
return Math.Round(avg / count, 1);
}
static decimal GetMedian(int[] inputs)
{
Array.Sort(inputs);
int count = 0;
if ((inputs.Length % 2) == 1)
{
count = (inputs.Length - 1) / 2;
return inputs[count];
}
else
{
count = inputs.Length / 2;
decimal a = inputs[count - 1];
decimal b = inputs[count];
return Math.Round((a + b) / 2, 1);
}
}
static decimal GetMode(decimal[] inputs)
{
//you can substitute below C# lambda express to hasttable technique
return inputs.GroupBy(n => n).
OrderByDescending(g => g.Count()).
Select(g => g.Key).FirstOrDefault();
}
static decimal GetWeightedAvg(decimal[] avg, decimal[] weight)
{
int count = avg.Length;
decimal w = 0;
decimal aAndW = 0;
for (int n = 0; n < count; n++)
{
aAndW += avg[n] * weight[n];
w += weight[n];
}
return Math.Round( (aAndW / w), 1);
}
static double GetStandardDeviation(int[] inputs)
{
int count = inputs.Length;
int avg = 0; int sum = 0;
for (int n = 0; n < count; n++)
{
sum += inputs[n];
}
avg = sum / count; //getting avg or mean
int preSd = 0;
for (int n = 0; n < count; n++)
{
int tmp = Math.Abs( ((inputs[n] - avg) )); //replace abs to sub and sub from higher to lower
preSd += tmp * tmp ;
}
return Math.Sqrt( preSd / count);
}
static decimal GetMean(decimal[] inputs)
{
int count = inputs.Length;
decimal avg = 0;
for (int n = 0; n < count; n++)
{
avg += inputs[n];
}
return Math.Round(avg / count, 1);
}
static decimal GetMedian(int[] inputs)
{
Array.Sort(inputs);
int count = 0;
if ((inputs.Length % 2) == 1)
{
count = (inputs.Length - 1) / 2;
return inputs[count];
}
else
{
count = inputs.Length / 2;
decimal a = inputs[count - 1];
decimal b = inputs[count];
return Math.Round((a + b) / 2, 1);
}
}
static decimal GetMode(decimal[] inputs)
{
//you can substitute below C# lambda express to hasttable technique
return inputs.GroupBy(n => n).
OrderByDescending(g => g.Count()).
Select(g => g.Key).FirstOrDefault();
}
static decimal GetWeightedAvg(decimal[] avg, decimal[] weight)
{
int count = avg.Length;
decimal w = 0;
decimal aAndW = 0;
for (int n = 0; n < count; n++)
{
aAndW += avg[n] * weight[n];
w += weight[n];
}
return Math.Round( (aAndW / w), 1);
}
static double GetStandardDeviation(int[] inputs)
{
int count = inputs.Length;
int avg = 0; int sum = 0;
for (int n = 0; n < count; n++)
{
sum += inputs[n];
}
avg = sum / count; //getting avg or mean
int preSd = 0;
for (int n = 0; n < count; n++)
{
int tmp = Math.Abs( ((inputs[n] - avg) )); //replace abs to sub and sub from higher to lower
preSd += tmp * tmp ;
}
return Math.Sqrt( preSd / count);
}