Exemple #1
0
     def _lle(self, X):

          W = self.barycenter_graph(X)
          # find the null space of (I-W)
          M = eye(*W.shape, format=W.format) - W
          # symmetrize
          M = (M.T * M).tocsr()  
    def fit(self, x, y_multiclass, kernel=poly(1), C=0.001):
        y_multiclass=y_multiclass.reshape(-1).astype(np.float64)
        self.x = sparse.COO(x.astype(np.float64))
        self.m = self.x.shape[0]
        self.y_multiclass = y_multiclass
        self.kernel = kernel
        self.C = C
        ys = [sparse.COO(self.cast(y_multiclass, k)) for k in range(self.n_svm)]
        self.y_matrix = sparse.stack(ys,0)
        del ys
        for k in range(self.n_svm):
            print("training ",k,"th SVM in ",self.n_svm)
            y = self.y_matrix[k, :].reshape((-1,1))
            yx = y * self.x
            G = kernel(yx, yx) # Gram matrix

            compensate = (sparse.eye(self.m)*1e-7).astype(np.float64)
            G = (G + compensate)
            objective = cp.Maximize(cp.sum(self.a[k])-(1/2)*cp.quad_form(self.a[k], G.tocsr()))

            if not objective.is_dcp():
                print("Not solvable!")
                assert objective.is_dcp()
            constraints = [self.a[k] <= C, cp.sum(cp.multiply(self.a[k],y.todense())) == 0] # box constraint
            prob = cp.Problem(objective, constraints)
            result = prob.solve()
            x_pos = x[y.todense()[:,0]==1,:]
            x_neg = x[y.todense()[:,0]==-1,:]
            b_min = -np.min(self.wTx(k,x_pos)) if x_pos.shape[0]!=0 else 0
            b_max = -np.max(self.wTx(k,x_neg)) if x_neg.shape[0]!=0 else 0
            self.b[k,0] = (1/2)*(b_min + b_max)
        self.a_matrix = np.stack([i.value.reshape(-1) for i in self.a],0)
        self.a_matrix = sparse.COO(self.a_matrix)
def _ident_like(A):
    # A compatibility function which should eventually disappear.
    if scipy.sparse.isspmatrix(A):
        return scipy.sparse.construct.eye(A.shape[0], A.shape[1],
                dtype=A.dtype, format=A.format)
    elif is_pydata_spmatrix(A):
        import sparse
        return sparse.eye(A.shape[0], A.shape[1], dtype=A.dtype)
    else:
        return np.eye(A.shape[0], A.shape[1], dtype=A.dtype)
Exemple #4
0
def _ident_like(A):
    # A compatibility function which should eventually disappear.
    if scipy.sparse.isspmatrix(A):
        return scipy.sparse._construct.eye(A.shape[0], A.shape[1],
                dtype=A.dtype, format=A.format)
    elif is_pydata_spmatrix(A):
        import sparse
        return sparse.eye(A.shape[0], A.shape[1], dtype=A.dtype)
    elif isinstance(A, scipy.sparse.linalg.LinearOperator):
        return IdentityOperator(A.shape, dtype=A.dtype)
    else:
        return np.eye(A.shape[0], A.shape[1], dtype=A.dtype)
Exemple #5
0
    def partial_svd(self, matrix, n_eigenvecs=None, random_state=None, **kwargs):
        # Check that matrix is... a matrix!
        if matrix.ndim != 2:
            raise ValueError('matrix be a matrix. matrix.ndim is {} != 2'.format(
                matrix.ndim))

        # Choose what to do depending on the params
        dim_1, dim_2 = matrix.shape
        if dim_1 <= dim_2:
            min_dim = dim_1
        else:
            min_dim = dim_2

        if not is_sparse(matrix) and (n_eigenvecs is None or n_eigenvecs >= min_dim):
            if n_eigenvecs is None or n_eigenvecs > min_dim:
                full_matrices = True
            else:
                full_matrices = False
            # Default on standard SVD
            U, S, V = scipy.linalg.svd(matrix, full_matrices=full_matrices)
            U, S, V = U[:, :n_eigenvecs], S[:n_eigenvecs], V[:n_eigenvecs, :]
            return U, S, V
        elif n_eigenvecs is None:
            raise ValueError('n_eigenvecs cannot be none')
        elif is_sparse(matrix) and matrix.nnz == 0:
            # all-zeros matrix, so we should do a quick return.
            U = sparse.eye(dim_1, n_eigenvecs, dtype=matrix.dtype)
            S = np.zeros(n_eigenvecs, dtype=matrix.dtype)
            V = sparse.eye(dim_2, n_eigenvecs, dtype=matrix.dtype)
        else:
            if n_eigenvecs > min_dim:
                msg = ('n_eigenvecs={} if greater than the minimum matrix '
                       'dimension ({})')
                raise ValueError(msg.format(n_eigenvecs, min(matrix.shape)))
            if np.issubdtype(matrix.dtype, np.complexfloating):
                raise NotImplementedError("Complex dtypes")
            # We can perform a partial SVD
            rng = self.check_random_state(random_state)
            # initilize with [-1, 1] as in ARPACK
            v0 = rng.uniform(-1, 1, min_dim)

            # First choose whether to use X * X.T or X.T *X
            if dim_1 < dim_2:
                conj = matrix.T
                xxT = matrix.dot(conj)
                if is_sparse(xxT):
                    xxT = xxT.to_scipy_sparse()
                if n_eigenvecs >= xxT.shape[0]:
                    # use dense form when sparse form will fail
                    S, U = scipy.linalg.eigh(xxT.toarray())
                else:
                    S, U = scipy.sparse.linalg.eigsh(xxT, k=n_eigenvecs, which='LM', v0=v0)
                S = np.sqrt(S)
                V = conj.dot(U / S[None, :])
            else:
                xTx = matrix.T.dot(matrix)
                if is_sparse(xTx):
                    xTx = xTx.to_scipy_sparse()
                if n_eigenvecs >= xTx.shape[0]:
                    # use dense form when sparse form will fail
                    S, V = scipy.linalg.eigh(xTx.toarray())
                else:
                    S, V = scipy.sparse.linalg.eigsh(xTx, k=n_eigenvecs, which='LM', v0=v0)
                S = np.sqrt(S)
                U = matrix.dot(V / S[None, :])

            # WARNING: here, V is still the transpose of what it should be
            U, S, V = U[:, ::-1], S[::-1], V[:, ::-1]
        return U, S, V.T.conj()
Exemple #6
0
 def mask_factory():
     s = tuple(ds.shape.sig)
     return sparse.eye(np.prod(s)).reshape((-1, *s))
Exemple #7
0
def test_dask_array_is_sparse():
    assert utils.is_dask_array_sparse(
        da.from_array(sparse.COO([], [], shape=(10, 10))))
    assert utils.is_dask_array_sparse(da.from_array(sparse.eye(10)))
    assert not utils.is_dask_array_sparse(da.from_array(np.eye(10)))