public bool test_Equal_True(out string message) { message = ""; DataLeaf a = new DataLeaf(typeof(int)); DataLeaf b = new DataLeaf(typeof(int)); a.Value = 874; b.Value = 874; if (!a.Equal(b)) { message = "wrong result of Equal - value '" + a.Value.ToString() + "' vs '" + b.Value.ToString() + "'"; return(false); } a = new DataLeaf(typeof(int[])); b = new DataLeaf(typeof(int[])); int [] array = new int[] { 874, 123 }; a.Value = array; b.Value = array; if (!a.Equal(b)) { message = "wrong result of Equal - reference"; return(false); } return(true); }
private static IDecisionNode NodeToDecisionTree(IDataNode node) { if (node is DataLeaf) { DataLeaf leaf = (DataLeaf)node; Tactic tactic = EnumUtils.GetEnumValues <Tactic>().MaxBy(t => leaf.ClassDistribution[(int)t]); return(new DecisionLeaf(tactic)); } else if (node is AttributeSplit) { AttributeSplit attributeSplit = (AttributeSplit)node; return(new DecisionNode { Partitioner = new ContinuousPartitioner((ContinuousAxis)attributeSplit.Axis, attributeSplit.SplitValue), Left = NodeToDecisionTree(attributeSplit.Left), Right = NodeToDecisionTree(attributeSplit.Right), }); } else if (node is CategoricalSplit) { CategoricalSplit categoricalSplit = (CategoricalSplit)node; return(new DecisionNode { Partitioner = new CategoricalPartitioner( (CategoricalAxis)categoricalSplit.Axis, PolicyHelper.BitsToCategories((CategoricalAxis)categoricalSplit.Axis, categoricalSplit.Categories).ToArray()), Left = NodeToDecisionTree(categoricalSplit.Left), Right = NodeToDecisionTree(categoricalSplit.Right), }); } else { throw new ArgumentException("Unknown node type: " + node); } }
private static double Accuracy(IDataNode root, IEnumerable <DataPoint> dataPoints) { return(dataPoints.Average(dataPoint => { DataLeaf leaf = Resolve(root, dataPoint); return (double)leaf.ClassDistribution[dataPoint.Class] / leaf.ClassDistribution.Sum(); })); }
public bool test_Value_TypeOF(out string message) { message = ""; int structValue = 5; DataLeaf leaf = new DataLeaf(typeof(int)); try { leaf.Value = structValue; if ((int)leaf.Value != structValue) { message = "Leaf returns wrong value (struct)"; return(false); } } catch (Exception ex) { message = "Cannot put into leaf struct value\n" + ex.Message; return(false); } DataLeafTest value = new DataLeafTest(); leaf = new DataLeaf(typeof(DataLeafTest)); try { leaf.Value = value; if ((DataLeafTest)leaf.Value != value) { message = "Leaf returns wrong value (struct)"; return(false); } } catch (Exception ex) { message = "Cannot put into leaf struct value\n" + ex.Message; return(false); } return(true); }
public bool test_Value_null(out string message) { message = ""; DataLeaf leaf = new DataLeaf(typeof(DataLeafTest)); try { leaf.Value = null; if (leaf.Value != null) { message = "Leaf returns wrong value (struct)"; return(false); } } catch (Exception ex) { message = "Cannot put into leaf struct value\n" + ex.Message; return(false); } return(true); }
public bool test_Equal_False(out string message) { message = ""; DataLeaf a = new DataLeaf(typeof(int)); DataLeaf b = new DataLeaf(typeof(int)); a.Value = 874; b.Value = 144; if (a.Equal(b)) { message = "wrong result of Equal - value"; return(false); } a = new DataLeaf(typeof(int[])); b = new DataLeaf(typeof(int[])); a.Value = new int[] { 123, 874 }; b.Value = new int[] { 874, 123 }; if (a.Equal(b)) { message = "wrong result of Equal - diffrent references on diffrent values"; return(false); } a = new DataLeaf(typeof(int[])); b = new DataLeaf(typeof(int[])); a.Value = new int[] { 874, 123 }; b.Value = new int[] { 874, 123 }; if (a.Equal(b)) { message = "wrong result of Equal - diffrent references on same values"; return(false); } return(true); }
public void TestSimpleAttributeSplit() { List <DataPoint> dataPoints = new List <DataPoint> { new DataPoint { Class = 0, Attributes = new float[] { 123 }, Categories = new uint[] { }, Weight = 1.0f, }, new DataPoint { Class = 1, Attributes = new float[] { 456 }, Categories = new uint[] { }, Weight = 1.0f, }, }; using (CudaManager cudaManager = Provider.CudaManagerPool.GetCudaManagerForThread(Provider.Logger)) using (DecisionLearner decisionLearner = new DecisionLearner(cudaManager, dataPoints)) { IDataNode root = decisionLearner.FitDecisionTree().Node; AttributeSplit attributeSplit = (AttributeSplit)root; Assert.AreEqual(0, attributeSplit.Axis); Assert.AreEqual(456, attributeSplit.SplitValue); DataLeaf left = (DataLeaf)attributeSplit.Left; Assert.AreEqual(1, left.ClassDistribution[0]); Assert.AreEqual(0, left.ClassDistribution[1]); DataLeaf right = (DataLeaf)attributeSplit.Right; Assert.AreEqual(0, right.ClassDistribution[0]); Assert.AreEqual(1, right.ClassDistribution[1]); Assert.AreEqual(1.0, Accuracy(root, dataPoints)); } }
public bool test_Equal_False(out string message) { message = ""; DataLeaf a = new DataLeaf(typeof(int)); DataLeaf b = new DataLeaf(typeof(int)); a.Value = 874; b.Value = 144; if (a.Equal(b)) { message = "wrong result of Equal - value"; return false; } a = new DataLeaf(typeof(int[])); b = new DataLeaf(typeof(int[])); a.Value = new int[]{123, 874}; b.Value = new int[]{874, 123}; if (a.Equal(b)) { message = "wrong result of Equal - diffrent references on diffrent values"; return false; } a = new DataLeaf(typeof(int[])); b = new DataLeaf(typeof(int[])); a.Value = new int[]{874, 123}; b.Value = new int[]{874, 123}; if (a.Equal(b)) { message = "wrong result of Equal - diffrent references on same values"; return false; } return true; }
public bool test_Equal_True(out string message) { message = ""; DataLeaf a = new DataLeaf(typeof(int)); DataLeaf b = new DataLeaf(typeof(int)); a.Value = 874; b.Value = 874; if (!a.Equal(b)) { message = "wrong result of Equal - value '" + a.Value.ToString() + "' vs '" + b.Value.ToString()+"'"; return false; } a = new DataLeaf(typeof(int[])); b = new DataLeaf(typeof(int[])); int [] array = new int[]{874, 123}; a.Value = array; b.Value = array; if (!a.Equal(b)) { message = "wrong result of Equal - reference"; return false; } return true; }
public bool test_Value_null(out string message) { message = ""; DataLeaf leaf = new DataLeaf(typeof(DataLeafTest)); try { leaf.Value = null; if (leaf.Value != null) { message = "Leaf returns wrong value (struct)"; return false; } } catch (Exception ex) { message = "Cannot put into leaf struct value\n" + ex.Message; return false; } return true; }
public bool test_Value_TypeOF(out string message) { message = ""; int structValue = 5; DataLeaf leaf = new DataLeaf(typeof(int)); try { leaf.Value = structValue; if ((int)leaf.Value != structValue) { message = "Leaf returns wrong value (struct)"; return false; } } catch (Exception ex) { message = "Cannot put into leaf struct value\n" + ex.Message; return false; } DataLeafTest value = new DataLeafTest(); leaf = new DataLeaf(typeof(DataLeafTest)); try { leaf.Value = value; if ((DataLeafTest)leaf.Value != value) { message = "Leaf returns wrong value (struct)"; return false; } } catch (Exception ex) { message = "Cannot put into leaf struct value\n" + ex.Message; return false; } return true; }