Exemple #1
0
        /// <summary>
        /// Searches for the best split using a brute force approach. The searcher only considers splits
        /// when both the threshold value and the target value has changed.
        /// The implementation assumes that the features and targets have been sorted
        /// together using the features as sort criteria
        /// </summary>
        /// <param name="impurityCalculator"></param>
        /// <param name="feature"></param>
        /// <param name="targets"></param>
        /// <param name="parentInterval"></param>
        /// <param name="parentImpurity"></param>
        /// <returns></returns>
        public SplitResult FindBestSplit(IImpurityCalculator impurityCalculator, double[] feature, double[] targets,
                                         Interval1D parentInterval, double parentImpurity)
        {
            var bestSplitIndex          = -1;
            var bestThreshold           = 0.0;
            var bestImpurityImprovement = 0.0;
            var bestImpurityLeft        = 0.0;
            var bestImpurityRight       = 0.0;

            int prevSplit  = parentInterval.FromInclusive;
            var prevValue  = feature[prevSplit];
            var prevTarget = targets[prevSplit];

            impurityCalculator.UpdateInterval(parentInterval);

            for (int j = prevSplit + 1; j < parentInterval.ToExclusive; j++)
            {
                var currentValue  = feature[j];
                var currentTarget = targets[j];
                if ((prevValue != currentValue) && (prevTarget != currentTarget))
                {
                    var currentSplit = j;
                    var leftSize     = (double)(currentSplit - parentInterval.FromInclusive);
                    var rightSize    = (double)(parentInterval.ToExclusive - currentSplit);

                    if (Math.Min(leftSize, rightSize) >= m_minimumSplitSize)
                    {
                        impurityCalculator.UpdateIndex(currentSplit);

                        if ((impurityCalculator.WeightedLeft < m_minimumLeafWeight) ||
                            (impurityCalculator.WeightedRight < m_minimumLeafWeight))
                        {
                            continue;
                        }

                        var improvement = impurityCalculator.ImpurityImprovement(parentImpurity);

                        if (improvement > bestImpurityImprovement)
                        {
                            var childImpurities = impurityCalculator.ChildImpurities(); // could be avoided

                            bestImpurityImprovement = improvement;
                            bestThreshold           = (currentValue + prevValue) * 0.5;
                            bestSplitIndex          = currentSplit;
                            bestImpurityLeft        = childImpurities.Left;
                            bestImpurityRight       = childImpurities.Right;
                        }

                        prevSplit = j;
                    }
                }

                prevValue  = currentValue;
                prevTarget = currentTarget;
            }

            return(new SplitResult(bestSplitIndex, bestThreshold,
                                   bestImpurityImprovement, bestImpurityLeft, bestImpurityRight));
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="parentIndex"></param>
 /// <param name="nodeType"></param>
 /// <param name="interval"></param>
 /// <param name="impurity"></param>
 /// <param name="nodeDepth"></param>
 public DecisionNodeCreationItem(int parentIndex, NodePositionType nodeType, Interval1D interval, double impurity, int nodeDepth)
 {
     ParentIndex = parentIndex;
     NodeType    = nodeType;
     Interval    = interval;
     Impurity    = impurity;
     NodeDepth   = nodeDepth;
 }
 /// <summary>
 /// Copies the provided indices from source to destination within the provided interval
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="indices"></param>
 /// <param name="source"></param>
 /// <param name="interval"></param>
 /// <param name="destination"></param>
 public static void IndexedCopy <T>(this int[] indices, T[] source, Interval1D interval, T[] destination)
 {
     for (int i = interval.FromInclusive; i < interval.ToExclusive; i++)
     {
         var index = indices[i];
         destination[i] = source[index];
     }
 }
Exemple #4
0
 /// <summary>
 /// Copies the provided indices from source to destination within the provided interval
 /// </summary>
 /// <param name="indices"></param>
 /// <param name="source"></param>
 /// <param name="interval"></param>
 /// <param name="destination"></param>
 public static void IndexedCopy(this int[] indices, F64MatrixColumnView source,
                                Interval1D interval, double[] destination)
 {
     for (int i = interval.FromInclusive; i < interval.ToExclusive; i++)
     {
         var index = indices[i];
         destination[i] = source[index];
     }
 }
Exemple #5
0
        public void Interval1D_Equals()
        {
            var sut      = Interval1D.Create(3, 5);
            var equal    = Interval1D.Create(3, 5);
            var notEqual = Interval1D.Create(3, 4);

            Assert.IsTrue(sut.Equals(equal));
            Assert.IsFalse(sut.Equals(notEqual));
        }
    /// <summary>
    /// 判断两个区域是否相交。
    /// </summary>
    /// <param name="that">需要判断相交的另一个区域。</param>
    /// <returns>如果相交则返回 True,否则返回 False。</returns>
    public bool Intersect(Interval1D that)
    {
        if (Max < that.Min || that.Max < Min)
        {
            return(false);
        }

        return(true);
    }
        public void Interval2D_Equals()
        {
            var sut      = Interval2D.Create(Interval1D.Create(0, 5), Interval1D.Create(2, 5));
            var equal    = Interval2D.Create(Interval1D.Create(0, 5), Interval1D.Create(2, 5));
            var notEqual = Interval2D.Create(Interval1D.Create(2, 5), Interval1D.Create(0, 5));

            Assert.IsTrue(sut.Equals(equal));
            Assert.IsFalse(sut.Equals(notEqual));
        }
Exemple #8
0
        /// <summary>
        /// Initialize the calculator with targets, weights and work interval
        /// </summary>
        /// <param name="uniqueTargets"></param>
        /// <param name="targets"></param>
        /// <param name="weights"></param>
        /// <param name="interval"></param>
        public void Init(double[] uniqueTargets, double[] targets, double[] weights, Interval1D interval)
        {
            if (targets == null)
            {
                throw new ArgumentException("targets");
            }
            if (weights == null)
            {
                throw new ArgumentException("weights");
            }
            m_targets  = targets;
            m_weights  = weights;
            m_interval = interval;

            m_weightedTotal = 0.0;
            m_weightedLeft  = 0.0;
            m_weightedRight = 0.0;

            m_meanLeft  = 0.0;
            m_meanRight = 0.0;
            m_meanTotal = 0.0;

            m_sqSumLeft  = 0.0;
            m_sqSumRight = 0.0;
            m_sqSumTotal = 0.0;

            m_varRight = 0.0;
            m_varLeft  = 0.0;

            m_sumLeft  = 0.0;
            m_sumRight = 0.0;
            m_sumTotal = 0.0;

            var w = 1.0;
            var weightsPresent = m_weights.Length != 0;

            for (int i = m_interval.FromInclusive; i < m_interval.ToExclusive; i++)
            {
                if (weightsPresent)
                {
                    w = weights[i];
                }

                var targetValue = targets[i];
                var wTarget     = w * targetValue;
                m_sumTotal   += wTarget;
                m_sqSumTotal += wTarget * targetValue;

                m_weightedTotal += w;
            }

            m_meanTotal = m_sumTotal / m_weightedTotal;

            m_currentPosition = m_interval.FromInclusive;
            this.Reset();
        }
Exemple #9
0
        /// <summary>
        /// Initialize the calculator with targets, weights and work interval
        /// </summary>
        /// <param name="targetNames"></param>
        /// <param name="targets"></param>
        /// <param name="weights"></param>
        /// <param name="interval"></param>
        public void Init(double[] targetNames, double[] targets, double[] weights, Interval1D interval)
        {
            if (targets == null)
            {
                throw new ArgumentException("targets");
            }
            if (weights == null)
            {
                throw new ArgumentException("weights");
            }
            if (targetNames == null)
            {
                throw new ArgumentException("uniqueTargets");
            }
            m_targets     = targets;
            m_weights     = weights;
            m_targetNames = targetNames;
            m_interval    = interval;

            SetMinMaxTargetNames();
            if (m_targetIndexOffSet > 0)
            {
                m_targetIndexOffSet = 0;
            }
            else
            {
                m_targetIndexOffSet = m_targetIndexOffSet * -1;
            }

            m_weightedTargetCount.Reset(m_maxTargetNameIndex, m_targetIndexOffSet);
            m_weightedTargetCountLeft.Reset(m_maxTargetNameIndex, m_targetIndexOffSet);
            m_weightedTargetCountRight.Reset(m_maxTargetNameIndex, m_targetIndexOffSet);

            var w = 1.0;
            var weightsPresent = m_weights.Length != 0;

            m_weightedTotal = 0.0;
            m_weightedLeft  = 0.0;
            m_weightedRight = 0.0;

            for (int i = m_interval.FromInclusive; i < m_interval.ToExclusive; i++)
            {
                if (weightsPresent)
                {
                    w = weights[i];
                }

                var targetIndex = (int)targets[i];
                m_weightedTargetCount[targetIndex] += w;

                m_weightedTotal += w;
            }

            m_currentPosition = m_interval.FromInclusive;
            this.Reset();
        }
        /// <summary>
        /// Sums the values within the provided interval
        /// </summary>
        /// <param name="array"></param>
        /// <param name="interval"></param>
        /// <returns></returns>
        public static double Sum(this double[] array, Interval1D interval)
        {
            var sum = 0.0;

            for (int i = interval.FromInclusive; i < interval.ToExclusive; i++)
            {
                sum += array[i];
            }
            return(sum);
        }
Exemple #11
0
        public void ArrayExtensions_CopyTo()
        {
            var values   = new int[] { 0, 1, 2, 3, 4, 5 };
            var interval = Interval1D.Create(0, values.Length);

            var destination = new int[interval.Length];

            values.CopyTo(interval, destination);
            CollectionAssert.AreEqual(destination, values);
        }
Exemple #12
0
        public unsafe void F64MatrixView_SubView()
        {
            var matrix = Matrix();

            using (var pinnedMatrix = matrix.GetPinnedPointer())
            {
                var subView   = pinnedMatrix.View().View(Interval2D.Create(Interval1D.Create(0, 2), Interval1D.Create(0, 3)));
                var subMatrix = matrix.Rows(new int[] { 0, 1 });
                AssertMatrixView(subMatrix, subView);
            }
        }
Exemple #13
0
        public void F64VectorView_SubView_End()
        {
            var vector = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            using (var pinned = vector.GetPinnedPointer())
            {
                var view     = pinned.View().View(Interval1D.Create(7, 10));
                var expected = new double[] { 7, 8, 9 };
                AssertVectorView(expected, view);
            }
        }
Exemple #14
0
        public void ArrayExtensions_CopyTo_Interval()
        {
            var values   = new int[] { 0, 1, 2, 3, 4, 5 };
            var interval = Interval1D.Create(1, 5);

            var destination = new int[values.Length];

            values.CopyTo(interval, destination);
            var expected = new int[] { 0, 1, 2, 3, 4, 0 };

            CollectionAssert.AreEqual(expected, destination);
        }
Exemple #15
0
        public void ArrayExtensions_IndexedCopy_Interval()
        {
            var values   = new int[] { 0, 10, 20, 30, 40, 50 };
            var indices  = new int[] { 1, 1, 2, 2, 2, 5 };
            var interval = Interval1D.Create(1, 5);

            var destination = new int[values.Length];

            indices.IndexedCopy(values, interval, destination);
            var expected = new int[] { 0, 10, 20, 20, 20, 0 };

            CollectionAssert.AreEqual(expected, destination);
        }
Exemple #16
0
        /// <summary>
        /// Find the best split.
        /// </summary>
        /// <param name="impurityCalculator">
        /// The impurity calculator.
        /// </param>
        /// <param name="feature">
        /// The feature.
        /// </param>
        /// <param name="targets">
        /// The targets.
        /// </param>
        /// <param name="parentInterval">
        /// The parent interval.
        /// </param>
        /// <param name="parentImpurity">
        /// The parent impurity.
        /// </param>
        /// <returns>
        /// The <see cref="SplitResult"/>.
        /// </returns>
        public SplitResult FindBestSplit(
            ITopPerformerFocusImpurityCalculator impurityCalculator,
            double[] feature,
            double[] targets,
            Interval1D parentInterval,
            double parentImpurity)
        {
            var bestKnownSplit = new SplitResult(-1, 0, 0, 0, 0);

            var prevSplitStartIndex  = parentInterval.FromInclusive;
            var previousFeatureValue = feature[prevSplitStartIndex];

            impurityCalculator.UpdateIntervalAndTargets(parentInterval, targets);

            for (var currentSplitIndex = prevSplitStartIndex + this.MinimumSplitSize;
                 currentSplitIndex <= parentInterval.ToExclusive - this.MinimumSplitSize;
                 currentSplitIndex++)
            {
                var currentFeatureValue = feature[currentSplitIndex];
                if (Math.Abs(previousFeatureValue - currentFeatureValue) > 1e-10)
                {
                    impurityCalculator.UpdateIndex(currentSplitIndex);

                    if (impurityCalculator.WeightedLeft < this.MinimumLeafWeight ||
                        impurityCalculator.WeightedRight < this.MinimumLeafWeight)
                    {
                        continue;
                    }

                    var currentImprovement = impurityCalculator.ImpurityImprovement(parentImpurity);

                    // check if the split is better than current best
                    if (currentImprovement > bestKnownSplit.ImpurityImprovement)
                    {
                        var childImpurities = impurityCalculator.ChildImpurities();
                        var bestThreshold   = (currentFeatureValue + previousFeatureValue) * 0.5;

                        bestKnownSplit = new SplitResult(
                            currentSplitIndex,
                            bestThreshold,
                            currentImprovement,
                            childImpurities.Left,
                            childImpurities.Right);
                    }
                }

                previousFeatureValue = currentFeatureValue;
            }

            return(bestKnownSplit);
        }
 void SelectNextRandomIndices(int[] candidateModelIndices)
 {
     if (m_selectWithReplacement)
     {
         for (int i = 0; i < candidateModelIndices.Length; i++)
         {
             candidateModelIndices[i] = m_random.Next(0, m_numberOfModelsToSelect);
         }
     }
     else
     {
         m_allIndices.Shuffle(m_random);
         m_allIndices.CopyTo(Interval1D.Create(0, m_numberOfModelsToSelect), candidateModelIndices);
     }
 }
        public void NaiveSinglePassVarianceEntropyMetric_Impurity_Interval()
        {
            var set1 = new double[] { 0, 1, 2, 3, 4, 3, 2, 1, 0 };
            var set2 = new double[] { 1, 1, 1, 1, 2, 2, 2, 2 };

            var sut      = new NaiveSinglePassVarianceImpurityMetric();
            var interval = Interval1D.Create(2, 7);

            var val1 = sut.Impurity(set1, interval);

            Assert.AreEqual(0.69999999999999929, val1);
            var val2 = sut.Impurity(set2, interval);

            Assert.AreEqual(0.29999999999999982, val2);
        }
        public void RegressionImpurityCalculator_NodeImpurity()
        {
            var values = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, };

            var parentInterval = Interval1D.Create(0, values.Length);

            var sut = new RegressionImpurityCalculator();

            sut.Init(new double[0], values, new double[0], parentInterval);

            sut.UpdateIndex(50);
            var actual = sut.NodeImpurity();

            Assert.AreEqual(0.66666666666666674, actual, 0.000001);
        }
Exemple #20
0
        public void ArrayExtensions_SortWith()
        {
            var values   = new int[] { 0, 1, 2, 3, 4, 5 };
            var keys     = new int[] { 5, 4, 3, 2, 1, 0 };
            var interval = Interval1D.Create(0, keys.Length);

            keys.SortWith(interval, values);
            var expectedKeys = new int[] { 0, 1, 2, 3, 4, 5 };

            CollectionAssert.AreEqual(expectedKeys, keys);

            var expectedValues = new int[] { 5, 4, 3, 2, 1, 0 };

            CollectionAssert.AreEqual(expectedValues, values);
        }
Exemple #21
0
 /// <inheritdoc />
 public SplitResult FindBestSplit(
     TestImpurityCalculator impurityCalculator,
     double[] feature,
     double[] targets,
     Interval1D parentInterval,
     double parentImpurity)
 {
     Interlocked.Increment(ref this._callCount);
     this.CheckIfTargetsDoMatch(impurityCalculator, targets);
     return(this._splitSearcher.FindBestSplit(
                impurityCalculator,
                feature,
                targets,
                parentInterval,
                parentImpurity));
 }
        public void GiniClassificationImpurityCalculator_NodeImpurity()
        {
            var values = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, };
            var unique = values.Distinct().ToArray();

            var parentInterval = Interval1D.Create(0, values.Length);

            var sut = new GiniClassificationImpurityCalculator();

            sut.Init(unique, values, new double[0], parentInterval);

            sut.UpdateIndex(50);
            var actual = sut.NodeImpurity();

            Assert.AreEqual(0.66666666666666674, actual, 0.000001);
        }
Exemple #23
0
        public void ArrayExtensions_IndexedCopy_ColumnView_Interval()
        {
            var values      = new double[] { 0, 10, 20, 30, 40, 50 };
            var matrix      = new F64Matrix(values, 6, 1);
            var indices     = new int[] { 1, 1, 2, 2, 2, 5 };
            var destination = new double[values.Length];
            var interval    = Interval1D.Create(1, 5);

            using (var ptr = matrix.GetPinnedPointer())
            {
                var view = ptr.View().ColumnView(0);
                indices.IndexedCopy(view, interval, destination);
                var expected = new double[] { 0, 10, 20, 20, 20, 0 };
                CollectionAssert.AreEqual(expected, destination);
            }
        }
        public void RegressionImpurityCalculator_LeafValue_Weighted()
        {
            var values         = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, };
            var weights        = values.Select(t => Weight(t)).ToArray();
            var parentInterval = Interval1D.Create(0, values.Length);

            var sut = new RegressionImpurityCalculator();

            sut.Init(new double[0], values, weights, parentInterval);

            var impurity = sut.NodeImpurity();

            sut.UpdateIndex(50);
            var actual = sut.LeafValue();

            Assert.AreEqual(1.75, actual, 0.000001);
        }
        public void RegressionImpurityCalculator_ChildImpurities()
        {
            var values = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, };

            var parentInterval = Interval1D.Create(0, values.Length);

            var sut = new RegressionImpurityCalculator();

            sut.Init(new double[0], values, new double[0], parentInterval);
            var impurity = sut.NodeImpurity();

            sut.UpdateIndex(50);
            var actual   = sut.ChildImpurities();
            var expected = new ChildImpurities(0.0, -2.25);

            Assert.AreEqual(expected, actual);
        }
        public void GiniClassificationImpurityCalculator_LeafValue_Weighted()
        {
            var values         = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, };
            var unique         = values.Distinct().ToArray();
            var weights        = values.Select(t => Weight(t)).ToArray();
            var parentInterval = Interval1D.Create(0, values.Length);

            var sut = new GiniClassificationImpurityCalculator();

            sut.Init(unique, values, weights, parentInterval);

            var impurity = sut.NodeImpurity();

            sut.UpdateIndex(50);
            var actual = sut.LeafValue();

            Assert.AreEqual(2.0, actual, 0.000001);
        }
        public void GiniClassificationImpurityCalculator_ChildImpurities()
        {
            var values = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, };
            var unique = values.Distinct().ToArray();

            var parentInterval = Interval1D.Create(0, values.Length);

            var sut = new GiniClassificationImpurityCalculator();

            sut.Init(unique, values, new double[0], parentInterval);

            var impurity = sut.NodeImpurity();

            sut.UpdateIndex(50);
            var actual   = sut.ChildImpurities();
            var expected = new ChildImpurities(0.0, .5);

            Assert.AreEqual(expected, actual);
        }
Exemple #28
0
        public void GiniImpurityMetric_Impurity_Interval()
        {
            var set1 = new double[] { 0, 1, 2, 3, 4, 3, 2, 1, 0 };
            var set2 = new double[] { 1, 1, 1, 1, 2, 2, 2, 2 };
            var set3 = new double[] { 1, 1, 1, 1, 1, 1, 1, 1 };

            var sut      = new GiniImpurityMetric();
            var interval = Interval1D.Create(2, 7);

            var val1 = sut.Impurity(set1, interval);

            Assert.AreEqual(0.64, val1);
            var val2 = sut.Impurity(set2, interval);

            Assert.AreEqual(0.48, val2);
            var val3 = sut.Impurity(set3, interval);

            Assert.AreEqual(0.0, val3);
        }
Exemple #29
0
        public void GiniImpurityMetric_Impurity_Weights_None()
        {
            var set1 = new double[] { 0, 1, 2, 3, 4, 3, 2, 1, 0 };
            var set2 = new double[] { 1, 1, 1, 1, 2, 2, 2, 2 };
            var set3 = new double[] { 1, 1, 1, 1, 1, 1, 1, 1 };

            var weights = new double[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 };
            var sut     = new GiniImpurityMetric();

            var val1 = sut.Impurity(set1, weights, Interval1D.Create(0, set1.Length));

            Assert.AreEqual(0.79012345679012341, val1);
            var val2 = sut.Impurity(set2, weights, Interval1D.Create(0, set2.Length));

            Assert.AreEqual(0.5, val2);
            var val3 = sut.Impurity(set3, weights, Interval1D.Create(0, set3.Length));

            Assert.AreEqual(0.0, val3);
        }
Exemple #30
0
        public void GiniImpurityMetric_Impurity_Weights()
        {
            var set1 = new double[] { 0, 1, 2, 3, 4, 3, 2, 1, 0 };
            var set2 = new double[] { 1, 1, 1, 1, 2, 2, 2, 2 };
            var set3 = new double[] { 1, 1, 1, 1, 1, 1, 1, 1 };

            var weights = new double[] { 3, 1, 4, 1, 0.5, 1, 2, 1, 10 };
            var sut     = new GiniImpurityMetric();

            var val1 = sut.Impurity(set1, weights, Interval1D.Create(0, set1.Length));

            Assert.AreEqual(0.96921684019918519, val1);
            var val2 = sut.Impurity(set2, weights, Interval1D.Create(0, set2.Length));

            Assert.AreEqual(0.82441700960219477, val2);
            var val3 = sut.Impurity(set3, weights, Interval1D.Create(0, set3.Length));

            Assert.AreEqual(0.64883401920438954, val3);
        }