public TargetCategoricalClass(SchemaVariable _variable, Node _node) { variable = _variable; node = _node; ClassSd = new SortedList<string, int>(); Def.Db.TargetCategoricalClassFill(node, ClassSd); }
public Predictor(SchemaVariable var, Node _node, int predictorLstIdx) { ChildrenGroups = new ValueGroup(this, 2); Variable = var; node = _node; PredictorLstIdx = predictorLstIdx; ValueSd = new SortedList<string,int>(); }
public NodeTargetContinuous(Node ancestor) : base(ancestor) { ImpCalc(); // Outdated use the PreCalculated //Imp = Ancestor.DescendentImpPreCalculated[ancestor.DescendentLst.Count - 1]; UpdateLevelLst(this); UI = new NodeTargetContinuousUI(this); }
public NodeTargetCategorical(Node ancestor) : base(ancestor) { //Fill the TargetCategoricalCases TargetClasses = new TargetCategoricalClass(Def.Schema.Target, this); //Imp = ancestor.DescendentImpPreCalculated[ancestor.DescendentLst.Count - 1]; ImpCalc(); // Outdated use the PreCalculated UpdateLevelLst(this); if (Def.ExperimentRunning) return; UI = new NodeTargetCategoricalUI(this); }
public Node(Node ancestor) { Tree = Def.Tree; ++Node.CountId; id = Node.CountId; PositionType = PositionTypeEnum.Intermediate; Table = new TableMap(this); Def.Db.PredictorsFill(this); MvProcedures(); Def.Tree.NodeAdd(this); Ancestor = ancestor; Ancestor.DescendentLst.Add(this); }
//The variation of the index for this predictor ex: //Sex: female, male offset 1 = male public PredictorMv(SchemaVariable var, Node _node, int idx, int fieldSpan, int nativeIdx, int offSet, int predictorLstIdx, MvTb MvTb) { Variable = var; N = _node; PredMvLstIdx = idx; Table = MvTb; FieldSpan = fieldSpan; NativeIdx = nativeIdx; Offset = offSet; PredictorLstIdx = predictorLstIdx; if (Variable.VariableTypeUserSet == SchemaVariable.VariableTypeEnum.Continuous) { LowerNumber = N.PredictorLst[PredictorLstIdx].LowerNumber; HigherNumber = N.PredictorLst[PredictorLstIdx].HigherNumber; } }
public void NodeFill(Node node) { if (node.DescendentLst.Count == 0) return; Node nl = node.DescendentLst[0], nr = node.DescendentLst[1]; string sql; double varSum; Def.Db.ValidationTableIndexDropIfExists(nl.Id); Def.Db.TableDropIfExists(Def.DbTsTb + nl.Id); Def.Db.ValidationTableIndexDropIfExists(nr.Id); Def.Db.TableDropIfExists(Def.DbTsTb + nr.Id); //Left sql = @"CREATE TABLE " + Def.DbTsTb + nl.Id + "(" + Def.DbTableIdName + " integer NOT NULL)"; Def.Db.ExecuteNonQuery(sql); //Right node sql = @"CREATE TABLE " + Def.DbTsTb + nr.Id + "(" + Def.DbTableIdName + " integer NOT NULL)"; Def.Db.ExecuteNonQuery(sql); List<string> insert = new List<string>(); node.MvTsTb.RowCount = (int)Def.Db.GetNumber("Select count(*) from " + Def.DbTsTb + node.Id); node.MvTsTb.DataFill(); for (int y = 0; y < node.MvTsTb.RowCount; ++y) { varSum = 0; for (int i = 0; i < node.PredMvTsLst.Count; ++i) { varSum += node.PredMvLst[i].Coef * node.PredMvTsLst[i].X(y); } if ((varSum + node.C) <= 0) { insert.Add(@"insert into " + Def.DbTsTb + (nl.Id) + " values (" + node.MvTsTb.Data.ID[y] + ")"); } else { insert.Add(@"insert into " + Def.DbTsTb + (nr.Id) + " values (" + node.MvTsTb.Data.ID[y] + ")"); } } node.MvTb.DataEmpty(); Def.Db.NonQueryTransaction(insert); Def.Db.ValidationTableIndexCreate(nr.Id); Def.Db.ValidationTableIndexCreate(nl.Id); NodeFill(nl); NodeFill(nr); }
//Called only by public void RemoveDescendents(Node subtree) private void SelectNodesToRemove(Node nd, List<Node> toRemove) { foreach (Node ndDescendent in nd.DescendentLst) { toRemove.Add(ndDescendent); SelectNodesToRemove(ndDescendent, toRemove); } nd.DescendentLst.Clear(); return; }
public bool AutoSplitMv(Node node) { int nextNodeId = -1; string sql = ""; double varSum=0; NodeTargetCategorical lcat, rcat; NodeTargetContinuous lcon, rcon; //if (node.SplitVariable.SplitStatus != Predictor.SplitStatusEnum.CanBeUsed) // return false; if (node.Level >= Def.TreeLevelsMax) return false; nextNodeId = Node.CountId + 1; Def.Db.TableDropIfExists(Def.DbTrTb + nextNodeId); Def.Db.ReferenceTableIndexDropIfExists(nextNodeId); //Left node sql = @"CREATE TABLE " + Def.DbTrTb + nextNodeId + "(" + Def.DbTableIdName + " integer NOT NULL)"; Def.Db.ExecuteNonQuery(sql); //Right node nextNodeId = nextNodeId + 1; Def.Db.TableDropIfExists(Def.DbTrTb + nextNodeId); Def.Db.ReferenceTableIndexDropIfExists(nextNodeId); sql = @"CREATE TABLE " + Def.DbTrTb + nextNodeId + "(" + Def.DbTableIdName + " integer NOT NULL)"; Def.Db.ExecuteNonQuery(sql); List<string> insert = new List<string>(); for (int y = 0; y < node.Table.RowCount; ++y) { varSum = 0; for (int i = 0; i < node.PredMvLst.Count; ++i) { varSum += node.PredMvLst[i].Coef * node.PredMvLst[i].X(y); } if ((varSum + node.C) <= 0) { insert.Add(@"insert into " + Def.DbTrTb + (nextNodeId - 1) + " values (" + node.MvTb.Data.ID[y] + ")"); } else { insert.Add(@"insert into " + Def.DbTrTb + (nextNodeId) + " values (" + node.MvTb.Data.ID[y] + ")"); } } Def.Db.NonQueryTransaction(insert); Def.Db.ReferenceTableIndexCreate(nextNodeId-1); Def.Db.ReferenceTableIndexCreate(nextNodeId); if (Def.Schema.Target.VariableTypeUserSet == SchemaVariable.VariableTypeEnum.Continuous) { rcon = new NodeTargetContinuous(node); lcon = new NodeTargetContinuous(node); // node equation goes here } else { rcat = new NodeTargetCategorical(node); lcat = new NodeTargetCategorical(node); // node equation goes here } afterSplit(node); return true; }
public TableMap(Node _node) { node = _node; schema = Def.Schema; rowCount = (int) Def.Db.GetNumber("Select count(*) from " + Def.DbTrTb + node.Id); }
public void FullAutogrow(Node node) { FrmMessage fmsg = null; if (!Def.ExperimentRunning) { Def.FrmMain.WindowState = FormWindowState.Minimized; fmsg = new FrmMessage("Please, wait", "Calculating..."); fmsg.Show(Def.FrmMain); Application.DoEvents(); } FullAutoGrowSubtree(node); if (!Def.ExperimentRunning) { Def.FrmMain.WindowState = FormWindowState.Maximized; fmsg.Close(); Def.FrmMain.TopMost = true; Application.DoEvents(); Def.FrmMain.TopMost = false; Def.FrmMain.Focus(); } Def.Tree.GrowthState = Tree.GrowthStateEnum.FullGrow; }
public void NodeFill(Node node) { if (node.DescendentLst.Count == 0) return; Node nl = node.DescendentLst[0], nr = node.DescendentLst[1]; string sql; int nextNodeId; Def.Db.TableDropIfExists(Def.DbTsTb + nl.Id); Def.Db.TableDropIfExists(Def.DbTsTb + nr.Id); Def.Db.ValidationTableIndexDropIfExists(nl.Id); Def.Db.ValidationTableIndexDropIfExists(nr.Id); if (node.SplitVariable.Variable.VariableTypeUserSet == SchemaVariable.VariableTypeEnum.Continuous) { sql = @"CREATE TABLE " + Def.DbTsTb + nl.Id + " AS " + "SELECT " + Def.DbTsTb + node.Id + "." + Def.DbTableIdName + " FROM " + Def.DbTsTb + node.Id + ", " + Def.DbBsTb + " WHERE " + Def.DbTsTb + node.Id + "." + Def.DbTableIdName + "=" + Def.DbBsTb + "." + Def.DbTableIdName + " AND " + node.SplitVariable.Variable.Name + " <= " + node.SplitValue; Def.Db.ExecuteNonQuery(sql); Def.Db.ValidationTableIndexCreate(nl.Id); sql = @"CREATE TABLE " + Def.DbTsTb + nr.Id + " AS " + "SELECT " + Def.DbTsTb + node.Id + "." + Def.DbTableIdName + " FROM " + Def.DbTsTb + node.Id + ", " + Def.DbBsTb + " WHERE " + Def.DbTsTb + node.Id + "." + Def.DbTableIdName + "=" + Def.DbBsTb + "." + Def.DbTableIdName + " AND " + node.SplitVariable.Variable.Name + " > " + node.SplitValue; Def.Db.ExecuteNonQuery(sql); Def.Db.ValidationTableIndexCreate(nr.Id); } else { //IF(nd.SplitVariable.Variable.VariableTypeUserSet == SchemaVariable.VariableTypeEnum.Categorical) string vals = ""; nextNodeId = nl.Id; //LEFT NODE List<string> caseLst = node.SplitVariable.ChildrenGroups.ValueGroupLst[0]; for (int i = 0; i < caseLst.Count; ++i) { vals += node.SplitVariable.Variable.Name + "='" + caseLst[i] + "' "; if (i < (caseLst.Count - 1)) { vals += " or "; } else vals += ")"; } sql = @"CREATE TABLE " + Def.DbTsTb + nextNodeId + " AS " + "SELECT " + Def.DbTsTb + node.Id + "." + Def.DbTableIdName + " FROM " + Def.DbTsTb + node.Id + ", " + Def.DbBsTb + " WHERE (" + Def.DbTsTb + node.Id + "." + Def.DbTableIdName + "=" + Def.DbBsTb + "." + Def.DbTableIdName + ") AND (" + vals; Def.Db.ExecuteNonQuery(sql); Def.Db.ValidationTableIndexCreate(nextNodeId); //RIGHT NODE vals = ""; nextNodeId = nr.Id; caseLst = node.SplitVariable.ChildrenGroups.ValueGroupLst[1]; for (int i = 0; i < caseLst.Count; ++i) { vals += node.SplitVariable.Variable.Name + "='" + caseLst[i] + "' "; if (i < (caseLst.Count - 1)) { vals += " or "; } else vals += ")"; } sql = @"CREATE TABLE " + Def.DbTsTb + nextNodeId + " AS " + "SELECT " + Def.DbTsTb + node.Id + "." + Def.DbTableIdName + " FROM " + Def.DbTsTb + node.Id + ", " + Def.DbBsTb + " WHERE (" + Def.DbTsTb + node.Id + "." + Def.DbTableIdName + "=" + Def.DbBsTb + "." + Def.DbTableIdName + ") AND (" + vals; Def.Db.ExecuteNonQuery(sql); Def.Db.ValidationTableIndexCreate(nextNodeId); } NodeFill(nl); NodeFill(nr); }
public void NodeAdd(Node n) { NodeLst.Add(n); return; }
public void ManuallySplit(Node node) { int nextNodeId=-1; string sql=""; NodeTargetCategorical lcat, rcat; NodeTargetContinuous lcon, rcon; if (node.SplitVariable.Variable.VariableTypeUserSet == SchemaVariable.VariableTypeEnum.Continuous) { nextNodeId = Node.CountId+1; Def.Db.TableDropIfExists(Def.DbTrTb + nextNodeId); Def.Db.ReferenceTableIndexDropIfExists(nextNodeId); sql = @"CREATE TABLE " + Def.DbTrTb + nextNodeId + " AS " + "SELECT " + Def.DbTrTb + node.Id + "." + Def.DbTableIdName + " FROM " + Def.DbTrTb + node.Id + ", " + Def.DbBsTb + " WHERE " + Def.DbTrTb + node.Id + "." + Def.DbTableIdName + "=" + Def.DbBsTb + "." + Def.DbTableIdName + " AND " + node.SplitVariable.Variable.Name + " <= " + node.SplitValue; Def.Db.ExecuteNonQuery(sql); Def.Db.ReferenceTableIndexCreate(nextNodeId); if (Def.Schema.Target.VariableTypeUserSet == SchemaVariable.VariableTypeEnum.Continuous) { lcon = new NodeTargetContinuous(node); lcon.LabelTopText = " <= " + Math.Round(node.SplitValue, 2); } else { lcat = new NodeTargetCategorical(node); lcat.LabelTopText = " <= " + Math.Round(node.SplitValue, 2); } //Right nextNodeId = Node.CountId + 1; Def.Db.TableDropIfExists(Def.DbTrTb + nextNodeId); Def.Db.ReferenceTableIndexDropIfExists(nextNodeId); sql = @"CREATE TABLE " + Def.DbTrTb + nextNodeId + " AS " + "SELECT " + Def.DbTrTb + node.Id + "." + Def.DbTableIdName + " FROM " + Def.DbTrTb + node.Id + ", " + Def.DbBsTb + " WHERE " + Def.DbTrTb + node.Id + "." + Def.DbTableIdName + "=" + Def.DbBsTb + "." + Def.DbTableIdName + " AND " + node.SplitVariable.Variable.Name + " > " + node.SplitValue; Def.Db.ExecuteNonQuery(sql); Def.Db.ReferenceTableIndexCreate(nextNodeId); if (Def.Schema.Target.VariableTypeUserSet == SchemaVariable.VariableTypeEnum.Continuous) { rcon = new NodeTargetContinuous(node); rcon.LabelTopText = " > " + Math.Round(node.SplitValue, 2); } else { rcat = new NodeTargetCategorical(node); rcat.LabelTopText = " > " + Math.Round(node.SplitValue, 2); } } else //SchemaVariable.VariableTypeEnum.Continuous if (node.SplitVariable.Variable.VariableTypeUserSet == SchemaVariable.VariableTypeEnum.Categorical) { string vals = ""; nextNodeId = Node.CountId + 1; //LEFT NODE List<string> caseLst = node.SplitVariable.ChildrenGroups.ValueGroupLst[0]; for (int i = 0; i < caseLst.Count; ++i) { vals += node.SplitVariable.Variable.Name + "='" + caseLst[i] + "' "; if (i < (caseLst.Count - 1)) { vals += " or "; } else vals += ")"; } Def.Db.TableDropIfExists(Def.DbTrTb + nextNodeId); Def.Db.ReferenceTableIndexDropIfExists(nextNodeId); sql = @"CREATE TABLE " + Def.DbTrTb + nextNodeId + " AS " + "SELECT " + Def.DbTrTb + node.Id + "." + Def.DbTableIdName + " FROM " + Def.DbTrTb + node.Id + ", " + Def.DbBsTb + " WHERE (" + Def.DbTrTb + node.Id + "." + Def.DbTableIdName + "=" + Def.DbBsTb + "." + Def.DbTableIdName + ") AND (" + vals; Def.Db.ExecuteNonQuery(sql); Def.Db.ReferenceTableIndexCreate(nextNodeId); if (Def.Schema.Target.VariableTypeUserSet == SchemaVariable.VariableTypeEnum.Continuous) { lcon = new NodeTargetContinuous(node); } else { lcat = new NodeTargetCategorical(node); } //RIGHT NODE vals = ""; nextNodeId = Node.CountId + 1; caseLst = node.SplitVariable.ChildrenGroups.ValueGroupLst[1]; for (int i = 0; i < caseLst.Count; ++i) { vals += node.SplitVariable.Variable.Name + "='" + caseLst[i] + "' "; if (i < (caseLst.Count - 1)) { vals += " or "; } else vals += ")"; } Def.Db.TableDropIfExists(Def.DbTrTb + nextNodeId); Def.Db.ReferenceTableIndexDropIfExists(nextNodeId); sql = @"CREATE TABLE " + Def.DbTrTb + nextNodeId + " AS " + "SELECT " + Def.DbTrTb + node.Id + "." + Def.DbTableIdName + " FROM " + Def.DbTrTb + node.Id + ", " + Def.DbBsTb + " WHERE (" + Def.DbTrTb + node.Id + "." + Def.DbTableIdName + "=" + Def.DbBsTb + "." + Def.DbTableIdName + ") AND (" + vals; Def.Db.ExecuteNonQuery(sql); Def.Db.ReferenceTableIndexCreate(nextNodeId); if (Def.Schema.Target.VariableTypeUserSet == SchemaVariable.VariableTypeEnum.Continuous) { rcon = new NodeTargetContinuous(node); } else { rcat = new NodeTargetCategorical(node); } } afterSplit(node); }
public void RemoveDescendents(Node subtree) { List<Node> NodeRemoveLst = new List<Node>(); subtree.LabelBottomHide(); SelectNodesToRemove(subtree, NodeRemoveLst); foreach (Node node in NodeRemoveLst) { Def.Tree.NodeLst.Remove(node); node.Dispose(); } Def.Tree.UpdateLevelsInformation(); subtree.SplitDone = false; if (subtree is NodeTargetCategorical) { NodeTargetCategorical nd = (NodeTargetCategorical)subtree; nd.UI.LabelBottom.Text = ""; } else { NodeTargetContinuous nd = (NodeTargetContinuous)subtree; nd.UI.LabelBottom.Text = ""; } Def.Tree.growthState=GrowthStateEnum.Intermediate; }
//public abstract void AllGainsCalcMv(); public void UpdateLevelLst(Node n) { Level lv; if (n.PositionType != Node.PositionTypeEnum.Root) { n.level = n.Ancestor.level + 1; if (Tree.LevelLast < n.level) { lv = new Level(); lv.NodeLst.Add(n); Tree.LevelLst.Add(lv); } else { Tree.LevelLst[n.Level].NodeLst.Add(n); } } // LabelBottomText = splitVariable.Variable.Name; }
public static double VarianceTimesCount(List<string> vals, Node node, Predictor pred) { string sql = "", valSql=""; List<double> NLst = null; for (int i = 0; i < vals.Count; ++i) { valSql += pred.Variable.Name + "='" + vals[i] + "' "; if (i < (vals.Count - 1)) { valSql += " or "; } else valSql += ")"; } sql = @"SELECT " + "COALESCE(variance(" + Def.Tree.Schema.Target.Name + "), 0), count(*) " + "FROM " + Def.DbBsTb + " , " + Def.DbTrTb + node.Id + " " + "WHERE (" + Def.DbBsTb + "." + Def.DbTableIdName + "=" + Def.DbTrTb + node.Id + "." + Def.DbTableIdName + ") and (" + valSql; NLst = Def.Db.GetNumberRowLst(sql); return NLst[0] * NLst[1]; }
private void FullAutoGrowSubtree(Node nd) { bool couldSplit = false; if (nd.Level == Def.TreeLevelsMax) return; if (!Def.Multivariate) { nd.AllGainsCalc(); nd.SearchBestSplit(); nd.SplitVariableIdx = nd.BestSplitIdx; couldSplit = nd.Tree.AutoSplit(nd); } else { nd.AllGainsCalc(); if (nd.SearchBestSplit()) { nd.SplitVariableIdx = nd.BestSplitIdx; nd.MvTb.DataFill(); nd.LocalMinimumSearchMv(); couldSplit = nd.Tree.AutoSplitMv(nd); nd.MvTb.DataEmpty(); } } if (couldSplit) foreach (Node ndDescendent in nd.DescendentLst) FullAutoGrowSubtree(ndDescendent); }
private void afterSplit(Node node) { // Things to do after any kind of split node.LabelBottomText = node.SplitVariable.Variable.Name; node.SplitDone = true; Def.Tree.GrowthState = Tree.GrowthStateEnum.Intermediate; CreateLayout(); }