Exemple #1
0
    def test_dense_svd(self):
        test_cases = self.svd_test_cases

        for x, u_expected, s_expected, v_expected in test_cases:
            for dim in [2, 3, 6]:
                u, s, v = Linalg.svd(DenseMatrix(x), dim)
                np.testing.assert_array_almost_equal(u.mat, u_expected, 2)
                np.testing.assert_array_almost_equal(s, s_expected, 2)
                np.testing.assert_array_almost_equal(v.mat, v_expected, 2)

            u, s, v = Linalg.svd(DenseMatrix(x), 1)
            np.testing.assert_array_almost_equal(u.mat, u_expected[:, 0:1], 2)
            np.testing.assert_array_almost_equal(s, s_expected[0:1], 2)
            np.testing.assert_array_almost_equal(v.mat, v_expected[:, 0:1], 2)
Exemple #2
0
    def test_sparse_svd(self):
        test_cases = self.svd_test_cases

        for x, u_expected, s_expected, v_expected in test_cases:
            for dim in [2,3,6]:
                u, s, v = Linalg.svd(SparseMatrix(x),dim)
                np.testing.assert_array_almost_equal(np.abs(u.mat.todense()), np.abs(u_expected), 2)
                np.testing.assert_array_almost_equal(np.abs(s), np.abs(s_expected), 2)
                np.testing.assert_array_almost_equal(np.abs(v.mat.todense()), np.abs(v_expected), 2)

            u, s, v = Linalg.svd(SparseMatrix(x),1)
            np.testing.assert_array_almost_equal(np.abs(u.mat.todense()), np.abs(u_expected[:,0:1]), 2)
            np.testing.assert_array_almost_equal(np.abs(s), np.abs(s_expected[0:1]), 2)
            np.testing.assert_array_almost_equal(np.abs(v.mat.todense()), np.abs(v_expected[:,0:1]), 2)
Exemple #3
0
    def test_dense_svd(self):
        test_cases = self.svd_test_cases

        for x, u_expected, s_expected, v_expected in test_cases:
            for dim in [2,3,6]:
                u, s, v = Linalg.svd(DenseMatrix(x),dim)
                np.testing.assert_array_almost_equal(u.mat, u_expected, 2)
                np.testing.assert_array_almost_equal(s, s_expected, 2)
                np.testing.assert_array_almost_equal(v.mat, v_expected, 2)

            u, s, v = Linalg.svd(DenseMatrix(x),1)
            np.testing.assert_array_almost_equal(u.mat, u_expected[:,0:1], 2)
            np.testing.assert_array_almost_equal(s, s_expected[0:1], 2)
            np.testing.assert_array_almost_equal(v.mat, v_expected[:, 0:1], 2)
Exemple #4
0
    def nndsvd_init(self, matrix_):
        def matrix_abs(mat_):
            mat_p = mat_.get_non_negative()
            mat_n_abs = mat_p - mat_
            return mat_p + mat_n_abs

        def padd_zeros(matrix_, axis, thickness):
            matrix_type = type(matrix_)
            if axis == 0:
                append_mat = matrix_type(
                    np.zeros((thickness, matrix_.shape[1])))
                return matrix_.vstack(append_mat)
            elif axis == 1:
                append_mat = matrix_type(
                    np.zeros((matrix_.shape[0], thickness)))
                return matrix_.hstack(append_mat)

        u, s, v = Linalg.svd(matrix_, self._reduced_dimension)

        rank = u.shape[1]
        w = [[]] * rank
        h = [[]] * rank

        vt = v.transpose()

        w[0] = sqrt(s[0]) * matrix_abs(u[:, 0])
        h[0] = sqrt(s[0]) * matrix_abs(vt[0, :])

        for i in range(1, rank):
            uu = u[:, i]
            vv = vt[i, :]
            uup = uu.get_non_negative()
            uun = uup - uu
            vvp = vv.get_non_negative()
            vvn = vvp - vv

            n_uup = uup.norm()
            n_uun = uun.norm()
            n_vvp = vvp.norm()
            n_vvn = vvn.norm()

            termp = n_uup * n_vvp
            termn = n_uun * n_vvn
            if (termp >= termn):
                w[i] = sqrt(s[i] * termp) * uup / n_uup
                h[i] = sqrt(s[i] * termp) * vvp / n_vvp
            else:
                w[i] = sqrt(s[i] * termn) * uun / n_uun
                h[i] = sqrt(s[i] * termn) * vvn / n_vvn

        w = matrix_.nary_hstack(w)
        h = matrix_.nary_vstack(h)

        w.remove_small_values(0.0000000001)
        h.remove_small_values(0.0000000001)

        if (rank < self._reduced_dimension):
            w = padd_zeros(w, 1, self._reduced_dimension - rank)
            h = padd_zeros(h, 0, self._reduced_dimension - rank)
        return w, h
Exemple #5
0
 def nndsvd_init(self,matrix_):
     def matrix_abs(mat_):
         mat_p = mat_.get_non_negative()
         mat_n_abs = mat_p - mat_
         return mat_p + mat_n_abs
     
     def padd_zeros(matrix_, axis, thickness):
         matrix_type = type(matrix_)
         if axis == 0:  
             append_mat = matrix_type(np.zeros((thickness, matrix_.shape[1])))
             return matrix_.vstack(append_mat)
         elif axis == 1:
             append_mat = matrix_type(np.zeros((matrix_.shape[0], thickness)))
             return matrix_.hstack(append_mat)
     
     u, s, v = Linalg.svd(matrix_, self._reduced_dimension);
     
     rank = u.shape[1]
     w = [[]]*rank
     h = [[]]*rank
     
     vt = v.transpose()
     
     w[0] = sqrt(s[0]) * matrix_abs(u[:,0])
     h[0] = sqrt(s[0]) * matrix_abs(vt[0,:])
     
     for i in range(1,rank):
         uu = u[:,i]
         vv = vt[i,:]
         uup = uu.get_non_negative()
         uun = uup - uu
         vvp = vv.get_non_negative()
         vvn = vvp - vv
         
         n_uup = uup.norm()
         n_uun = uun.norm()
         n_vvp = vvp.norm()
         n_vvn = vvn.norm()
         
         termp = n_uup * n_vvp; termn = n_uun * n_vvn
         if (termp >= termn):
             w[i] = sqrt(s[i] * termp) * uup / n_uup 
             h[i] = sqrt(s[i] * termp) * vvp / n_vvp
         else:
             w[i] = sqrt(s[i] * termn) * uun / n_uun 
             h[i] = sqrt(s[i] * termn) * vvn / n_vvn
     
     w = matrix_.nary_hstack(w)
     h = matrix_.nary_vstack(h)
     
     w.remove_small_values(0.0000000001)
     h.remove_small_values(0.0000000001)
     
     if (rank < self._reduced_dimension):
         w = padd_zeros(w, 1, self._reduced_dimension - rank)
         h = padd_zeros(h, 0, self._reduced_dimension - rank)
     return w,h      
Exemple #6
0
    def test_sparse_svd(self):
        test_cases = self.svd_test_cases

        for x, u_expected, s_expected, v_expected in test_cases:
            for dim in [2, 3, 6]:
                u, s, v = Linalg.svd(SparseMatrix(x), dim)
                np.testing.assert_array_almost_equal(np.abs(u.mat.todense()),
                                                     np.abs(u_expected), 2)
                np.testing.assert_array_almost_equal(np.abs(s),
                                                     np.abs(s_expected), 2)
                np.testing.assert_array_almost_equal(np.abs(v.mat.todense()),
                                                     np.abs(v_expected), 2)

            u, s, v = Linalg.svd(SparseMatrix(x), 1)
            np.testing.assert_array_almost_equal(np.abs(u.mat.todense()),
                                                 np.abs(u_expected[:, 0:1]), 2)
            np.testing.assert_array_almost_equal(np.abs(s),
                                                 np.abs(s_expected[0:1]), 2)
            np.testing.assert_array_almost_equal(np.abs(v.mat.todense()),
                                                 np.abs(v_expected[:, 0:1]), 2)
Exemple #7
0
    def apply(self, matrix_):

        u, s, v = Linalg.svd(matrix_, self._reduced_dimension)
        return u.scale_columns(s), v
Exemple #8
0
    def apply(self, matrix_):

        u, s, v = Linalg.svd(matrix_, self._reduced_dimension)
        return u.scale_columns(s), v