/// <summary> /// Inicjuje nowe wyst¹pienie klasy atrybutu /// </summary> /// <param name="name">Wskazuje nazwê atrybutu</param> /// <param name="values">Wskazuje mo¿liwe wartoœci dla atrybutu</param> public TreeAttribute(string name, PossibleValueCollection possibleValues) { _name = name; if (possibleValues == null) { _possibleValues = null; } else { _possibleValues = possibleValues; _possibleValues.Sort(); } }
/// <summary> /// Calculate the gain of an attribute /// </summary> /// <param name="attribute">Attribute to be calculated </param> /// <returns> Gain attribute </returns> private double gain(DataTable samples, TreeAttribute attribute) { PossibleValueCollection values = attribute.PossibleValues; if (isContinousSet(values)) { double sum = 0.0; double bsum = -9999999.0; // return the value for the best possible split for (int i = 0; i < values.Count; i++) { int positives, negatives; positives = negatives = 0; getValuesToAttributeContinous(samples, attribute, values[i], out positives, out negatives); double entropy = getCalculatedEntropy(positives, negatives); sum = -(double)(positives + negatives) / mTotal * entropy; if (sum > bsum) { bsum = sum; } } return(mEntropySet + bsum); } else { double sum = 0.0; for (int i = 0; i < values.Count; i++) { int positives, negatives; positives = negatives = 0; getValuesToAttribute(samples, attribute, values[i], out positives, out negatives); // does it really split? int remainder = mTotal - (positives + negatives); if (remainder > 0) { double entropy = getCalculatedEntropy(positives, negatives); sum += -(double)(positives + negatives) / mTotal * entropy; } } return(mEntropySet + sum); } }
bool isContinousSet(PossibleValueCollection values) { if (values.Count == 0) { return(false); } for (int i = 0; i < values.Count; i++) { double result; if (Double.TryParse(values[i], out result) == false) { return(false); } } return(true); }
private double gain(DataTable samples, TreeAttribute attribute) { PossibleValueCollection values = attribute.PossibleValues; double sum = 0.0; for (int i = 0; i < values.Count; i++) { int positives, negatives; positives = negatives = 0; getValuesToAttribute(samples, attribute, values[i], out positives, out negatives); double entropy = calculateEntropy(positives, negatives); sum += -(double)(positives + negatives) / mTotal * entropy; } return(mEntropySet + sum); }
public PossibleValueCollection GetValuesFromColumn(string columnName) { PossibleValueCollection returnList = new PossibleValueCollection(); foreach (DataRow row in this.Rows) { foreach (DataColumn column in this.Columns) { if (column.ColumnName.ToUpper() == columnName.ToUpper().Trim()) { if (!returnList.Contains(row[column].ToString())) { returnList.Add(row[column].ToString()); } } } } return(returnList); }
/// <summary> /// Sets up a decision tree based on samples submitted /// </summary> /// <param name="samples">Table with samples that will be provided for mounting the tree </param> /// <param name="targetAttribute"> Name column of the table that otherwise has the value true or false to /// Validate or not a sample</param> /// <returns>The root of the decision tree mounted </returns></returns?> private TreeNode internalMountTree(DataTable samples, string targetAttribute, TreeAttributeCollection attributes) { if (allSamplesAreUniform(samples, targetAttribute) == true) { return(new TreeNode(new OutcomeTreeAttribute(getUniformRefValue(samples, targetAttribute)))); } //if (allSamplesArePositive(samples, targetAttribute) == true) // return new TreeNode(new OutcomeTreeAttribute(true)); //if (allSamplesAreNegative(samples, targetAttribute) == true) // return new TreeNode(new OutcomeTreeAttribute(false)); if (attributes.Count == 0) { return(new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(samples, targetAttribute)))); } mTotal = samples.Rows.Count; mTargetAttribute = targetAttribute; mTotalPositives = countTotalPositives(samples); mEntropySet = getCalculatedEntropy(mTotalPositives, mTotal - mTotalPositives); TreeAttribute bestAttribute = getBestAttribute(samples, attributes); TreeNode root = new TreeNode(bestAttribute); if (bestAttribute == null) { return(root); } PossibleValueCollection bestAttrValues = bestAttribute.PossibleValues; bool continousSet = isContinousSet(bestAttrValues); //DataTable aSample = samples.Clone(); if (continousSet) { string value = bestSplitValue(samples, bestAttribute); { DataTable aSample = samples.Clone(); //First Below then Above DataRow[] rows; string cond = bestAttribute.AttributeName + " <= " + "" + value + ""; rows = samples.Select(cond); aSample.Rows.Clear(); foreach (DataRow row in rows) { aSample.Rows.Add(row.ItemArray); Console.WriteLine(" SPLIT {0} ROW:", cond); foreach (DataColumn myCol in samples.Columns) { Console.WriteLine(" " + row[myCol]); } } // Create a new attribute list unless the attribute which is the current best attribute TreeAttributeCollection aAttributes = new TreeAttributeCollection(); //ArrayList aAttributes = new ArrayList(attributes.Count - 1); for (int i = 0; i < attributes.Count; i++) { if (attributes[i].AttributeName != bestAttribute.AttributeName) { aAttributes.Add(attributes[i]); } } //Recycle the best continous attribute if there are others if (aAttributes.Count > 0) { aAttributes.Add(bestAttribute); } // Create a new attribute list unless the attribute which is the current best attribute if (rows.Length == 0) { //return new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(aSample, targetAttribute))); return(new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(samples, targetAttribute)))); } else { DecisionTree dc3 = new DecisionTree(); TreeNode ChildNode = dc3.mountTree(aSample, targetAttribute, aAttributes); root.AddTreeNode(ChildNode, value, "leq"); } } { DataTable aSample = samples.Clone(); DataRow[] rows2; string cond = bestAttribute.AttributeName + " > " + "" + value + ""; rows2 = samples.Select(cond); aSample.Rows.Clear(); foreach (DataRow row in rows2) { aSample.Rows.Add(row.ItemArray); Console.WriteLine(" SPLIT {0} ROW:", cond); foreach (DataColumn myCol in samples.Columns) { Console.WriteLine(" " + row[myCol]); } } // Create a new attribute list unless the attribute which is the current best attribute TreeAttributeCollection aAttributes2 = new TreeAttributeCollection(); //ArrayList aAttributes = new ArrayList(attributes.Count - 1); for (int i = 0; i < attributes.Count; i++) { if (attributes[i].AttributeName != bestAttribute.AttributeName) { aAttributes2.Add(attributes[i]); } } //Recycle the best continous attribute if there are others if (aAttributes2.Count > 0) { aAttributes2.Add(bestAttribute); } // Create a new attribute list unless the attribute which is the current best attribute if (rows2.Length == 0) { //return new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(aSample, targetAttribute))); return(new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(samples, targetAttribute)))); } else { DecisionTree dc3 = new DecisionTree(); TreeNode ChildNode = dc3.mountTree(aSample, targetAttribute, aAttributes2); root.AddTreeNode(ChildNode, value, "gt"); } } } else { DataTable aSample = samples.Clone(); foreach (string value in bestAttribute.PossibleValues) { // Select all elements with the value of this attribute aSample.Rows.Clear(); DataRow[] rows; rows = samples.Select(bestAttribute.AttributeName + " = " + "'" + value + "'"); foreach (DataRow row in rows) { aSample.Rows.Add(row.ItemArray); } // Select all elements with the value of this attribute // Create a new attribute list unless the attribute which is the current best attribute TreeAttributeCollection aAttributes = new TreeAttributeCollection(); //ArrayList aAttributes = new ArrayList(attributes.Count - 1); for (int i = 0; i < attributes.Count; i++) { if (attributes[i].AttributeName != bestAttribute.AttributeName) { aAttributes.Add(attributes[i]); } } // Create a new attribute list unless the attribute which is the current best attribute if (aSample.Rows.Count == 0) { //return new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(aSample, targetAttribute))); return(new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(samples, targetAttribute)))); } else { DecisionTree dc3 = new DecisionTree(); TreeNode ChildNode = dc3.mountTree(aSample, targetAttribute, aAttributes); root.AddTreeNode(ChildNode, value, "eq"); } } } return(root); }