Example #1
0
    def lstsq_matrix_test(self):
        """Test least square solution using a matrix with zero column"""
        # Initialize input matrices
        volumes   = [
                        [24], [20], [20], [20], [21], [30],
                        [40], [20], [20], [20], [19], [35]
                    ]
        promoted = [
                        [1, 0, 0, 0, 1],
                        [1, 0, 0, 0, 0],
                        [1, 0, 0, 0, 0],
                        [1, 0, 0, 0, 0],
                        [1, 0, 0, 0, 0],
                        [1, 0, 1, 0, 0],
                        [1, 0, 0, 1, 0],
                        [1, 0, 0, 0, 0],
                        [1, 0, 0, 0, 0],
                        [1, 0, 0, 0, 0],
                        [1, 0, 0, 0, 0],
                        [1, 0, 1, 0, 0]
                    ]
        volMatrix = Matrix(1, 12)
        volMatrix.initialize(volumes)
        proMatrix = Matrix(5, 12)
        proMatrix.initialize(promoted)
        # expected result calculated with scipy.lstsq()
        exRes = [
                    [2.00000000e+01],
                    [2.18197426e-16],
                    [1.25000000e+01],
                    [2.00000000e+01],
                    [4.00000000e+00]
                ]
        # Calculate least square solution
        res = LinearRegression.lstsq(proMatrix, volMatrix)

        # Compare if the values of the result are almost equal with the expected ones.
        for row in xrange(len(exRes)):
            for col in xrange(len(exRes[0])):
                self.assertAlmostEqual(exRes[row][col], res.get_value(col, row), self.precision)
Example #2
0
    def lstsq_test(self):
        """Test the least square method"""
        # Initialize input matrices
        volumes  = [
                        [24], [20], [20], [20], [21], [30],
                        [40], [20], [20], [20], [19], [35]
                    ]
        promoted = [
                        [1, 0, 0, 0, 1],
                        [1, 0, 0, 0, 0],
                        [1, 0, 0, 0, 0],
                        [1, 0, 0, 0, 0],
                        [1, 1, 0, 0, 0],
                        [1, 0, 1, 0, 0],
                        [1, 0, 0, 1, 0],
                        [1, 0, 0, 0, 0],
                        [1, 0, 0, 0, 0],
                        [1, 0, 0, 0, 0],
                        [1, 1, 0, 0, 0],
                        [1, 0, 1, 0, 0]
                    ]

        expRes = [
                    [19.999999999999993],
                    [8.8817841970012523e-15],
                    [12.500000000000011],
                    [20.000000000000007],
                    [4.0]
                ]
        volMatrix = Matrix(1, 12)
        volMatrix.initialize(volumes)
        proMatrix = Matrix(5, 12)
        proMatrix.initialize(promoted)

        # Execute least square method and compare result
        resMatrix = LinearRegression.lstsq(proMatrix, volMatrix)
        for row in range(resMatrix.get_height()):
            for col in range(resMatrix.get_width()):
                self.assertAlmostEqual(resMatrix.get_value(col, row), expRes[row][col], self.precision)
Example #3
0
    def lstsq_test(self):
        """Test the least square method"""
        # Initialize input matrices
        volumes = [[24], [20], [20], [20], [21], [30], [40], [20], [20], [20],
                   [19], [35]]
        promoted = [[1, 0, 0, 0, 1], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0],
                    [1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 0, 1, 0, 0],
                    [1, 0, 0, 1, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0],
                    [1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 0, 1, 0, 0]]

        expRes = [[19.999999999999993], [8.8817841970012523e-15],
                  [12.500000000000011], [20.000000000000007], [4.0]]
        volMatrix = Matrix(1, 12)
        volMatrix.initialize(volumes)
        proMatrix = Matrix(5, 12)
        proMatrix.initialize(promoted)

        # Execute least square method and compare result
        resMatrix = LinearRegression.lstsq(proMatrix, volMatrix)
        for row in range(resMatrix.get_height()):
            for col in range(resMatrix.get_width()):
                self.assertAlmostEqual(resMatrix.get_value(col, row),
                                       expRes[row][col], self.precision)
Example #4
0
    def lstsq_matrix_test(self):
        """Test least square solution using a matrix with zero column"""
        # Initialize input matrices
        volumes = [[24], [20], [20], [20], [21], [30], [40], [20], [20], [20],
                   [19], [35]]
        promoted = [[1, 0, 0, 0, 1], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0],
                    [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 1, 0, 0],
                    [1, 0, 0, 1, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0],
                    [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 1, 0, 0]]
        volMatrix = Matrix(1, 12)
        volMatrix.initialize(volumes)
        proMatrix = Matrix(5, 12)
        proMatrix.initialize(promoted)
        # expected result calculated with scipy.lstsq()
        exRes = [[2.00000000e+01], [2.18197426e-16], [1.25000000e+01],
                 [2.00000000e+01], [4.00000000e+00]]
        # Calculate least square solution
        res = LinearRegression.lstsq(proMatrix, volMatrix)

        # Compare if the values of the result are almost equal with the expected ones.
        for row in xrange(len(exRes)):
            for col in xrange(len(exRes[0])):
                self.assertAlmostEqual(exRes[row][col],
                                       res.get_value(col, row), self.precision)