Exemple #1
0
        public override Common.MLCore.ModelBase BuildModel(double[][] trainingData,
                                                           string[] attributeHeaders,
                                                           int indexTargetAttribute)
        {
            VerifyData(trainingData, attributeHeaders, indexTargetAttribute);

            ModelLinearMultiVariableBase model = new ModelLinearMultiVariableBase(_missingValue,
                                                                                  _indexTargetAttribute, _trainingData.Length - 1);


            //Create Matrix Y and replace Target values with 1
            double[][] Y = new double[1][];
            Y[0] = new double[trainingData[0].Length];
            Parallel.For(0, Y[0].Length,
                         new ParallelOptions {
                MaxDegreeOfParallelism = _maxParallelThreads
            },
                         row =>
            {
                Y[0][row] = trainingData[indexTargetAttribute][row];
                trainingData[indexTargetAttribute][row] = 1;//Set it to
            });

            MatrixOperations mo = new MatrixOperations();

            double[][] transposeX = mo.Transpose(trainingData);
            double[][] multiply   = mo.Multiply(transposeX, trainingData);
            double[][] inverse    = mo.Inverse(multiply);

            double[][] final = mo.Multiply(inverse, transposeX);
            final = mo.Multiply(final, Y);

            //Copy coeffs into main array
            //Intercept is in last index
            for (int idx = 0; idx < _noOfAttributes + 1; idx++)
            {
                model.SetCoeff(idx, final[0][idx]);
            }

            //Restore Y values in TrainingData
            Parallel.For(0, Y[0].Length,
                         new ParallelOptions {
                MaxDegreeOfParallelism = _maxParallelThreads
            },
                         row =>
            {
                trainingData[indexTargetAttribute][row] = Y[0][row];    //Set it to
            });

            return(model);
        }
        public void Matrix_inverse_determinant_0_3_row_test()
        {
            double[][] matrixA = new double[3][];
            matrixA[0] = new double[] { 1, 4, 7 };
            matrixA[1] = new double[] { 2, 5, 8 };
            matrixA[2] = new double[] { 3, 6, 9 };

            MatrixOperations mo = new MatrixOperations();

            double[][] matrixI =
                mo.Inverse(matrixA);

            //Check Det
            Assert.IsFalse(SupportFunctions.DoubleCompare(matrixI[0][0], -11.0 / 12.0));
        }
        public void Matrix_inverse_2_row_test()
        {
            double[][] matrixA = new double[2][];
            matrixA[0] = new double[] { 1, 2 };
            matrixA[1] = new double[] { 3, 4 };

            MatrixOperations mo = new MatrixOperations();

            double[][] matrixI =
                mo.Inverse(matrixA);

            //Check Det
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrixI[0][0], -2.0));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrixI[0][1], 1.0));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrixI[1][0], 3.0 / 2.0));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrixI[1][1], -1.0 / 2.0));
        }
        public void Matrix_inverse_3_row_test()
        {
            double[][] matrixA = new double[3][];
            matrixA[0] = new double[] { 1, 2, 3 };
            matrixA[1] = new double[] { 4, 0, 6 };
            matrixA[2] = new double[] { 7, 8, 9 };

            MatrixOperations mo = new MatrixOperations();

            double[][] matrixI =
                mo.Inverse(matrixA);

            //Check Det
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrixI[0][0], -0.8));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrixI[0][1], 0.1));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrixI[0][2], 0.2));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrixI[1][0], 0.1));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrixI[1][1], -0.2));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrixI[1][2], 0.1));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrixI[2][0], 8.0 / 15.0));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrixI[2][1], 0.1));
            Assert.IsTrue(SupportFunctions.DoubleCompare(matrixI[2][2], -2.0 / 15.0));
        }