示例#1
0
        public double getPredictionErrorFromData(double[][] input, double[][] output)
        {
            double[] buyOutput  = new double[output.Length];
            double[] sellOutput = new double[output.Length];

            for (int i = 0; i < output.Length; i++)
            {
                buyOutput[i]  = output[i][0];
                sellOutput[i] = output[i][1];
            }

            return((teacherBuy.ComputeError(input, buyOutput) + teacherSell.ComputeError(input, sellOutput)) / 2);
        }
        public void RegularizationTest2()
        {
            CsvReader reader = CsvReader.FromText(Properties.Resources.regression, true);

            double[][] data = reader.ToTable().ToArray(System.Globalization.CultureInfo.InvariantCulture);

            double[][] inputs = data.GetColumns(new[] { 0, 1 });

            double[] output = data.GetColumn(2);

            var regression = new LogisticRegression(2);
            var irls       = new IterativeReweightedLeastSquares(regression);

            double error    = irls.Run(inputs, output);
            double newError = 0;

            for (int i = 0; i < 50; i++)
            {
                newError = irls.Run(inputs, output);
            }

            double actual = irls.ComputeError(inputs, output);

            Assert.AreEqual(30.507262964894068, actual, 1e-8);

            Assert.AreEqual(3, regression.Coefficients.Length);
            Assert.AreEqual(-0.38409721299838279, regression.Coefficients[0], 1e-7);
            Assert.AreEqual(0.1065137931017601, regression.Coefficients[1], 1e-7);
            Assert.AreEqual(22.010378526331344, regression.Coefficients[2], 1e-7);

            for (int i = 0; i < 50; i++)
            {
                newError = irls.Run(inputs, output);
            }

            Assert.AreEqual(-0.38409721299838279, regression.Coefficients[0], 1e-7);
            Assert.AreEqual(0.1065137931017601, regression.Coefficients[1], 1e-8);
            Assert.AreEqual(22.010378588337979, regression.Coefficients[2], 1e-8);
        }