private double trainingDecision(DecisionTree decision)
        {
            double best  = 0;
            int    index = 8;

            for (int i = 2; i <= 9; i++)
            {
                decision.depthLimit = i;
                decision.training(trainingSet);
                double aux = Math.Round(decision.testing(testingSet), 2);
                if (aux > best)
                {
                    best  = aux;
                    index = i;
                }
                advance++;
            }
            decision.depthLimit = index;
            decision.training(trainingSet);
            double result = Math.Round(decision.testing(testingSet), 2);

            advance++;
            return(result);
        }
Exemplo n.º 2
0
        private void defineMessage(Node node, int number, DecisionTree decisionTreeParent)
        {
            string value = "";

            switch (node.parent.value)
            {
            case 1:
                if (number == 0)
                {
                    value = "Age < 30";
                }
                else if (number == 1)
                {
                    value = "Age < 60";
                }
                else
                {
                    value = "Age >= 60";
                }
                break;

            case 2:
                if (number == 0)
                {
                    value = "F";
                }
                else
                {
                    value = "M";
                }
                break;

            case 3:
                value = "Type " + number;
                break;

            case 4:
                if (number == 0)
                {
                    value = "Blood pressure < " + decisionTreeParent.splitBloodPressure;
                }
                else
                {
                    value = "Blood Pressure >= " + decisionTreeParent.splitBloodPressure;
                }
                break;

            case 5:
                if (number == 0)
                {
                    value = "Cholesterol < " + decisionTreeParent.splitCholesterol;
                }
                else
                {
                    value = "Cholesterol >= " + decisionTreeParent.splitCholesterol;
                }
                break;

            case 6:
                if (number == 0)
                {
                    value = "Level sugar < 120 mg/dl";
                }
                else
                {
                    value = "Level sugar >= 120 mg/dl";
                }
                break;

            case 7:
                if (number == 0)
                {
                    value = "NO";
                }
                else
                {
                    value = "YES";
                }
                break;

            case 8:
                value = "Value " + number;
                break;

            case 9:
                if (number == 0)
                {
                    value = "Heart rate max < " + decisionTreeParent.splitHeartRate;
                }
                else
                {
                    value = "Heart rate max >= " + decisionTreeParent.splitHeartRate;
                }
                break;
            }
            node.message = value;
        }
Exemplo n.º 3
0
        private void training()
        {
            bool haveAtributes = false;

            for (int i = 1; i <= 9 && haveAtributes == false && trainingSet.Count > 0 && depth <= depthLimit; i++)
            {
                if (trainingSet.ElementAt(0).get(i) != -1)
                {
                    haveAtributes = true;
                }
            }

            if (haveAtributes)
            {
                mainEntropy = entropy();
                if (mainEntropy == 0)
                {
                    asnswer = trainingSet.ElementAt(0).result;
                }
                else
                {
                    double[] gain  = new double[9];
                    double   max   = -1;
                    int      index = 0;
                    for (int i = 1; i <= 9; i++)
                    {
                        if (trainingSet.ElementAt(0).get(i) != -1)
                        {
                            if (!isCategoric(i))
                            {
                                splitContinuValues(i);
                            }
                            gain[i - 1] = mainEntropy - entropy(i);
                            if (max < gain[i - 1])
                            {
                                index = i;
                                max   = gain[i - 1];
                            }
                        }
                    }
                    column = index;
                    int aux = 2;
                    if (column == 1 || column == 8)//variables de 3 valores
                    {
                        aux = 3;
                    }
                    else if (column == 3)
                    {
                        aux = 4;
                    }
                    childrenNode = new DecisionTree[aux];
                    List <Patient>[] all = new List <Patient> [aux];
                    for (int i = 0; i < all.Length; i++)
                    {
                        all[i] = new List <Patient>();
                    }
                    List <Patient> newPatients = new List <Patient>();
                    foreach (Patient patient in trainingSet)
                    {
                        int    value = patient.get(column);
                        double safe  = 0;
                        if (!isCategoric(column))
                        {
                            if (column == 4)
                            {
                                safe = splitBloodPressure;
                            }
                            else if (column == 5)
                            {
                                safe = splitCholesterol;
                            }
                            else if (column == 9)
                            {
                                safe = splitHeartRate;
                            }

                            if (patient.get(column) < safe)
                            {
                                value = 0;
                            }
                            else
                            {
                                value = 1;
                            }
                        }
                        if (column == 1)
                        {
                            if (value < 30)//Young
                            {
                                value = 0;
                            }
                            else if (value < 60)//Adult
                            {
                                value = 1;
                            }
                            else//Elder
                            {
                                value = 2;
                            }
                        }
                        int[] values = new int[11];
                        for (int j = 0; j < 11; j++)
                        {
                            values[j] = patient.get(j);
                        }

                        values[column] = -1;
                        Patient newPatient = new Patient(values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9], values[10]);
                        all[value].Add(newPatient);
                    }
                    if (column == 2)
                    {
                    }
                    for (int i = 0; i < childrenNode.Length; i++)
                    {
                        DecisionTree child = new DecisionTree();
                        child.depth      = depth + 1;
                        child.depthLimit = depthLimit;
                        child.training(all[i]);
                        childrenNode[i] = child;
                    }
                }
            }
            else
            {
                double ammountYes = 0;
                double ammountNo  = 0;
                foreach (Patient patient in trainingSet)
                {
                    if (patient.result == 0)
                    {
                        ammountNo++;
                    }
                    else
                    {
                        ammountYes++;
                    }
                }
                if (ammountYes / (ammountYes + ammountNo) > 0.54)
                {
                    asnswer = 1;
                }
                else
                {
                    asnswer = 0;
                }
            }
        }