Ejemplo n.º 1
0
        public SimpleLinearRegression Regress(double[] inputs, double[] outputs)
        {
            var newInputs = new double[inputs.Length][];

            for (int i = 0; i < inputs.Length; i++)
            {
                newInputs[i] = new double[] { inputs[i] };
            }

            var result = _multipleLinearRegression.Regress(newInputs, outputs);

            _linearRegression = result.LinearRegression;
            return(this);
        }
Ejemplo n.º 2
0
        public MultipleLinearRegression Regress(Matrix matrix, Vector output, bool addOne = true)
        {
            if (addOne)
            {
                matrix = matrix.AddColumn(1);
            }

            output.SetVertical();
            if (_alg == MatrixDecompositionAlgs.NAIVE)
            {
                _singularValueDecomposition.DecomposeNaive(matrix);
            }
            else
            {
                _singularValueDecomposition.DecomposeGolubReinsch(matrix);
            }

            var coefficients = _singularValueDecomposition.Inverse()
                               .Multiply((Matrix)output)
                               .Evaluate()
                               .GetColumnVector(0);
            var result = new LinearRegressionResult();

            for (int i = 0; i < coefficients.Length; i++)
            {
                var coefficient       = coefficients[i].GetNumber();
                var coefficientResult = new CoefficientResult
                {
                    Value = coefficient
                };
                if (i == 0)
                {
                    result.Intercept = coefficientResult;
                }
                else
                {
                    result.SlopeLst.Add(coefficientResult);
                }
            }

            _linearRegressionResult = result;
            UpdateInformation(matrix, output, addOne);
            return(this);
        }