コード例 #1
0
        private void DoPrune()
        {
            var isContinuous = (Labels.ValueCount(0) < 2);

            for (var row = 0; row < Features.Rows(); row++)
            {
                var nearest = FindKnn(Features.Row(row), new int[1] {
                    row
                });
                var    with    = 0;
                double withSSE = 0;
                var    pred    = new double[1];

                for (var n = 0; n < nearest.Length; n++)
                {
                    pred[0] = 0;
                    var targ = Labels.Row(nearest[n]);
                    GetOutput(FindKnn(Features.Row(nearest[n]), new int[1] {
                        nearest[n]
                    }), pred);
                    if (isContinuous)
                    {
                        var delta = targ[0] - pred[0];
                        withSSE += (delta * delta);
                    }
                    else if (pred[0] == targ[0])
                    {
                        with++;
                    }
                }

                var    without    = 0;
                double withoutSSE = 0;
                for (var n = 0; n < nearest.Length; n++)
                {
                    pred[0] = 0;
                    var targ = Labels.Row(nearest[n]);
                    GetOutput(FindKnn(Features.Row(nearest[n]), new int[2] {
                        row, nearest[n]
                    }), pred);
                    if (isContinuous)
                    {
                        var delta = targ[0] - pred[0];
                        withoutSSE += (delta * delta);
                    }
                    else if (pred[0] == targ[0])
                    {
                        without++;
                    }
                }


                var remove = false;

                if (isContinuous)
                {
                    withSSE    = Math.Sqrt(withSSE / K);
                    withoutSSE = Math.Sqrt(withoutSSE / K);
                    if (Math.Abs(withSSE - withoutSSE) <= (0.1 * withSSE))
                    {
                        remove = true;
                    }
                }
                else if (without - with >= 0)
                {
                    remove = true;
                }

                if (remove)
                {
                    // remove the row
                    Features.Delete(row);
                    Labels.Delete(row);
                    row--;
                }
            }

            Console.WriteLine();
            Console.WriteLine("After pruning:");
            Console.WriteLine("Number of instances: " + Features.Rows());
        }