示例#1
0
        public void Logistic_Regression_Test_Generator()
        {
            Matrix m = new[, ] {
                { 0.0512670, 0.6995600 },
                { -0.0927420, 0.6849400 },
                { -0.2137100, 0.6922500 },
                { -0.3750000, 0.5021900 },
                { -0.5132500, 0.4656400 },
                { -0.5247700, 0.2098000 },
                { -0.3980400, 0.0343570 },
                { -0.3058800, -0.1922500 },
                { 0.0167050, -0.4042400 },
                { 0.1319100, -0.5138900 },
                { -0.6111800, -0.0679820 },
                { -0.6630200, -0.2141800 },
                { -0.5996500, -0.4188600 },
                { -0.7263800, -0.0826020 },
                { -0.8300700, 0.3121300 },
                { -0.7206200, 0.5387400 },
                { -0.5938900, 0.4948800 },
                { -0.4844500, 0.9992700 },
                { -0.0063364, 0.9992700 },
                { 0.6326500, -0.0306120 },
            };

            Vector y = new Vector(new double[] {
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0
            });

            Vector test = new Vector(new double[] { 0.1319100, -0.513890 });

            var generator = new LogisticRegressionGenerator()
            {
                Lambda = 1, LearningRate = 0.01, PolynomialFeatures = 6, MaxIterations = 400
            };

            var    model2 = generator.Generate(m, y);
            double p      = model2.Predict(test);

            Assert.AreEqual(1d, p);
        }
        public void Logistic_Regression_XOR_Learner()
        {
            var xor = new[]
            {
                new { a = false, b = false, c = false },
                new { a = false, b = true, c = true },
                new { a = true, b = false, c = true },
                new { a = true, b = true, c = false },
                new { a = false, b = false, c = false },
                new { a = false, b = true, c = true },
                new { a = true, b = false, c = true },
                new { a = true, b = true, c = false },
                new { a = false, b = false, c = false },
                new { a = false, b = true, c = true },
                new { a = true, b = false, c = true },
                new { a = true, b = true, c = false },
                new { a = false, b = false, c = false },
                new { a = false, b = true, c = true },
                new { a = true, b = false, c = true },
                new { a = true, b = true, c = false },
                new { a = false, b = false, c = false },
                new { a = false, b = true, c = true },
                new { a = true, b = false, c = true },
                new { a = true, b = true, c = false },
            };

            var d = Descriptor.New("XOR")
                    .With("a").As(typeof(bool))
                    .With("b").As(typeof(bool))
                    .Learn("c").As(typeof(bool));

            var generator = new LogisticRegressionGenerator()
            {
                Descriptor = d, PolynomialFeatures = 3, LearningRate = 0.01, Lambda = 0
            };
            var model = Learner.Learn(xor, 0.9, 5, generator).Model;

            Matrix x = new[, ]
            {
                { -1, -1 },   // false, false -> -
                { -1, 1 },    // false, true  -> +
                { 1, -1 },    // true, false  -> +
                { 1, 1 }
            };                // true, true   -> -

            Vector y_actual = new Vector(new[] { 0d, 1d, 1d, 0d });

            Vector y = new Vector(x.Rows);

            for (int i = 0; i < x.Rows; i++)
            {
                y[i] = model.Predict(x[i, VectorType.Row]);
            }

            var scored = Supervised.Score.ScorePredictions(y, y_actual);

            Assert.True(scored.Accuracy >= 0.75);

            Console.WriteLine($"LR Model Score: { scored }");
        }
        //[Test]
        public void Save_And_Load_LogisticRegression()
        {
            Matrix m = new[, ] {
                { 0.0512670, 0.6995600 },
                { -0.0927420, 0.6849400 },
                { -0.2137100, 0.6922500 },
                { -0.3750000, 0.5021900 },
                { -0.5132500, 0.4656400 },
                { -0.5247700, 0.2098000 },
                { -0.3980400, 0.0343570 },
                { -0.3058800, -0.1922500 },
                { 0.0167050, -0.4042400 },
                { 0.1319100, -0.5138900 },
                { -0.6111800, -0.0679820 },
                { -0.6630200, -0.2141800 },
                { -0.5996500, -0.4188600 },
                { -0.7263800, -0.0826020 },
                { -0.8300700, 0.3121300 },
                { -0.7206200, 0.5387400 },
                { -0.5938900, 0.4948800 },
                { -0.4844500, 0.9992700 },
                { -0.0063364, 0.9992700 },
                { 0.6326500, -0.0306120 },
            };

            Vector y = new Vector(new double[] {
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0
            });

            var generator = new LogisticRegressionGenerator()
            {
                Lambda = 1, LearningRate = 0.01, PolynomialFeatures = 6, MaxIterations = 400
            };

            var model = generator.Generate(m, y) as LogisticRegressionModel;

            Serialize(model);

            var lmodel = Deserialize <LogisticRegressionModel>();

            Assert.AreEqual(model.Theta, lmodel.Theta);
            Assert.AreEqual(model.PolynomialFeatures, lmodel.PolynomialFeatures);
            Assert.AreEqual(model.LogisticFunction, lmodel.LogisticFunction);
        }