Naive Bayes Classifier in C# I took the code from internet and tried to simplify it. So, what can I do?

How can I simplify my code more to make it easily understandable?

public class NaiveBayesAlgorithm
{
    private static List&lt;Training&gt; trainingDataSet = new List&lt;Training&gt;();
    private static Dictionary&lt;string, HeightWeightSize&gt; gaussianDataSet = new Dictionary&lt;string, HeightWeightSize&gt;();
    private static Dictionary&lt;string, Gaussian&gt; gaussianList = new Dictionary&lt;string, Gaussian&gt;();

    //takes a table
    public static void Train(List&lt;Training&gt; trainingData)
    {
        //adding the training data to the dataset
        trainingDataSet = trainingData;

        foreach(Training t in trainingDataSet)
        {
            if (!gaussianDataSet.ContainsKey(t.Class))
            {
                gaussianDataSet.Add(t.Class, new HeightWeightSize());
            }

            gaussianDataSet[t.Class].Height.Add(t.Height);
            gaussianDataSet[t.Class].Weight.Add(t.Weight);
            gaussianDataSet[t.Class].FootSize.Add(t.FootSize);
        }

        foreach(string key in gaussianDataSet.Keys)
        {
            Gaussian gaussian = new Gaussian();
            gaussian.Sex = key;
            gaussian.Count = gaussianDataSet[key].Height.Count;

            gaussian.HeightMean = Statistics.Mean(gaussianDataSet[key].Height);
            gaussian.WeightMean = Statistics.Mean(gaussianDataSet[key].Weight);
            gaussian.FootSizeMean = Statistics.Mean(gaussianDataSet[key].FootSize);

            gaussian.HeightVariance = Statistics.Variance(gaussianDataSet[key].Height);
            gaussian.WeightVariance = Statistics.Variance(gaussianDataSet[key].Weight);
            gaussian.FootSizeVariance = Statistics.Variance(gaussianDataSet[key].FootSize);

            gaussianList.Add(key, gaussian);
        }

        string str2 = string.Empty;
    }

    public static string Classify(double[] obj)
    {
        double maxScore = 0;
        string className = string.Empty;

        foreach( string key in gaussianList.Keys)
        {
            gaussianList[key].HeightNormalDist = Statistics.NormalDist(obj[0], gaussianList[key].HeightMean, gaussianList[key].HeightVariance);
            gaussianList[key].WeightNormalDist = Statistics.NormalDist(obj[1], gaussianList[key].WeightMean, gaussianList[key].WeightVariance);
            gaussianList[key].FootSizeNormalDist = Statistics.NormalDist(obj[2], gaussianList[key].FootSizeMean, gaussianList[key].FootSizeVariance);

            gaussianList[key].CombinedNormalDist = gaussianList[key].HeightNormalDist * gaussianList[key].WeightNormalDist * gaussianList[key].FootSizeNormalDist;
            gaussianList[key].UltimateScore = gaussianList[key].CombinedNormalDist * 0.5;

            if(maxScore &lt; gaussianList[key].UltimateScore)
            {
                maxScore = gaussianList[key].UltimateScore;
                className = key;
            }
        }

        return className;
    }
}
HeightWeightSize



This doesn't make a good class name. What if you add another property? You'd need to change its name again.

I guess Person or something alike would be much better.




Gaussian gaussian = new Gaussian();
gaussian.Sex = key;
gaussian.Count = gaussianDataSet[key].Height.Count;

gaussian.HeightMean = Statistics.Mean(gaussianDataSet[key].Height);
gaussian.WeightMean = Statistics.Mean(gaussianDataSet[key].Weight);
gaussian.FootSizeMean = Statistics.Mean(gaussianDataSet[key].FootSize);

gaussian.HeightVariance = Statistics.Variance(gaussianDataSet[key].Height);
gaussian.WeightVariance = Statistics.Variance(gaussianDataSet[key].Weight);
gaussian.FootSizeVariance = Statistics.Variance(gaussianDataSet[key].FootSize);



You could simplify this by putting it inside the Gaussian class.

class Gaussian 
{
    public Gaussian(string sex, int count, double height, double weight, double footSize) 
    {
        HeightMean = Statistics.Mean(Height);
        // ...
    }

    public double HeightMean { get; }
    // ...
}


Adding it to a dictionary would then can be reduced to:

gaussianList.Add(key, new Gaussian(
    sex, 
    gaussianDataSet[key].Count
    gaussianDataSet[key].Height, 
    ...,
    ...)
);




There is still too much redundancy and you could improve more then the above but I don't know how the HeightWeightSize type looks like.