// Use this for initialization
    IEnumerator LoadFromDb()
    {
        using (SqliteConnection conn = new SqliteConnection(GetConnectionString()))
        {
            conn.Open();
            SqliteCommand command = new SqliteCommand(conn);
            id3 = new DecisionTreeID3();
            int iBatch = 0;
            foreach (var info in decisionTrees)
            {
                string query = string.Format("SELECT * FROM {0}", info.tableName);
                command.CommandText = query;
                foreach (var split in info.outcomes)
                {
                    var reader = command.ExecuteReader();

                    DataTable table = new DataTable(info.tableName);
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        if (reader.GetName(i) == info.predictColumn)
                        {
                            table.Columns.Add(reader.GetName(i), typeof(bool));
                        }
                        else
                        {
                            table.Columns.Add(reader.GetName(i), reader.GetFieldType(i));
                        }
                    }

                    while (reader.Read())
                    {
                        var dr = table.NewRow();
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            if (table.Columns[i].ColumnName == info.predictColumn)
                            {
                                if (!reader.IsDBNull(i))
                                {
                                    switch (split.type)
                                    {
                                    case DecisionTreeOutcome.OutcomeType.Int:
                                        dr[i] = (reader.GetInt32(i) == split.value);
                                        break;

                                    case DecisionTreeOutcome.OutcomeType.Text:
                                        dr[i] = (reader.GetString(i) == split.textValue);
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                dr[i] = reader[i];
                            }
                        }
                        table.Rows.Add(dr);
                        if (++iBatch % batchSize == 0)
                        {
                            yield return(new WaitForEndOfFrame());
                        }
                    }

                    table.AcceptChanges();
                    split.root = id3.MountTree(table, info.predictColumn, info.GetAttributes());
                    Hawk.Log(string.Format("DECISION TREE {0}{1}\n{2}", info.decisionTreeName, split.name, split.root.PrintNode("--->")));

                    foreach (var obj in eventListeners)
                    {
                        obj.SendMessage("OnDecisionTreeReady", split.root);
                    }

                    reader.Close();
                }
            }
        }
    }
Пример #2
0
    private TreeNode InternalMountTree(DataTable samples, String targetCustomAttribute, CustomAttribute[] attributes)
    {
        if (AllSamplesPositives(samples, targetCustomAttribute) == true)
        {
            return(new TreeNode(new CustomAttribute(true)));
        }

        if (AllSamplesNegatives(samples, targetCustomAttribute) == true)
        {
            return(new TreeNode(new CustomAttribute(false)));
        }

        if (attributes.Length == 0)
        {
            return(new TreeNode(new CustomAttribute(GetMostCommonValue(samples, targetCustomAttribute))));
        }

        mTotal = samples.Rows.Count;
        mTargetCustomAttribute = targetCustomAttribute;
        mTotalPositives        = CountTotalPositives(samples);
        mEntropySet            = CalcEntropy(mTotalPositives, mTotal - mTotalPositives);

        CustomAttribute bestCustomAttribute = GetBestCustomAttribute(samples, attributes);

        if (bestCustomAttribute == null)
        {
            return(new TreeNode(new CustomAttribute(false)));
        }

        TreeNode root = new TreeNode(bestCustomAttribute);

        DataTable aSample = samples.Clone();

        foreach (String value in bestCustomAttribute.values)
        {
            aSample.Rows.Clear();

            DataRow[] rows = samples.Select(bestCustomAttribute.CustomAttributeName + " = " + "'" + value + "'");

            foreach (DataRow row in rows)
            {
                aSample.Rows.Add(row.ItemArray);
            }

            ArrayList aCustomAttributes = new ArrayList(attributes.Length - 1);
            for (int i = 0; i < attributes.Length; i++)
            {
                if (attributes[i].CustomAttributeName != bestCustomAttribute.CustomAttributeName)
                {
                    aCustomAttributes.Add(attributes[i]);
                }
            }

            if (aSample.Rows.Count == 0)
            {
                return(new TreeNode(new CustomAttribute(GetMostCommonValue(aSample, targetCustomAttribute))));
            }
            else
            {
                DecisionTreeID3 dc3       = new DecisionTreeID3();
                TreeNode        ChildNode = dc3.MountTree(aSample, targetCustomAttribute, (CustomAttribute[])aCustomAttributes.ToArray(typeof(CustomAttribute)));
                root.AddTreeNode(ChildNode, value);
            }
        }
        return(root);
    }