public void TesteID3() { Attribute ceu = new Attribute("ceu", new string[] { "sol", "nublado", "chuva" }); Attribute temperatura = new Attribute("temperatura", new string[] { "alta", "baixa", "suave" }); Attribute humidade = new Attribute("humidade", new string[] { "alta", "normal" }); Attribute vento = new Attribute("vento", new string[] { "sim", "nao" }); Attribute[] attributes = new Attribute[] { ceu, temperatura, humidade, vento }; DataTable samples = getDataTable(); DecisionTreeID3 id3 = new DecisionTreeID3(); TreeNode root = id3.mountTree(samples, "result", attributes); printNode(root, ""); }
// 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(); } } } }
static void Main(string[] args) { /*** Множество значений атрибутов про сердце ***/ /* string fileName = "data_heart.txt"; int countAtr = 22; Attribute f1 = new Attribute("f1", new string[] {"0", "1"}); Attribute f2 = new Attribute("f2", new string[] {"0", "1"}); Attribute f3 = new Attribute("f3", new string[] {"0", "1"}); Attribute f4 = new Attribute("f4", new string[] {"0", "1"}); Attribute f5 = new Attribute("f5", new string[] {"0", "1"}); Attribute f6 = new Attribute("f6", new string[] {"0", "1"}); Attribute f7 = new Attribute("f7", new string[] {"0", "1"}); Attribute f8 = new Attribute("f8", new string[] {"0", "1"}); Attribute f9 = new Attribute("f9", new string[] {"0", "1"}); Attribute f10 = new Attribute("f10", new string[] {"0", "1"}); Attribute f11= new Attribute("f11", new string[] {"0", "1"}); Attribute f12= new Attribute("f12", new string[] {"0", "1"}); Attribute f13= new Attribute("f13", new string[] {"0", "1"}); Attribute f14= new Attribute("f14", new string[] {"0", "1"}); Attribute f15= new Attribute("f15", new string[] {"0", "1"}); Attribute f16= new Attribute("f16", new string[] {"0", "1"}); Attribute f17= new Attribute("f17", new string[] {"0", "1"}); Attribute f18= new Attribute("f18", new string[] {"0", "1"}); Attribute f19= new Attribute("f19", new string[] {"0", "1"}); Attribute f20= new Attribute("f20", new string[] {"0", "1"}); Attribute f21= new Attribute("f21", new string[] {"0", "1"}); Attribute f22= new Attribute("f22", new string[] {"0", "1"}); Attribute[] attributes = new Attribute[] { f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22}; */ /*** Множество значений атрибутов про шары ***/ string fileName = "data_balloons.txt"; int countAtr = 4; Attribute color = new Attribute("color", new string[] {"YELLOW", "GREEN"}); Attribute size = new Attribute("size", new string[] {"LARGE", "SMALL"}); Attribute act = new Attribute("act", new string[] { "STRETCH", "DIP"}); Attribute age = new Attribute("age", new string[] { "ADULT", "CHILD" }); Attribute[] attributes = new Attribute[] { color, size, act, age}; DataTable samples = getDataTable(fileName, countAtr); DecisionTreeID3 id3 = new DecisionTreeID3(); TreeNode root = id3.mountTree(samples, "result", attributes); StreamWriter writer = new StreamWriter ("out.txt"); printNode(root, "", writer); writer.Close(); Console.WriteLine("Дерево в файле out.txt. Нажмите Enter для завершения"); Console.ReadLine(); }
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); }
private TreeNode internalMountTree(DataTable samples, string targetAttribute, Attribute[] attributes) { if (allSamplesPositives(samples, targetAttribute) == true) return new TreeNode(new Attribute(true)); if (allSamplesNegatives(samples, targetAttribute) == true) return new TreeNode(new Attribute(false)); if (attributes.Length == 0) return new TreeNode(new Attribute(getMostCommonValue(samples, targetAttribute))); mTotal = samples.Rows.Count; mTargetAttribute = targetAttribute; mTotalPositives = countTotalPositives(samples); mEntropySet = calcEntropy(mTotalPositives, mTotal - mTotalPositives); Attribute bestAttribute = getBestAttribute(samples, attributes); TreeNode root = new TreeNode(bestAttribute); DataTable aSample = samples.Clone(); foreach (string value in bestAttribute.values) { aSample.Rows.Clear(); DataRow[] rows = samples.Select(bestAttribute.AttributeName + " = " + "'" + value + "'"); foreach (DataRow row in rows) { aSample.Rows.Add(row.ItemArray); } ArrayList aAttributes = new ArrayList(attributes.Length - 1); for (int i = 0; i < attributes.Length; i++) { if (attributes[i].AttributeName != bestAttribute.AttributeName) aAttributes.Add(attributes[i]); } if (aSample.Rows.Count == 0) { //return new TreeNode(new Attribute(getMostCommonValue(aSample, targetAttribute))); return new TreeNode(new Attribute(null)); } else { DecisionTreeID3 dc3 = new DecisionTreeID3(); TreeNode ChildNode = dc3.mountTree(aSample, targetAttribute, (Attribute[])aAttributes.ToArray(typeof(Attribute))); root.AddTreeNode(ChildNode, value); } } return root; }