Exemplo n.º 1
0
        static string SelectParameter(InstanceSet instances)
        {
            Console.WriteLine();
            Console.WriteLine($"Selecting parameter for\r\n{instances}");

            Console.WriteLine($"Gini(E) = {Math.Round(Gini(instances), 2)}");

            var    parameters = instances.First().Parameters.Values.OrderBy(p => p.Name);
            string result     = null;
            double max        = Double.MinValue;

            foreach (var parameter in parameters)
            {
                var gain = GiniGain(instances, parameter.Name);
                Console.WriteLine($"GiniGain({parameter.Name}) = {Math.Round(gain, 2)} ");
                if (gain > max)
                {
                    max    = gain;
                    result = parameter.Name;
                }
            }
            Console.WriteLine($"Selected {result}");

            return(result);
        }
Exemplo n.º 2
0
        static double GiniGain(InstanceSet instances, string parameterName)
        {
            Console.WriteLine($"    Gini(E, {parameterName}) = {Math.Round(Gini(instances, parameterName), 2)}");
            Console.WriteLine("    " + String.Join("; ", Enums.Items <Value>().Select(v => $"P({parameterName}={v}) = " + P(parameterName, v, instances) + $" Gini(E{parameterName}={v}) = " +
                                                                                      Math.Round(Gini(instances.Where(inst => inst.Parameters[parameterName].Value == v).ToInstanceSet()), 2))));

            return(Gini(instances) - Gini(instances, parameterName));
        }
Exemplo n.º 3
0
 public Node(InstanceSet trainingInstances, Parameter?parameter = null)
 {
     TrainingInstances = trainingInstances;
     Parameter         = parameter;
 }
Exemplo n.º 4
0
 static double Gini(InstanceSet instances, string parameterName)
 {
     return(Enums.Items <Value>().Select(v => P(parameterName, v, instances) * Gini(instances.Where(inst => inst.Parameters[parameterName].Value == v).ToInstanceSet())).Sum());
 }
Exemplo n.º 5
0
 static double Gini(InstanceSet instances)
 {
     return(1 - Enums.Items <Class>().Select(cls => Math.Pow(P(cls, instances), 2)).Sum());
 }
Exemplo n.º 6
0
 static double P(string parameterName, Value value, InstanceSet instances)
 {
     return((double)instances.Count(inst => inst.Parameters[parameterName].Value == value) / instances.Count);
 }
Exemplo n.º 7
0
 static double P(Class cls, InstanceSet instances)
 {
     return(!instances.Any() ? 0 : (double)instances.Count(inst => inst.Class == cls) / instances.Count);
 }