Exemplo n.º 1
0
        public void multilabel_linear_new_usage()
        {
            #region doc_learn_ldcd
            // Let's say we have the following data to be classified
            // into three possible classes. Those are the samples:
            //
            double[][] inputs =
            {
                //               input         output
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 0, 0, 1, 0 }, //  0
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 1, 0, 0, 0 }, //  1
                new double[] { 1, 0, 0, 0 }, //  1
                new double[] { 1, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 1, 1, 1, 1 }, //  2
                new double[] { 1, 0, 1, 1 }, //  2
                new double[] { 1, 1, 0, 1 }, //  2
                new double[] { 0, 1, 1, 1 }, //  2
                new double[] { 1, 1, 1, 1 }, //  2
            };

            int[] outputs = // those are the class labels
            {
                0, 0, 0, 0, 0,
                1, 1, 1, 1, 1,
                2, 2, 2, 2, 2,
            };

            // Create a one-vs-one multi-class SVM learning algorithm
            var teacher = new MultilabelSupportVectorLearning <Linear>()
            {
                // using LIBLINEAR's L2-loss SVC dual for each SVM
                Learner = (p) => new LinearDualCoordinateDescent()
                {
                    Loss = Loss.L2
                }
            };

            // The following line is only needed to ensure reproducible results. Please remove it to enable full parallelization
            teacher.ParallelOptions.MaxDegreeOfParallelism = 1; // (Remove, comment, or change this line to enable full parallelism)

            // Learn a machine
            var machine = teacher.Learn(inputs, outputs);

            // Obtain class predictions for each sample
            bool[][] predicted = machine.Decide(inputs);

            // Compute classification error using mean accuracy (mAcc)
            double error = new HammingLoss(outputs).Loss(predicted);
            #endregion

            Assert.AreEqual(0, error);
            Assert.IsTrue(predicted.ArgMax(dimension: 1).IsEqual(outputs));
        }
Exemplo n.º 2
0
        public void multilabel_calibration_generic_kernel()
        {
            // Let's say we have the following data to be classified
            // into three possible classes. Those are the samples:
            //
            double[][] inputs =
            {
                //               input         output
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 0, 0, 1, 0 }, //  0
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 1, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 1, 0, 1, 1 }, //  2
                new double[] { 1, 1, 0, 1 }, //  2
                new double[] { 0, 1, 1, 1 }, //  2
                new double[] { 1, 1, 1, 1 }, //  2
            };

            int[] outputs = // those are the class labels
            {
                0, 0, 0, 0, 0,
                1, 1, 1,
                2, 2, 2, 2,
            };

            // Create the multi-class learning algorithm for the machine
            var teacher = new MultilabelSupportVectorLearning <IKernel>()
            {
                // Configure the learning algorithm to use SMO to train the
                //  underlying SVMs in each of the binary class subproblems.
                Learner = (param) => new SequentialMinimalOptimization <IKernel>()
                {
                    UseKernelEstimation = false,
                    Kernel = Gaussian.FromGamma(0.5)
                }
            };

            // Learn a machine
            var machine = teacher.Learn(inputs, outputs);


            // Create the multi-class learning algorithm for the machine
            var calibration = new MultilabelSupportVectorLearning <IKernel>(machine)
            {
                // Configure the learning algorithm to use SMO to train the
                //  underlying SVMs in each of the binary class subproblems.
                Learner = (p) => new ProbabilisticOutputCalibration <IKernel>(p.Model)
            };


            // Configure parallel execution options
            calibration.ParallelOptions.MaxDegreeOfParallelism = 1;

            // Learn a machine
            calibration.Learn(inputs, outputs);

            // Obtain class predictions for each sample
            bool[][] predicted = machine.Decide(inputs);

            // Get class scores for each sample
            double[][] scores = machine.Scores(inputs);

            // Get log-likelihoods (should be same as scores)
            double[][] logl = machine.LogLikelihoods(inputs);

            // Get probability for each sample
            double[][] prob = machine.Probabilities(inputs);

            // Compute classification error using mean accuracy (mAcc)
            double error = new HammingLoss(outputs).Loss(predicted);
            double loss  = new CategoryCrossEntropyLoss(outputs).Loss(prob);

            string a = scores.ToCSharp();
            string b = logl.ToCSharp();
            string c = prob.ToCSharp();

            double[][] expectedScores =
            {
                new double[] {  1.85316017783605, -2.59688389729331,  -2.32170102153988 },
                new double[] {  1.84933597524124, -1.99399145231446,   -2.2920299547693 },
                new double[] {  1.44477953581274, -1.98592298465108,  -2.27356092239125 },
                new double[] {  1.85316017783605, -2.59688389729331,  -2.32170102153988 },
                new double[] {  1.84933597524124, -1.99399145231446,   -2.2920299547693 },
                new double[] { -2.40815576360914, 0.328362962196791, -0.932721757919691 },
                new double[] { -2.13111157264226,    1.809192096031,   -2.2920299547693 },
                new double[] { -2.13111157264226,    1.809192096031,   -2.2920299547693 },
                new double[] { -2.14888646926108, -1.99399145231447,   1.33101148524982 },
                new double[] { -2.12915064678299, -1.98592298465108,    1.3242171079396 },
                new double[] { -1.47197826667149, -1.96368715704762,  0.843414180834243 },
                new double[] { -2.14221021749314, -2.83117892529093,   2.61354519154994 }
            };

            double[][] expectedLogL =
            {
                new double[] { -0.145606614365135,  -2.66874434442222,   -2.41528841111469 },
                new double[] { -0.146125659911391,  -2.12163759796483,    -2.3883043096263 },
                new double[] { -0.211716960454159,  -2.11453945718522,   -2.37154474995633 },
                new double[] { -0.145606614365135,  -2.66874434442222,   -2.41528841111469 },
                new double[] { -0.146125659911391,  -2.12163759796483,    -2.3883043096263 },
                new double[] {   -2.4943161092787, -0.542383360363463,   -1.26452689970624 },
                new double[] {  -2.24328358118314, -0.151678833375872,    -2.3883043096263 },
                new double[] {  -2.24328358118314, -0.151678833375872,    -2.3883043096263 },
                new double[] {  -2.25918730624753,  -2.12163759796483,  -0.234447327588685 },
                new double[] {  -2.24153091066541,  -2.11453945718522,    -0.2358711195715 },
                new double[] {  -1.67856232802554,   -2.0950136294762,  -0.357841632335707 },
                new double[] {  -2.25321037906455,  -2.88845047104229, -0.0707140798850236 }
            };

            double[][] expectedProbs =
            {
                new double[] {  0.844913862516144, 0.0677684640174953, 0.0873176734663607 },
                new double[] {  0.803266328757473,  0.111405242674824, 0.0853284285677024 },
                new double[] {  0.790831391595502,  0.117950175028754, 0.0912184333757438 },
                new double[] {  0.844913862516144, 0.0677684640174953, 0.0873176734663607 },
                new double[] {  0.803266328757473,  0.111405242674824, 0.0853284285677024 },
                new double[] { 0.0872387667998771,  0.614360294206236,  0.298400938993887 },
                new double[] {  0.100372339295793,  0.812805149315815, 0.0868225113883914 },
                new double[] {  0.100372339295793,  0.812805149315815, 0.0868225113883914 },
                new double[] {  0.102863726210119,   0.11803188195247,  0.779104391837411 },
                new double[] {  0.104532503226998,  0.118686968710368,  0.776780528062634 },
                new double[] {  0.184996665350572,  0.121983586443407,  0.693019748206021 },
                new double[] { 0.0961702585148881, 0.0509517983210315,   0.85287794316408 }
            };

            int[] actual = predicted.ArgMax(dimension: 1);
            Assert.IsTrue(actual.IsEqual(outputs));

            // Must be exactly the same as test above
            Assert.AreEqual(0, error);
            Assert.AreEqual(0.5, ((Gaussian)machine[0].Kernel).Gamma);
            Assert.AreEqual(0.5, ((Gaussian)machine[1].Kernel).Gamma);
            Assert.AreEqual(0.5, ((Gaussian)machine[2].Kernel).Gamma);
            Assert.AreEqual(2.9395943260892361, loss);
            Assert.IsTrue(expectedScores.IsEqual(scores, 1e-10));
            Assert.IsTrue(expectedLogL.IsEqual(logl, 1e-10));
            Assert.IsTrue(expectedProbs.IsEqual(prob, 1e-10));

            double[] probabilities = CorrectProbabilities(machine, inputs[0]);
            double[] actualProb    = machine.Probabilities(inputs[0]);
            Assert.IsTrue(probabilities.IsEqual(actualProb, 1e-8));
        }
Exemplo n.º 3
0
        public void multilabel_calibration()
        {
            #region doc_learn_calibration
            // Let's say we have the following data to be classified
            // into three possible classes. Those are the samples:
            //
            double[][] inputs =
            {
                //               input         output
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 0, 0, 1, 0 }, //  0
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 1, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 1, 0, 1, 1 }, //  2
                new double[] { 1, 1, 0, 1 }, //  2
                new double[] { 0, 1, 1, 1 }, //  2
                new double[] { 1, 1, 1, 1 }, //  2
            };

            int[] outputs = // those are the class labels
            {
                0, 0, 0, 0, 0,
                1, 1, 1,
                2, 2, 2, 2,
            };

            // Create the multi-class learning algorithm for the machine
            var teacher = new MultilabelSupportVectorLearning <Gaussian>()
            {
                // Configure the learning algorithm to use SMO to train the
                //  underlying SVMs in each of the binary class subproblems.
                Learner = (param) => new SequentialMinimalOptimization <Gaussian>()
                {
                    // Estimate a suitable guess for the Gaussian kernel's parameters.
                    // This estimate can serve as a starting point for a grid search.
                    UseKernelEstimation = true
                }
            };

            // Learn a machine
            var machine = teacher.Learn(inputs, outputs);

            // Create the multi-class learning algorithm for the machine
            var calibration = new MultilabelSupportVectorLearning <Gaussian>()
            {
                Model = machine, // We will start with an existing machine

                // Configure the learning algorithm to use SMO to train the
                //  underlying SVMs in each of the binary class subproblems.
                Learner = (param) => new ProbabilisticOutputCalibration <Gaussian>()
                {
                    Model = param.Model // Start with an existing machine
                }
            };


            // Configure parallel execution options
            calibration.ParallelOptions.MaxDegreeOfParallelism = 1;

            // Learn a machine
            calibration.Learn(inputs, outputs);

            // Obtain class predictions for each sample
            bool[][] predicted = machine.Decide(inputs);

            // Get class scores for each sample
            double[][] scores = machine.Scores(inputs);

            // Get log-likelihoods (should be same as scores)
            double[][] logl = machine.LogLikelihoods(inputs);

            // Get probability for each sample
            double[][] prob = machine.Probabilities(inputs);

            // Compute classification error using mean accuracy (mAcc)
            double error = new HammingLoss(outputs).Loss(predicted);
            double loss  = new CategoryCrossEntropyLoss(outputs).Loss(prob);
            #endregion

            string a = scores.ToCSharp();
            string b = logl.ToCSharp();
            string c = prob.ToCSharp();

            double[][] expectedScores =
            {
                new double[] {  1.85316017783605, -2.59688389729331,  -2.32170102153988 },
                new double[] {  1.84933597524124, -1.99399145231446,   -2.2920299547693 },
                new double[] {  1.44477953581274, -1.98592298465108,  -2.27356092239125 },
                new double[] {  1.85316017783605, -2.59688389729331,  -2.32170102153988 },
                new double[] {  1.84933597524124, -1.99399145231446,   -2.2920299547693 },
                new double[] { -2.40815576360914, 0.328362962196791, -0.932721757919691 },
                new double[] { -2.13111157264226,    1.809192096031,   -2.2920299547693 },
                new double[] { -2.13111157264226,    1.809192096031,   -2.2920299547693 },
                new double[] { -2.14888646926108, -1.99399145231447,   1.33101148524982 },
                new double[] { -2.12915064678299, -1.98592298465108,    1.3242171079396 },
                new double[] { -1.47197826667149, -1.96368715704762,  0.843414180834243 },
                new double[] { -2.14221021749314, -2.83117892529093,   2.61354519154994 }
            };

            double[][] expectedLogL =
            {
                new double[] { -0.145606614365135,  -2.66874434442222,   -2.41528841111469 },
                new double[] { -0.146125659911391,  -2.12163759796483,    -2.3883043096263 },
                new double[] { -0.211716960454159,  -2.11453945718522,   -2.37154474995633 },
                new double[] { -0.145606614365135,  -2.66874434442222,   -2.41528841111469 },
                new double[] { -0.146125659911391,  -2.12163759796483,    -2.3883043096263 },
                new double[] {   -2.4943161092787, -0.542383360363463,   -1.26452689970624 },
                new double[] {  -2.24328358118314, -0.151678833375872,    -2.3883043096263 },
                new double[] {  -2.24328358118314, -0.151678833375872,    -2.3883043096263 },
                new double[] {  -2.25918730624753,  -2.12163759796483,  -0.234447327588685 },
                new double[] {  -2.24153091066541,  -2.11453945718522,    -0.2358711195715 },
                new double[] {  -1.67856232802554,   -2.0950136294762,  -0.357841632335707 },
                new double[] {  -2.25321037906455,  -2.88845047104229, -0.0707140798850236 }
            };

            double[][] expectedProbs =
            {
                new double[] {  0.844913862516144, 0.0677684640174953, 0.0873176734663607 },
                new double[] {  0.803266328757473,  0.111405242674824, 0.0853284285677024 },
                new double[] {  0.790831391595502,  0.117950175028754, 0.0912184333757438 },
                new double[] {  0.844913862516144, 0.0677684640174953, 0.0873176734663607 },
                new double[] {  0.803266328757473,  0.111405242674824, 0.0853284285677024 },
                new double[] { 0.0872387667998771,  0.614360294206236,  0.298400938993887 },
                new double[] {  0.100372339295793,  0.812805149315815, 0.0868225113883914 },
                new double[] {  0.100372339295793,  0.812805149315815, 0.0868225113883914 },
                new double[] {  0.102863726210119,   0.11803188195247,  0.779104391837411 },
                new double[] {  0.104532503226998,  0.118686968710368,  0.776780528062634 },
                new double[] {  0.184996665350572,  0.121983586443407,  0.693019748206021 },
                new double[] { 0.0961702585148881, 0.0509517983210315,   0.85287794316408 }
            };

            int[] actual = predicted.ArgMax(dimension: 1);
            Assert.IsTrue(actual.IsEqual(outputs));
            Assert.AreEqual(0, error);
            Assert.AreEqual(3, machine.Count);
            Assert.AreEqual(0.5, machine[0].Kernel.Gamma);
            Assert.AreEqual(0.5, machine[1].Kernel.Gamma);
            Assert.AreEqual(0.5, machine[2].Kernel.Gamma);
            Assert.AreEqual(2.9395943260892361, loss);
            Assert.IsTrue(expectedScores.IsEqual(scores, 1e-10));
            Assert.IsTrue(expectedLogL.IsEqual(logl, 1e-10));
            Assert.IsTrue(expectedProbs.IsEqual(prob, 1e-10));
            double[] rowSums = expectedProbs.Sum(1);
            Assert.IsTrue(rowSums.IsEqual(Vector.Ones(expectedProbs.Length), 1e-10));

            {
                bool[][]   predicted2 = null;
                double[][] scores2    = machine.Scores(inputs, ref predicted2);
                Assert.IsTrue(scores2.IsEqual(scores));
                Assert.IsTrue(predicted2.IsEqual(predicted));

                double[][] logl2 = machine.LogLikelihoods(inputs, ref predicted2);
                Assert.IsTrue(logl2.IsEqual(logl));
                Assert.IsTrue(predicted2.IsEqual(predicted));

                double[][] prob2 = machine.Probabilities(inputs, ref predicted2);
                Assert.IsTrue(prob2.IsEqual(prob));
                Assert.IsTrue(predicted2.IsEqual(predicted));

                bool[][]   predicted3 = new bool[predicted2.Length][];
                double[][] scores3    = inputs.ApplyWithIndex((x, i) => machine.Scores(x, ref predicted3[i]));
                Assert.IsTrue(scores3.IsEqual(scores));
                Assert.IsTrue(predicted3.IsEqual(predicted));

                double[][] logl3 = inputs.ApplyWithIndex((x, i) => machine.LogLikelihoods(x, ref predicted3[i]));
                Assert.IsTrue(logl3.IsEqual(logl));
                Assert.IsTrue(predicted3.IsEqual(predicted));

                double[][] prob3 = inputs.ApplyWithIndex((x, i) => machine.Probabilities(x, ref predicted3[i]));
                Assert.IsTrue(prob3.IsEqual(prob));
                Assert.IsTrue(predicted3.IsEqual(predicted));
            }

            {
                double[] ed = new double[scores.Length];
                double[] es = new double[scores.Length];
                double[] el = new double[scores.Length];
                double[] ep = new double[scores.Length];
                for (int i = 0; i < expectedScores.Length; i++)
                {
                    int j = scores[i].ArgMax();
                    ed[i] = j;
                    es[i] = scores[i][j];
                    el[i] = logl[i][j];
                    ep[i] = prob[i][j];
                }

                int[]    predicted2 = null;
                double[] scores2    = machine.ToMulticlass().Score(inputs, ref predicted2);
                Assert.IsTrue(scores2.IsEqual(es));
                Assert.IsTrue(predicted2.IsEqual(ed));

                double[] logl2 = machine.ToMulticlass().LogLikelihood(inputs, ref predicted2);
                Assert.IsTrue(logl2.IsEqual(el));
                Assert.IsTrue(predicted2.IsEqual(ed));

                double[] prob2 = machine.ToMulticlass().Probability(inputs, ref predicted2);
                Assert.IsTrue(prob2.IsEqual(ep));
                Assert.IsTrue(predicted2.IsEqual(ed));

                int[]    predicted3 = new int[predicted2.Length];
                double[] scores3    = inputs.ApplyWithIndex((x, i) => machine.ToMulticlass().Score(x, out predicted3[i]));
                Assert.IsTrue(scores3.IsEqual(es));
                Assert.IsTrue(predicted3.IsEqual(ed));

                double[] logl3 = inputs.ApplyWithIndex((x, i) => machine.ToMulticlass().LogLikelihood(x, out predicted3[i]));
                Assert.IsTrue(logl3.IsEqual(el));
                Assert.IsTrue(predicted3.IsEqual(ed));

                double[] prob3 = inputs.ApplyWithIndex((x, i) => machine.ToMulticlass().Probability(x, out predicted3[i]));
                Assert.IsTrue(prob3.IsEqual(ep));
                Assert.IsTrue(predicted3.IsEqual(ed));
            }
        }
        public void multilabel_calibration_generic_kernel()
        {
            // Let's say we have the following data to be classified
            // into three possible classes. Those are the samples:
            //
            double[][] inputs =
            {
                //               input         output
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 0, 0, 1, 0 }, //  0
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 1, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 1, 0, 1, 1 }, //  2
                new double[] { 1, 1, 0, 1 }, //  2
                new double[] { 0, 1, 1, 1 }, //  2
                new double[] { 1, 1, 1, 1 }, //  2
            };

            int[] outputs = // those are the class labels
            {
                0, 0, 0, 0, 0,
                1, 1, 1,
                2, 2, 2, 2,
            };

            // Create the multi-class learning algorithm for the machine
            var teacher = new MultilabelSupportVectorLearning <IKernel>()
            {
                // Configure the learning algorithm to use SMO to train the
                //  underlying SVMs in each of the binary class subproblems.
                Learner = (param) => new SequentialMinimalOptimization <IKernel>()
                {
                    UseKernelEstimation = false,
                    Kernel = Gaussian.FromGamma(0.5)
                }
            };

            // Learn a machine
            var machine = teacher.Learn(inputs, outputs);


            // Create the multi-class learning algorithm for the machine
            var calibration = new MultilabelSupportVectorLearning <IKernel>(machine)
            {
                // Configure the learning algorithm to use SMO to train the
                //  underlying SVMs in each of the binary class subproblems.
                Learner = (p) => new ProbabilisticOutputCalibration <IKernel>(p.Model)
            };


            // Configure parallel execution options
            calibration.ParallelOptions.MaxDegreeOfParallelism = 1;

            // Learn a machine
            calibration.Learn(inputs, outputs);

            // Obtain class predictions for each sample
            bool[][] predicted = machine.Decide(inputs);

            // Get class scores for each sample
            double[][] scores = machine.Scores(inputs);

            // Get log-likelihoods (should be same as scores)
            double[][] logl = machine.LogLikelihoods(inputs);

            // Get probability for each sample
            double[][] prob = machine.Probabilities(inputs);

            // Compute classification error using mean accuracy (mAcc)
            double error = new HammingLoss(outputs).Loss(predicted);
            double loss  = new CategoryCrossEntropyLoss(outputs).Loss(prob);

            string a = scores.ToCSharp();
            string b = logl.ToCSharp();
            string c = prob.ToCSharp();

            double[][] expectedScores =
            {
                new double[] {  1.85316017783605, -2.59688389729331,  -2.32170102153988 },
                new double[] {  1.84933597524124, -1.99399145231446,   -2.2920299547693 },
                new double[] {  1.44477953581274, -1.98592298465108,  -2.27356092239125 },
                new double[] {  1.85316017783605, -2.59688389729331,  -2.32170102153988 },
                new double[] {  1.84933597524124, -1.99399145231446,   -2.2920299547693 },
                new double[] { -2.40815576360914, 0.328362962196791, -0.932721757919691 },
                new double[] { -2.13111157264226,    1.809192096031,   -2.2920299547693 },
                new double[] { -2.13111157264226,    1.809192096031,   -2.2920299547693 },
                new double[] { -2.14888646926108, -1.99399145231447,   1.33101148524982 },
                new double[] { -2.12915064678299, -1.98592298465108,    1.3242171079396 },
                new double[] { -1.47197826667149, -1.96368715704762,  0.843414180834243 },
                new double[] { -2.14221021749314, -2.83117892529093,   2.61354519154994 }
            };

            double[][] expectedLogL =
            {
                new double[] {  1.85316017783605, -2.59688389729331,  -2.32170102153988 },
                new double[] {  1.84933597524124, -1.99399145231446,   -2.2920299547693 },
                new double[] {  1.44477953581274, -1.98592298465108,  -2.27356092239125 },
                new double[] {  1.85316017783605, -2.59688389729331,  -2.32170102153988 },
                new double[] {  1.84933597524124, -1.99399145231446,   -2.2920299547693 },
                new double[] { -2.40815576360914, 0.328362962196791, -0.932721757919691 },
                new double[] { -2.13111157264226,    1.809192096031,   -2.2920299547693 },
                new double[] { -2.13111157264226,    1.809192096031,   -2.2920299547693 },
                new double[] { -2.14888646926108, -1.99399145231447,   1.33101148524982 },
                new double[] { -2.12915064678299, -1.98592298465108,    1.3242171079396 },
                new double[] { -1.47197826667149, -1.96368715704762,  0.843414180834243 },
                new double[] { -2.14221021749314, -2.83117892529093,   2.61354519154994 }
            };

            double[][] expectedProbs =
            {
                new double[] {   6.37994947365835, 0.0745053832890827, 0.0981065622139132 },
                new double[] {   6.35559784678136,  0.136150899620619,  0.101061104020747 },
                new double[] {   4.24091706941419,  0.137253872418087,  0.102944947658882 },
                new double[] {   6.37994947365835, 0.0745053832890827, 0.0981065622139132 },
                new double[] {   6.35559784678136,  0.136150899620619,  0.101061104020747 },
                new double[] { 0.0899810880411361,   1.38869292386051,  0.393481290780948 },
                new double[] {  0.118705270957796,   6.10551277113228,  0.101061104020747 },
                new double[] {  0.118705270957796,   6.10551277113228,  0.101061104020747 },
                new double[] {  0.116613938707895,  0.136150899620619,   3.78486979203385 },
                new double[] {  0.118938271567046,  0.137253872418087,   3.75924112261421 },
                new double[] {  0.229471080877097,  0.140340010119971,    2.3242889884131 },
                new double[] {   0.11739508739354, 0.0589433229176013,   13.6473476521179 }
            };

            int[] actual = predicted.ArgMax(dimension: 1);
            Assert.IsTrue(actual.IsEqual(outputs));

            // Must be exactly the same as test above
            Assert.AreEqual(0, error);
            Assert.AreEqual(0.5, ((Gaussian)machine[0].Kernel).Gamma);
            Assert.AreEqual(0.5, ((Gaussian)machine[1].Kernel).Gamma);
            Assert.AreEqual(0.5, ((Gaussian)machine[2].Kernel).Gamma);
            Assert.AreEqual(-18.908706961799737, loss);
            Assert.IsTrue(expectedScores.IsEqual(scores, 1e-10));
            Assert.IsTrue(expectedLogL.IsEqual(logl, 1e-10));
            Assert.IsTrue(expectedProbs.IsEqual(prob, 1e-10));
        }
        public void multilabel_calibration_generic_kernel()
        {
            // Let's say we have the following data to be classified
            // into three possible classes. Those are the samples:
            //
            double[][] inputs =
            {
                //               input         output
                new double[] { 0, 1, 1, 0 }, //  0 
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 0, 0, 1, 0 }, //  0
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 1, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 1, 0, 1, 1 }, //  2
                new double[] { 1, 1, 0, 1 }, //  2
                new double[] { 0, 1, 1, 1 }, //  2
                new double[] { 1, 1, 1, 1 }, //  2
            };

            int[] outputs = // those are the class labels
            {
                0, 0, 0, 0, 0,
                1, 1, 1, 
                2, 2, 2, 2, 
            };

            // Create the multi-class learning algorithm for the machine
            var teacher = new MultilabelSupportVectorLearning<IKernel>()
            {
                // Configure the learning algorithm to use SMO to train the
                //  underlying SVMs in each of the binary class subproblems.
                Learner = (param) => new SequentialMinimalOptimization<IKernel>()
                {
                    UseKernelEstimation = false,
                    Kernel = Gaussian.FromGamma(0.5)
                }
            };

            // Learn a machine
            var machine = teacher.Learn(inputs, outputs);


            // Create the multi-class learning algorithm for the machine
            var calibration = new MultilabelSupportVectorLearning<IKernel>(machine)
            {
                // Configure the learning algorithm to use SMO to train the
                //  underlying SVMs in each of the binary class subproblems.
                Learner = (p) => new ProbabilisticOutputCalibration<IKernel>(p.Model)
            };


            // Configure parallel execution options
            calibration.ParallelOptions.MaxDegreeOfParallelism = 1;

            // Learn a machine
            calibration.Learn(inputs, outputs);

            // Obtain class predictions for each sample
            bool[][] predicted = machine.Decide(inputs);

            // Get class scores for each sample
            double[][] scores = machine.Scores(inputs);

            // Get log-likelihoods (should be same as scores)
            double[][] logl = machine.LogLikelihoods(inputs);

            // Get probability for each sample
            double[][] prob = machine.Probabilities(inputs);

            // Compute classification error using mean accuracy (mAcc)
            double error = new HammingLoss(outputs).Loss(predicted);
            double loss = new CategoryCrossEntropyLoss(outputs).Loss(prob);

            string a = scores.ToCSharp();
            string b = logl.ToCSharp();
            string c = prob.ToCSharp();

            double[][] expectedScores =
            {
                new double[] { 1.85316017783605, -2.59688389729331, -2.32170102153988 },
                new double[] { 1.84933597524124, -1.99399145231446, -2.2920299547693 },
                new double[] { 1.44477953581274, -1.98592298465108, -2.27356092239125 },
                new double[] { 1.85316017783605, -2.59688389729331, -2.32170102153988 },
                new double[] { 1.84933597524124, -1.99399145231446, -2.2920299547693 },
                new double[] { -2.40815576360914, 0.328362962196791, -0.932721757919691 },
                new double[] { -2.13111157264226, 1.809192096031, -2.2920299547693 },
                new double[] { -2.13111157264226, 1.809192096031, -2.2920299547693 },
                new double[] { -2.14888646926108, -1.99399145231447, 1.33101148524982 },
                new double[] { -2.12915064678299, -1.98592298465108, 1.3242171079396 },
                new double[] { -1.47197826667149, -1.96368715704762, 0.843414180834243 },
                new double[] { -2.14221021749314, -2.83117892529093, 2.61354519154994 } 
            };

            double[][] expectedLogL =
            {
                new double[] { 1.85316017783605, -2.59688389729331, -2.32170102153988 },
                new double[] { 1.84933597524124, -1.99399145231446, -2.2920299547693 },
                new double[] { 1.44477953581274, -1.98592298465108, -2.27356092239125 },
                new double[] { 1.85316017783605, -2.59688389729331, -2.32170102153988 },
                new double[] { 1.84933597524124, -1.99399145231446, -2.2920299547693 },
                new double[] { -2.40815576360914, 0.328362962196791, -0.932721757919691 },
                new double[] { -2.13111157264226, 1.809192096031, -2.2920299547693 },
                new double[] { -2.13111157264226, 1.809192096031, -2.2920299547693 },
                new double[] { -2.14888646926108, -1.99399145231447, 1.33101148524982 },
                new double[] { -2.12915064678299, -1.98592298465108, 1.3242171079396 },
                new double[] { -1.47197826667149, -1.96368715704762, 0.843414180834243 },
                new double[] { -2.14221021749314, -2.83117892529093, 2.61354519154994 } 
            };

            double[][] expectedProbs =
            {
                new double[] { 6.37994947365835, 0.0745053832890827, 0.0981065622139132 },
                new double[] { 6.35559784678136, 0.136150899620619, 0.101061104020747 },
                new double[] { 4.24091706941419, 0.137253872418087, 0.102944947658882 },
                new double[] { 6.37994947365835, 0.0745053832890827, 0.0981065622139132 },
                new double[] { 6.35559784678136, 0.136150899620619, 0.101061104020747 },
                new double[] { 0.0899810880411361, 1.38869292386051, 0.393481290780948 },
                new double[] { 0.118705270957796, 6.10551277113228, 0.101061104020747 },
                new double[] { 0.118705270957796, 6.10551277113228, 0.101061104020747 },
                new double[] { 0.116613938707895, 0.136150899620619, 3.78486979203385 },
                new double[] { 0.118938271567046, 0.137253872418087, 3.75924112261421 },
                new double[] { 0.229471080877097, 0.140340010119971, 2.3242889884131 },
                new double[] { 0.11739508739354, 0.0589433229176013, 13.6473476521179 }             };

            int[] actual = predicted.ArgMax(dimension: 1);
            Assert.IsTrue(actual.IsEqual(outputs));

            // Must be exactly the same as test above
            Assert.AreEqual(0, error);
            Assert.AreEqual(0.5, ((Gaussian)machine[0].Kernel).Gamma);
            Assert.AreEqual(0.5, ((Gaussian)machine[1].Kernel).Gamma);
            Assert.AreEqual(0.5, ((Gaussian)machine[2].Kernel).Gamma);
            Assert.AreEqual(-18.908706961799737, loss);
            Assert.IsTrue(expectedScores.IsEqual(scores, 1e-10));
            Assert.IsTrue(expectedLogL.IsEqual(logl, 1e-10));
            Assert.IsTrue(expectedProbs.IsEqual(prob, 1e-10));
        }
        public void multilabel_linear_new_usage()
        {
            #region doc_learn_ldcd
            // Let's say we have the following data to be classified
            // into three possible classes. Those are the samples:
            //
            double[][] inputs =
            {
                //               input         output
                new double[] { 0, 1, 1, 0 }, //  0 
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 0, 0, 1, 0 }, //  0
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 1, 0, 0, 0 }, //  1
                new double[] { 1, 0, 0, 0 }, //  1
                new double[] { 1, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 1, 1, 1, 1 }, //  2
                new double[] { 1, 0, 1, 1 }, //  2
                new double[] { 1, 1, 0, 1 }, //  2
                new double[] { 0, 1, 1, 1 }, //  2
                new double[] { 1, 1, 1, 1 }, //  2
            };

            int[] outputs = // those are the class labels
            {
                0, 0, 0, 0, 0,
                1, 1, 1, 1, 1,
                2, 2, 2, 2, 2,
            };

            // Create a one-vs-one multi-class SVM learning algorithm 
            var teacher = new MultilabelSupportVectorLearning<Linear>()
            {
                // using LIBLINEAR's L2-loss SVC dual for each SVM
                Learner = (p) => new LinearDualCoordinateDescent()
                {
                    Loss = Loss.L2
                }
            };

            // Configure parallel execution options
            teacher.ParallelOptions.MaxDegreeOfParallelism = 1;

            // Learn a machine
            var machine = teacher.Learn(inputs, outputs);

            // Obtain class predictions for each sample
            bool[][] predicted = machine.Decide(inputs);

            // Compute classification error using mean accuracy (mAcc)
            double error = new HammingLoss(outputs).Loss(predicted);
            #endregion

            Assert.AreEqual(0, error);
            Assert.IsTrue(predicted.ArgMax(dimension:1 ).IsEqual(outputs));
        }
Exemplo n.º 7
0
        private List <Colaborador> metodoquedefineoscolaboradores(int pGerencia, int pClassificacao, int pDificuldade, int pPrioridade, int pTipoFalha)
        {
            string    filename = "C:/Users/Oscar/Downloads/solics-completo.csv";
            CsvReader reader   = new CsvReader(filename, hasHeaders: true);

            DataTable table = reader.ToTable();

            string[] inputNames  = new[] { "Id", "Gerencia", "Cliente", "Sistema", "Modulo", "Classificacao", "TipoFalha", "Prioridade", "Dificuldade" };
            string[] outputNames = new[] { "Colaborador" };

            /*var codification1 = new Codification()
             * {
             *  { "Id", CodificationVariable.Ordinal},
             *  { "Gerencia", CodificationVariable.Discrete },
             *  { "Cliente", CodificationVariable.Discrete },
             *  { "Sistema", CodificationVariable.Discrete },
             *  { "Modulo", CodificationVariable.Discrete },
             *  { "Classificacao", CodificationVariable.Discrete},
             *  { "TipoFalha", CodificationVariable.Discrete },
             *  { "Prioridade", CodificationVariable.Discrete },
             *  { "Dificuldade", CodificationVariable.Discrete },
             * };
             *
             * var codification2 = new Codification()
             * {
             *  DefaultMissingValueReplacement = Double.NaN
             * };
             *
             * codification1.Learn(table);
             * codification2.Learn(table);
             *
             */

            Codification codebook = new Codification(table);


            //DataTable symbols = codification.Apply(table);
            //int[] outputis = symbols.ToArray<int>("COLABORADOR");

            // Now, transform symbols into a vector representation, growing the number of inputs:
            //double[][] x = codification.Transform(table, inputNames, out inputNames).ToDouble();
            int[][] x = codebook.Apply(table, "Id", "Gerencia", "Cliente", "Sistema", "Modulo", "Classificacao", "TipoFalha", "Prioridade", "Dificuldade").ToJagged(out inputNames).ToInt32();
            //double[][] y = codification.Transform(table, outputNames, out outputNames).ToDouble();
            //int[] y = codification.Apply(table, "COLABORADOR").ToJagged(out outputName);

            string outputName;    // can see below the new variable names that will be generated)

            int[] outputs = codebook.Apply(table, "Colaborador").ToVector(out outputName).ToInt32();



            /*
             * // Create the multi-class learning algorithm for the machine
             * var teacher = new MultilabelSupportVectorLearning<Gaussian>()
             * {
             *  // Configure the learning algorithm to use SMO to train the
             *  //  underlying SVMs in each of the binary class subproblems.
             *  Learner = (param) => new SequentialMinimalOptimization<Gaussian>()
             *  {
             *      // Estimate a suitable guess for the Gaussian kernel's parameters.
             *      // This estimate can serve as a starting point for a grid search.
             *      UseKernelEstimation = true
             *  }
             * };
             *
             * // Learn a machine
             * var machine = teacher.Learn(x, outputs);
             *
             * // Create the multi-class learning algorithm for the machine
             * var calibration = new MultilabelSupportVectorLearning<Gaussian>()
             * {
             *  Model = machine, // We will start with an existing machine
             *
             *  // Configure the learning algorithm to use SMO to train the
             *  //  underlying SVMs in each of the binary class subproblems.
             *  Learner = (param) => new ProbabilisticOutputCalibration<Gaussian>()
             *  {
             *      Model = param.Model // Start with an existing machine
             *  }
             * };
             *
             *
             * // Configure parallel execution options
             * calibration.ParallelOptions.MaxDegreeOfParallelism = 1;
             *
             * // Learn a machine
             * calibration.Learn(x, outputs);
             *
             * // Obtain class predictions for each sample
             * bool[][] predicted = machine.Decide(x);
             *
             * // Get class scores for each sample
             * double[][] scores = machine.Scores(x);
             *
             * // Get log-likelihoods (should be same as scores)
             * double[][] logl = machine.LogLikelihoods(x);
             *
             * // Get probability for each sample
             * double[][] prob = machine.Probabilities(x);
             *
             * // Compute classification error using mean accuracy (mAcc)
             * double error = new HammingLoss(outputs).Loss(predicted);
             * double loss = new CategoryCrossEntropyLoss(outputs).Loss(prob);*/

            Accord.Math.Random.Generator.Seed = 1;
            DecisionVariable[] Attributes = DecisionVariable.FromCodebook(codebook, inputNames);

            // Now, let's create the forest learning algorithm
            var teacher = new RandomForestLearning(Attributes)
            {
                NumberOfTrees = 1,
                SampleRatio   = 1.0
            };

            // Finally, learn a random forest from data
            var forest = teacher.Learn(x, outputs);

            // We can estimate class labels using
            int[] predicted = forest.Decide(x);

            // And the classification error (0) can be computed as
            double error = new ZeroOneLoss(outputs).Loss(forest.Decide(x));

            // Compute classification error using mean accuracy (mAcc)
            double error2 = new HammingLoss(outputs).Loss(predicted);

            List <Colaborador> lLista = new List <Colaborador>();

            //lLista.AddRange(db.Colaborador.Where(x => x.Nome.StartsWith("J")).OrderBy(x => x.Nome));
            return(lLista);
        }
Exemplo n.º 8
0
        private Tuple <MulticlassSupportVectorMachine <Gaussian>, double, double, double> Training(List <double[]> inputsList, List <int> outputsList)
        {
            /*var gridsearch = new GridSearch<MulticlassSupportVectorMachine<Gaussian>, double[], int>()
             * {
             *  // Here we can specify the range of the parameters to be included in the search
             *  ParameterRanges = new GridSearchRangeCollection()
             *  {
             *      new GridSearchRange("Complexity", new double[]{Math.Pow(2, -10), Math.Pow(2, -8),
             *          Math.Pow(2, -6), Math.Pow(2, -4), Math.Pow(2, -2), Math.Pow(2, 0), Math.Pow(2, 2),
             *          Math.Pow(2, 4), Math.Pow(2, 6), Math.Pow(2, 8), Math.Pow(2, 10)}),
             *      new GridSearchRange("Gamma", new double[]{Math.Pow(2, -10), Math.Pow(2, -8),
             *          Math.Pow(2, -6), Math.Pow(2, -4), Math.Pow(2, -2), Math.Pow(2, 0), Math.Pow(2, 2),
             *          Math.Pow(2, 4), Math.Pow(2, 6), Math.Pow(2, 8), Math.Pow(2, 10)})
             *  },
             *
             *  // Indicate how learning algorithms for the models should be created
             *  Learner = (p) => new MulticlassSupportVectorLearning<Gaussian>()
             *  {
             *      // Configure the learning algorithm to use SMO to train the
             *      //  underlying SVMs in each of the binary class subproblems.
             *      Learner = (param) => new SequentialMinimalOptimization<Gaussian>()
             *      {
             *          // Estimate a suitable guess for the Gaussian kernel's parameters.
             *          // This estimate can serve as a starting point for a grid search.
             *          UseComplexityHeuristic = true,
             *          UseKernelEstimation = true
             *          //Complexity = p["Complexity"],
             *          //Kernel = Gaussian.FromGamma(p["Gamma"])
             *      }
             *  },
             *  // Define how the model should be learned, if needed
             *  Fit = (teacher, x, y, w) => teacher.Learn(x, y, w),
             *
             *  // Define how the performance of the models should be measured
             *  Loss = (actual, expected, m) =>
             *  {
             *      double totalError = 0;
             *      foreach (var input in _originalInputsList)
             *      {
             *          if (!m.Decide(input.Item1).Equals(input.Item2))
             *          {
             *              totalError++;
             *          }
             *      }
             *      return totalError / _originalInputsList.Count;
             *  }
             * };*/
            var Learner = new MulticlassSupportVectorLearning <Gaussian>()
            {
                // Configure the learning algorithm to use SMO to train the
                //  underlying SVMs in each of the binary class subproblems.
                Learner = (param) => new SequentialMinimalOptimization <Gaussian>()
                {
                    // Estimate a suitable guess for the Gaussian kernel's parameters.
                    // This estimate can serve as a starting point for a grid search.
                    UseComplexityHeuristic = true,
                    UseKernelEstimation    = true
                                             //Complexity = p["Complexity"],
                                             //Kernel = Gaussian.FromGamma(p["Gamma"])
                }
            };



            Learner.ParallelOptions.MaxDegreeOfParallelism = _paralelism;
            var model = Learner.Learn(inputsList.ToArray(), outputsList.ToArray());

            Console.WriteLine("y nos ponemos a aprender");
            // Search for the best model parameters
            //var result = gridsearch.Learn(inputsList.ToArray(), outputsList.ToArray());
            //Console.WriteLine("Error modelo: " + result.BestModelError);

            //var model = result.BestModel;
            double gamma = model.Kernel.Gamma;

            int[]  predicted = model.Decide(_originputsList.ToArray());
            double error     = new HammingLoss(outputsList.ToArray()).Loss(predicted);

            /*foreach (var input in _originalInputsList)
             * {
             *  if (!model.Decide(input.Item1).Equals(input.Item2))
             *  {
             *      error++;
             *  }
             * }
             * error = error / (double)_originalInputsList.Count;*/

            return(new Tuple <MulticlassSupportVectorMachine <Gaussian>, double, double, double>(model, error, 0, gamma));
        }