Пример #1
0
        public void TestLinearRegression()
        {
            double [][] inputs = new double[3][];

            inputs[0] = new double[3];
            inputs[1] = new double[3];
            inputs[2] = new double[3];

            /*
             * system of equations:
             * x1 + x2 + x3 = 35
             * x1 + x2 - x3 = -13
             * x1 - x2 + x3 = 19
             *
             * x1 should be 3, x2 should be 8, x3 should be 24
             *
             */
            inputs[0][0] = 1;
            inputs[0][1] = 1;
            inputs[0][2] = 1;

            inputs[1][0] = 1;
            inputs[1][1] = 1;
            inputs[1][2] = -1;

            inputs[2][0] = 1;
            inputs[2][1] = -1;
            inputs[2][2] = 1;

            double [] outputs = new double[3];
            outputs[0] = 35;
            outputs[1] = -13;
            outputs[2] = 19;

            var normalizer = new MultiThreadedNormalization();
            var regressor  = new LinearRegressor(normalizer, epsilon: 0.0000001);
            var matrix     = new Matrix(inputs);
            var weights    = regressor.Fit(matrix, outputs);

            var inputs2 = new double[1][];

            inputs2[0] = new double[3];

            inputs2[0][0] = 2;
            inputs2[0][1] = 2;
            inputs2[0][2] = 2;
            var inputMatrix = new Matrix(inputs2);

            var result = regressor.Predict(inputMatrix);
        }
Пример #2
0
        private static void LinearRegression()
        {
            Console.WriteLine("Fetching data . . .");
            var data = Queries.GetData();

            Console.WriteLine("Preprocessing data for linear regression . . .");
            var train = (int)(data.Count * 0.9);
            var lr    = new LinearRegressor(5);

            for (var i = 0; i < train; ++i)
            {
                var x = new double[5]
                {
                    Location.ToDistance(data[i].Get("Deo Grada")),
                    Convert.ToDouble(data[i].Get("Kvadratura")?.Split(' ')[0] ?? "0") / 100000,
                    Convert.ToDouble(data[i].Get("Godina izgradnje") ?? "0") / 2020,
                    Convert.ToDouble(data[i].Get("Ukupan broj soba") ?? "0") / 1000,
                    Convert.ToDouble(data[i].Get("Ukupan broj spratova") ?? "0") / 500
                };

                lr.Add(x, Convert.ToDouble(data[i].Price));
            }

            Console.WriteLine("Training linear regressor . . .");
            lr.Train(LinearRegressor.MSE, 1e-6, 10000);

            Console.WriteLine("Test\t\tTrue\t\t\tPredicted");
            for (var i = train; i < data.Count; ++i)
            {
                var x = new double[5]
                {
                    Location.ToDistance(data[i].Get("Deo Grada")),
                    Convert.ToDouble(data[i].Get("Kvadratura")?.Split(' ')[0] ?? "0") / 100000,
                    Convert.ToDouble(data[i].Get("Godina izgradnje") ?? "0") / 2020,
                    Convert.ToDouble(data[i].Get("Ukupan broj soba") ?? "0") / 1000,
                    Convert.ToDouble(data[i].Get("Ukupan broj spratova") ?? "0") / 500
                };

                var yh = lr.Predict(x);

                Console.WriteLine($"{i - train + 1}:\t\t{data[i].Price:F2}\t\t{yh:F2}");
            }
        }