示例#1
5
 def matrix_completion(self):
     if self.completion == 'matrix':
         res, _ = md.matrix_decomposition(self.Y, self.mask, **self.options)
     elif self.completion == 'mean':
         means = self.Y.mean(0) # means along the column axis
         # calculate the true means along the column axis
         # using only the values in the mask
         rows, cols = self.Y.shape
         means = sp.zeros(cols)
         # calculate the true means along the column axis
         # using only the values in the mask
         for c in range(cols):
             sum = 0.0
             n = 0
             for r in range(rows):
                 if self.mask[r][c]:
                     sum += self.Y[r][c]
                     n += 1
             if n == 0:
                 means[c] = 0
             else:
                 means[c] = sum/n
         
         res = double(self.Y)
         for r in range(rows):
             for c in range(cols):
                 if not self.mask[r][c]:
                     res[r][c] = means[c]
     return res
 def comparative_test(self):
     TH, GA = self._TH, self._GA
     Y, Mask, args = self._Y, self._Mask, self._decomposition_args
     A, B = md.matrix_decomposition(Y, Mask, **args)
     if self.verbose == True:
         print GA[1, :]
         print B[1, :]
         print Mask[1, :]
         print TH[1, :]
         print A[1, :]
         
         error = ((TH - A)**2).sum() / TH.size
         print "mean square error:", error
         error2 = ((TH - Y*Mask)**2).sum() / TH.size
         print "mse for naive solution:", error2
         print "improvement:", error2/error, "times"
 def test_decomposition_id(self):
     # testing using the identity map
     TH = sp.array([[1, 2, 3], [2, 4, 6]])
     GA = sp.array([[0, 0, 0], [0, 100, 0]])
     
     Y = TH + GA
     
     A, B = md.matrix_decomposition(Y, lambda_d=0.1, mu_d=0.08, alpha=1000)
     
     self.assertLess(abs(TH-A).sum(), 1.0, "")
     self.assertLess(abs(TH-A).max(), 0.2, "")
     
     self.assertLess(abs(GA-A).sum() < 1.0, "")
    def matrix_completion(self):
        if self.completion == 'matrix':
            res, _ = md.matrix_decomposition(self.Y, self.mask, **self.options)
        elif self.completion == 'mean':
            means = self.Y.mean(0)  # means along the column axis
            # calculate the true means along the column axis
            # using only the values in the mask
            rows, cols = self.Y.shape
            means = sp.zeros(cols)
            # calculate the true means along the column axis
            # using only the values in the mask
            for c in range(cols):
                sum = 0.0
                n = 0
                for r in range(rows):
                    if self.mask[r][c]:
                        sum += self.Y[r][c]
                        n += 1
                if n == 0:
                    means[c] = 0
                else:
                    means[c] = sum / n

            res = double(self.Y)
            for r in range(rows):
                for c in range(cols):
                    if not self.mask[r][c]:
                        res[r][c] = means[c]
        return res
    def test_decomposition_id(self):
        # testing using the identity map
        TH = sp.array([[1, 2, 3], [2, 4, 6]])
        GA = sp.array([[0, 0, 0], [0, 100, 0]])

        Y = TH + GA

        A, B = md.matrix_decomposition(Y, lambda_d=0.1, mu_d=0.08, alpha=1000)

        self.assertLess(abs(TH - A).sum(), 1.0, "")
        self.assertLess(abs(TH - A).max(), 0.2, "")

        self.assertLess(abs(GA - A).sum() < 1.0, "")
    def comparative_test(self):
        TH, GA = self._TH, self._GA
        Y, Mask, args = self._Y, self._Mask, self._decomposition_args
        A, B = md.matrix_decomposition(Y, Mask, **args)
        if self.verbose == True:
            print GA[1, :]
            print B[1, :]
            print Mask[1, :]
            print TH[1, :]
            print A[1, :]

            error = ((TH - A)**2).sum() / TH.size
            print "mean square error:", error
            error2 = ((TH - Y * Mask)**2).sum() / TH.size
            print "mse for naive solution:", error2
            print "improvement:", error2 / error, "times"