コード例 #1
0
        public static double CalculateEntropy(this Dataset dataset, int attributeIndex)
        {
            double entopy = 0;

            if (dataset.Metadata.Attributes[attributeIndex] is NominalAttribute)
            {
                for (int classIndex = 0; classIndex < dataset.Metadata.Target.Values.Length; classIndex++)
                {
                    double currentEntropy = 0;

                    for (int valueIndex = 0; valueIndex < ((NominalAttribute)dataset.Metadata.Attributes[attributeIndex]).Values.Length; valueIndex++)
                    {
                        int freq = dataset.Filter(attributeIndex, valueIndex, classIndex).Count;
                        if (freq != 0)
                        {
                            double prob = (double)freq / (double)dataset.Size;
                            currentEntropy -= prob * Math.Log(prob);
                        }
                    }

                    double classProb = (double)dataset.Metadata.Target.ValueCounts[classIndex] / (double)dataset.Size;
                    entopy += currentEntropy * classProb;
                }
            }
            else
            {
                double stdv = dataset.CalculateStdValue(attributeIndex);

                //ln(sigma*Sqrt(2*Pi*e))

                double differentialEntropy = Math.Log(stdv * Math.Sqrt(2 * Math.PI * Math.E), Math.E);

                entopy = differentialEntropy;
            }


            return(entopy);
        }