Ejemplo n.º 1
0
Archivo: libmps.py Proyecto: japs/mpys
    def normalise_site_svd(self, site, left=True):
        '''
        Normalise the MP at a given site. Use the SVD method,
        arXiv:1008.3477, sec. 4.1.3
        '''
        if (left and site == L) or (not left and site == 1):
            self.normalise_extremal_site(site, left=left)
            return
        
        M_asb = self.mps[site] # the matrix we have to work on, M_site
        r, s, c = M_asb.shape  # row, spin, column

        if left:
            M_aab = M_asb.reshape((r * s, c)) # merge first two indices

            U, S, Vh = la.svd(M_aab, full_matrices=False)
            self.mps[site] = U.reshape((r, s, U.size // (r * s))) # new A_site

            # Contract over index b: (S @ Vh)_ab (X) (M_site+1)_bsc
            SVh = np.dot(S, Vh)
            self.mps[site+1] = np.tensordot(SVh, self.mps[site+1], ((1),(0)))
        else:
            M_aab = M_asb.reshape((r, s * c)) # merge last two indices

            U, S, Vh = la.svd(M_aab, full_matrices=False)
            self.mps[site] = Vh.reshape((Vh.size // (s * c), s, c)) # new B
            # Contract over index b: (M_site-1)_asb (X) (U @ S)_bc 
            US = np.dot(U, S)
            self.mps[site-1] = np.tensordot(self.mps[site-1], US, ((2),(0)))
        return
Ejemplo n.º 2
0
Archivo: libmps.py Proyecto: japs/mpys
    def mixed_canonicalise(site):
        '''
        Put the MPS in mixed canonical form.

        Sites 1...site are left-normalised, sites site...L are 
        right-normalised. The singular values are stored in self.S and 
        returned.
        '''
        if site > self.left_normalised:
            # gotta left-normalise sites left_normalised...site
            # left qr up to site-1
            for _s in range(self.left_normalised+1, site):
                self.normalise_site_qr(_s, left=True)
            # svd at site
            Msite = self.mps[site]
            r, s, c = Msite.shape
            Msite = Msite.reshape((r * s, c))
            U, self.S, Vh = la.svd(Msite, full_matrices=False)
            self.mps[site] = U.reshape((r, s, U.size // (r * s))) # new A_site
            self.mps[site+1] = np.tensordot(Vh, self.mps[site+1], ((1),(0))) 
        else:
            # gotta right-normalise sites right_normalised...site+1
            # right qr up to site+2
            for _s in range(self.right_normalised, site+1):
                self.normalise_site_qr(_s, left=False)
            # svd at site+1
            Msite = self.mps[site+1]
            r, s, c = Msite.shape
            Msite = Msite.reshape((r, s * c))
            U, self.S, Vh = la.svd(Msite, full_matrices=False)
            self.mps[site+1] = Vh.reshape((Vh // (s * c), s,c)) #new B
            self.mps[site] = np.tensordot(self.mps[site], U, ((1),(0))) 

        return self.S
Ejemplo n.º 3
0
def ideal_data(num, dimU, dimY, dimX, noise=1):
    """Linear system data"""
    # generate randomized linear system matrices
    A = randn(dimX, dimX)
    B = randn(dimX, dimU)
    C = randn(dimY, dimX)
    D = randn(dimY, dimU)

    # make sure state evolution is stable
    U, S, V = svd(A)
    A = dot(U, dot(diag(S / max(S)), V))
    U, S, V = svd(B)
    S2 = zeros((size(U,1), size(V,0)))
    S2[:,:size(U,1)] = diag(S / max(S))
    B = dot(U, dot(S2, V))

    # random input
    U = randn(num, dimU)

    # initial state
    X = reshape(randn(dimX), (1,-1))

    # initial output
    Y = reshape(dot(C, X[-1]) + dot(D, U[0]), (1,-1))

    # generate next state
    X = concatenate((X, reshape(dot(A, X[-1]) + dot(B, U[0]), (1,-1))))

    # and so forth
    for u in U[1:]:
        Y = concatenate((Y, reshape(dot(C, X[-1]) + dot(D, u), (1,-1))))
        X = concatenate((X, reshape(dot(A, X[-1]) + dot(B, u), (1,-1))))

    return U, Y + randn(num, dimY) * noise
Ejemplo n.º 4
0
def randomized_svd(A, rank=None, power_iter=2, k=50, p=50, block_krylov=True, l=0, use_id=False):
    """
    :param rank: If None, adaptive range finder is used to detect the rank.
    :param power_iter: Number of power iterations.
    :param k: Initial estimate of rank, if adaptive range finder is used.
    :param p: Batch size in the incremental steps of adaptive range finder.
    :param l: Number of overestimated columns in computing Q.
    """
    if rank is None:
        Q = adaptive_range(A, k=k, p=p, power_iter_k=power_iter)
    else:
        Q = randomized_range(A, rank, power_iter=power_iter, block_krylov=block_krylov, l=l)

    if rank is None:
        rank = Q.shape[1]

    if use_id:
        J, P = ID_row(Q, rank)
        W, R = la.qr(A[J, :].T, mode='economic')
        Q = P.dot(R.T)
        U, sigma, VT = la.svd(Q, full_matrices=False)
        V = VT.dot(W.T)
        return U[:, :rank], sigma[:rank], V[:rank, :]

    M = Q.T.dot(A)
    U, sigma, VT = la.svd(M, full_matrices=False)
    U = Q.dot(U)
    return U[:, :rank], sigma[:rank], VT[:rank, :]
Ejemplo n.º 5
0
def _compute_subcorr(G, phi_sig):
    """Compute the subspace correlation."""
    Ug, Sg, Vg = linalg.svd(G, full_matrices=False)
    tmp = np.dot(Ug.T.conjugate(), phi_sig)
    Uc, Sc, Vc = linalg.svd(tmp, full_matrices=False)
    X = np.dot(np.dot(Vg.T, np.diag(1. / Sg)), Uc)  # subcorr
    return Sc[0], X[:, 0] / linalg.norm(X[:, 0])
    def setdata(self, X, V):
        A = self.bialtprodeye(2*self.F.J_coords)
        """Note: p, q <= min(n,m)"""

        self.data.Brand = 2*(random((A.shape[0],self.data.p))-0.5)
        self.data.Crand = 2*(random((A.shape[1],self.data.q))-0.5)
        self.data.B = zeros((A.shape[0],self.data.p), Float)
        self.data.C = zeros((A.shape[1],self.data.q), Float)
        self.data.D = zeros((self.data.q,self.data.p), Float)

        U, S, Vh = linalg.svd(A)
        self.data.b = U[:,-1:]
        self.data.c = num_transpose(Vh)[:,-1:]
        
        if self.update:
            self.data.B[:,1] = self.data.b
            self.data.C[:,1] = self.data.c
            
            U2, S2, Vh2 = linalg.svd(c_[r_[A, transpose(self.data.C[:,1])], r_[self.data.B[:,1], [[0]]]])
            self.data.B[:,2] = U2[0:A.shape[0],-1:]
            self.data.C[:,2] = num_transpose(Vh2)[0:A.shape[1],-1:]
            self.data.D[0,1] = U2[A.shape[0],-1]
            self.data.D[1,0] = num_transpose(Vh2)[A.shape[1],-1]
        else:
            self.data.B = self.data.Brand
            self.data.C = self.data.Crand
Ejemplo n.º 7
0
def rcca(X, Y, reg):   
    X = np.array(X)
    Y = np.array(Y)
    n, p = X.shape
    n, q = Y.shape
        
    # zero mean
    X = X - X.mean(axis=0)
    Y = Y - Y.mean(axis=0)
        
    # covariances
    S = np.cov(X.T, Y.T, bias=1)
    
    SXX = S[:p,:p]
    SYY = S[p:,p:]
    SXY = S[:p,p:]

    ux, lx, vxt = SLA.svd(SXX)
    #uy, ly, vyt = SLA.svd(SYY)
    
    #print lx
    #正則化
    Rg = np.diag(np.ones(p)*reg)
    SXX = SXX + Rg
    SYY = SYY + Rg

    sqx = SLA.sqrtm(SLA.inv(SXX)) # SXX^(-1/2)
    sqy = SLA.sqrtm(SLA.inv(SYY)) # SYY^(-1/2)
    M = np.dot(np.dot(sqx, SXY), sqy.T) # SXX^(-1/2) * SXY * SYY^(-T/2)
    A, r, Bh = SLA.svd(M, full_matrices=False)
    B = Bh.T      
    #r = self.reg*r
    return r[0], lx[0], lx[0] 
Ejemplo n.º 8
0
def  addblock_svd_update( Uarg, Sarg, Varg, Aarg, force_orth = False):
  U = Varg
  V = Uarg
  S = np.eye(len(Sarg),len(Sarg))*Sarg
  A = Aarg.T
  
  current_rank = U.shape[1]
  m = np.dot(U.T,A)
  p = A - np.dot(U,m)
  P = lin.orth(p)
  Ra = np.dot(P.T,p)
  z = np.zeros(m.shape)
  K = np.vstack(( np.hstack((S,m)), np.hstack((z.T,Ra)) ))
  tUp,tSp,tVp = lin.svd(K);
  tUp = tUp[:,:current_rank]
  tSp = np.diag(tSp[:current_rank])
  tVp = tVp[:,:current_rank]
  Sp = tSp
  Up = np.dot(np.hstack((U,P)),tUp)
  Vp = np.dot(V,tVp[:current_rank,:])
  Vp = np.vstack((Vp, tVp[current_rank:tVp.shape[0], :]))
  
  if force_orth:
    UQ,UR = lin.qr(Up,mode='economic')
    VQ,VR = lin.qr(Vp,mode='economic')
    tUp,tSp,tVp = lin.svd( np.dot(np.dot(UR,Sp),VR.T));
    tSp = np.diag(tSp)
    Up = np.dot(UQ,tUp)
    Vp = np.dot(VQ,tVp)
    Sp = tSp;

  Up1 = Vp;
  Vp1 = Up;
    
  return Up1,Sp,Vp1
    def _solve_svd(self, X, y):
        """SVD solver.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            Training data.

        y : array-like, shape (n_samples,) or (n_samples, n_targets)
            Target values.
        """
        n_samples, n_features = X.shape
        n_classes = len(self.classes_)

        self.means_ = _class_means(X, y)
        if self.store_covariance:
            self.covariance_ = _class_cov(X, y, self.priors_)

        Xc = []
        for idx, group in enumerate(self.classes_):
            Xg = X[y == group, :]
            Xc.append(Xg - self.means_[idx])

        self.xbar_ = np.dot(self.priors_, self.means_)

        Xc = np.concatenate(Xc, axis=0)

        # 1) within (univariate) scaling by with classes std-dev
        std = Xc.std(axis=0)
        # avoid division by zero in normalization
        std[std == 0] = 1.
        fac = 1. / (n_samples - n_classes)

        # 2) Within variance scaling
        X = np.sqrt(fac) * (Xc / std)
        # SVD of centered (within)scaled data
        U, S, V = linalg.svd(X, full_matrices=False)

        rank = np.sum(S > self.tol)
        if rank < n_features:
            warnings.warn("Variables are collinear.")
        # Scaling of within covariance is: V' 1/S
        scalings = (V[:rank] / std).T / S[:rank]

        # 3) Between variance scaling
        # Scale weighted centers
        X = np.dot(((np.sqrt((n_samples * self.priors_) * fac)) *
                    (self.means_ - self.xbar_).T).T, scalings)
        # Centers are living in a space with n_classes-1 dim (maximum)
        # Use SVD to find projection in the space spanned by the
        # (n_classes) centers
        _, S, V = linalg.svd(X, full_matrices=0)

        rank = np.sum(S > self.tol * S[0])
        self.scalings_ = np.dot(scalings, V.T[:, :rank])
        coef = np.dot(self.means_ - self.xbar_, self.scalings_)
        self.intercept_ = (-0.5 * np.sum(coef ** 2, axis=1)
                           + np.log(self.priors_))
        self.coef_ = np.dot(coef, self.scalings_.T)
        self.intercept_ -= np.dot(self.xbar_, self.coef_.T)
Ejemplo n.º 10
0
def compute_bench(data_gen, samples_range, features_range, q=3):

    it = 0

    results = defaultdict(lambda: [])

    max_it = len(samples_range) * len(features_range)
    for n_samples in samples_range:
        for n_features in features_range:
            it += 1
            print '===================='
            print 'Iteration %03d of %03d' % (it, max_it)
            print '===================='
            X = make_data(n_samples, n_features)
            rank = min(n_samples, n_samples) / 10 + 1

            gc.collect()
            print "benching scipy svd: "
            tstart = time()
            svd(X, full_matrices=False)
            results['scipy svd'].append(time() - tstart)

            gc.collect()
            print "benching scikit-learn fast_svd: q=0"
            tstart = time()
            fast_svd(X, rank, q=0)
            results['scikit-learn fast_svd (q=0)'].append(time() - tstart)

            gc.collect()
            print "benching scikit-learn fast_svd: q=%d " % q
            tstart = time()
            fast_svd(X, rank, q=q)
            results['scikit-learn fast_svd (q=%d)' % q].append(time() - tstart)

    return results
Ejemplo n.º 11
0
def _tucker3(X, n_components, tol, max_iter, init_type, random_state=None):
    """
    3 dimensional Tucker decomposition.

    This code is meant to be a tutorial/testing example... in general _tuckerN
    should be more compact and equivalent mathematically.
    """

    if len(X.shape) != 3:
        raise ValueError("Tucker3 decomposition only supports 3 dimensions!")

    if init_type == "random":
        A, B, C = _random_init(X, n_components, random_state)
    elif init_type == "hosvd":
        A, B, C = _hosvd_init(X, n_components)
    err = 1E10
    X_sq = np.sum(X ** 2)

    for itr in range(max_iter):
        err_old = err
        U, S, V = linalg.svd(matricize(X, 0).dot(np.kron(C, B)),
                             full_matrices=False)
        A = U[:, :n_components]
        U, S, V = linalg.svd(matricize(X, 1).dot(np.kron(C, A)),
                             full_matrices=False)
        B = U[:, :n_components]
        U, S, V = linalg.svd(matricize(X, 2).dot(np.kron(B, A)),
                             full_matrices=False)
        C = U[:, :n_components]
        G = tmult(tmult(tmult(X, A.T, 0), B.T, 1), C.T, 2)
        err = np.sum(G ** 2) - X_sq
        thresh = np.abs(err - err_old) / err_old
        if thresh < tol:
            break
    return G, A, B, C
Ejemplo n.º 12
0
    def fit(self, X, **params):
        """Fit the model to the data X"""
        self._set_params(**params)
        X = np.atleast_2d(X)
        n_samples = X.shape[0]
        if self.copy:
            X = X.copy()
        # Center data
        self.mean_ = np.mean(X, axis=0)
        X -= self.mean_
        if self.do_fast_svd:
            if  self.n_comp == "mle" or self.n_comp is None:
                warnings.warn('All components are to be computed'
                              'Not using fast truncated SVD')
                U, S, V = linalg.svd(X, full_matrices=False)
            else:
                U, S, V = fast_svd(X, self.n_comp, q=self.iterated_power)
        else:
            U, S, V = linalg.svd(X, full_matrices=False)
        self.explained_variance_ = (S ** 2) / n_samples
        self.explained_variance_ratio_ = self.explained_variance_ / \
                                        self.explained_variance_.sum()
        self.components_ = V.T
        if self.n_comp=='mle':
            self.n_comp = _infer_dimension_(self.explained_variance_,
                                            n_samples, X.shape[1])

        if self.n_comp is not None:
            self.components_ = self.components_[:, :self.n_comp]
            self.explained_variance_ = self.explained_variance_[:self.n_comp]

        return self
Ejemplo n.º 13
0
def taskFive(n,R,G,B,w,h):
  Ur, Sr, VrT = svd(R.T, full_matrices=False)
  Ug, Sg, VgT = svd(G.T, full_matrices=False)
  Ub, Sb, VbT = svd(B.T, full_matrices=False)
  Ur = Ur[:,0:n]
  Sr = Sr[0:n]
  VrT = VrT[0:n,:]
  Ug = Ug[:,0:n]
  Sg = Sg[0:n]
  VgT = VgT[0:n,:]
  Ub = Ub[:,0:n]
  Sb = Sb[0:n]
  VbT = VbT[0:n,:]

  newim = Image.new('RGB',(w,h))
  R = numpy.dot(numpy.dot(Ur,numpy.diag(Sr)),VrT)
  R = numpy.array(R.T.flatten(),dtype=int)
  G = numpy.dot(numpy.dot(Ug,numpy.diag(Sg)),VgT)
  G = numpy.array(G.T.flatten(),dtype=int)
  B = numpy.dot(numpy.dot(Ub,numpy.diag(Sb)),VbT)
  B = numpy.array(B.T.flatten(),dtype=int)
  t = zip(R,G,B)
  newim.putdata(t)
  """newim.show()"""
  newim.save("large"+str(n)+".tif")
Ejemplo n.º 14
0
    def dataNorm(self):
        SXX = np.cov(self.X)
        U, l, Ut = LA.svd(SXX, full_matrices=True) 
        H = np.dot(LA.sqrtm(LA.inv(np.diag(l))),Ut)
        self.nX = np.dot(H,self.X)

        #print np.cov(self.nX)
        #print "mean:"
        #print np.mean(self.nX)

        SYY = np.cov(self.Y)
        U, l, Ut = LA.svd(SYY, full_matrices=True) 
        H = np.dot(LA.sqrtm(LA.inv(np.diag(l))),Ut)
        #print "H"
        #print H
        self.nY = np.dot(H,self.Y)
        #print np.cov(self.nY)

        print "dataNorm_X:"
        for i in range(len(self.nX)):
            print(self.nX[i])
        print("---")

        print "dataNorm_Y:"
        for i in range(len(self.nY)):
            print(self.nY[i])
        print("---")
Ejemplo n.º 15
0
def rpca(M, eps=0.001, r=1):
    '''
    An implementation of the robust pca algorithm as
    outlined in [need reference]
    '''
    assert(len(M.shape) == 2)

    m,n = M.shape

    s = linalg.svd(M, compute_uv=False)
    B = 1 / np.sqrt(n) # threshold parameter
    thresh = B * s[0]

    # Initial Low Rank Component
    L = np.zeros(M.shape)
    # Initial Sparse Component
    S = HT(M - L, thresh)

    iterations = range(int(10 * np.log(n * B * frob_norm(M - S) / eps)))
    logger.info('Number of iterations: %d to achieve eps = %f' % (len(iterations), eps))

    for k in xrange(1, r+1):
        for t in iterations:

            U,s,Vt = linalg.svd(M - S, full_matrices=False)
            thresh = B * ( s[k] + s[k-1] * (1/2)**t )

            # Best rank k approximation of M - S
            L = np.dot(np.dot(U[:,:k], np.diag(s[:k])), Vt[:k])
            S = HT(M - L, thresh)

        if (B * s[k]) < (eps / (2*n)):
            break

    return L,S
Ejemplo n.º 16
0
def compute_fundamental(x1,x2):
    """    Computes the fundamental matrix from corresponding points 
        (x1,x2 3*n arrays) using the 8 point algorithm.
        Each row in the A matrix below is constructed as
        [x'*x, x'*y, x', y'*x, y'*y, y', x, y, 1] """
    
    n = x1.shape[1]
    if x2.shape[1] != n:
        raise ValueError("Number of points don't match.")
    
    # build matrix for equations
    A = zeros((n,9))
    for i in range(n):
        A[i] = [x1[0,i]*x2[0,i], x1[0,i]*x2[1,i], x1[0,i]*x2[2,i],
                x1[1,i]*x2[0,i], x1[1,i]*x2[1,i], x1[1,i]*x2[2,i],
                x1[2,i]*x2[0,i], x1[2,i]*x2[1,i], x1[2,i]*x2[2,i] ]
            
    # compute linear least square solution
    U,S,V = linalg.svd(A)
    F = V[-1].reshape(3,3)
        
    # constrain F
    # make rank 2 by zeroing out last singular value
    U,S,V = linalg.svd(F)
    S[2] = 0
    F = dot(U,dot(diag(S),V))
    
    return F/F[2,2]
Ejemplo n.º 17
0
def geigen(Amat, Bmat, Cmat):
    """
    generalized eigenvalue problem of the form

    max tr L'AM / sqrt(tr L'BL tr M'CM) w.r.t. L and M

    :param Amat numpy ndarray of shape (M,N)
    :param Bmat numpy ndarray of shape (M,N)
    :param Bmat numpy ndarray of shape (M,N)

    :rtype: numpy ndarray
    :return values: eigenvalues
    :return Lmat: left eigenvectors
    :return Mmat: right eigenvectors

    """
    if Bmat.shape[0] != Bmat.shape[1]:
        print("BMAT is not square.\n")
        sys.exit(1)

    if Cmat.shape[0] != Cmat.shape[1]:
        print("CMAT is not square.\n")
        sys.exit(1)

    p = Bmat.shape[0]
    q = Cmat.shape[0]

    s = min(p, q)
    tmp = fabs(Bmat - Bmat.transpose())
    tmp1 = fabs(Bmat)
    if tmp.max() / tmp1.max() > 1e-10:
        print("BMAT not symmetric..\n")
        sys.exit(1)

    tmp = fabs(Cmat - Cmat.transpose())
    tmp1 = fabs(Cmat)
    if tmp.max() / tmp1.max() > 1e-10:
        print("CMAT not symmetric..\n")
        sys.exit(1)

    Bmat = (Bmat + Bmat.transpose()) / 2.
    Cmat = (Cmat + Cmat.transpose()) / 2.
    Bfac = cholesky(Bmat)
    Cfac = cholesky(Cmat)
    Bfacinv = inv(Bfac)
    Bfacinvt = Bfacinv.transpose()
    Cfacinv = inv(Cfac)
    Dmat = Bfacinvt.dot(Amat).dot(Cfacinv)
    if p >= q:
        u, d, v = svd(Dmat)
        values = d
        Lmat = Bfacinv.dot(u)
        Mmat = Cfacinv.dot(v.transpose())
    else:
        u, d, v = svd(Dmat.transpose())
        values = d
        Lmat = Bfacinv.dot(u)
        Mmat = Cfacinv.dot(v.transpose())

    return values, Lmat, Mmat
Ejemplo n.º 18
0
def compute_fundamental(x1,x2):
  """ 正規化8点法を使って対応点群(x1,x2:3*nの配列)
      から基礎行列を計算する。各列は次のような並びである。
      [x'*x, x'*y, x', y'*x, y'*y, y', x, y, 1] """

  n = x1.shape[1]
  if x2.shape[1] != n:
    raise ValueError("Number of points don't match.")

  # 方程式の行列を作成する
  A = zeros((n,9))
  for i in range(n):
    A[i] = [x1[0,i]*x2[0,i], x1[0,i]*x2[1,i], x1[0,i]*x2[2,i],
        x1[1,i]*x2[0,i], x1[1,i]*x2[1,i], x1[1,i]*x2[2,i],
        x1[2,i]*x2[0,i], x1[2,i]*x2[1,i], x1[2,i]*x2[2,i] ]

  # 線形最小2乗法で計算する
  U,S,V = linalg.svd(A)
  F = V[-1].reshape(3,3)

  # Fの制約
  # 最後の特異値を0にして階数を2にする
  U,S,V = linalg.svd(F)
  S[2] = 0
  F = dot(U,dot(diag(S),V))

  return F
Ejemplo n.º 19
0
def compute_bench(samples_range, features_range, q=3, rank=50):

    it = 0

    results = defaultdict(lambda: [])

    max_it = len(samples_range) * len(features_range)
    for n_samples in samples_range:
        for n_features in features_range:
            it += 1
            print '===================='
            print 'Iteration %03d of %03d' % (it, max_it)
            print '===================='
            X = low_rank_fat_tail(n_samples, n_features, effective_rank=rank,
                                  tail_strength=0.2)

            gc.collect()
            print "benching scipy svd: "
            tstart = time()
            svd(X, full_matrices=False)
            results['scipy svd'].append(time() - tstart)

            gc.collect()
            print "benching scikit-learn fast_svd: q=0"
            tstart = time()
            fast_svd(X, rank, q=0)
            results['scikit-learn fast_svd (q=0)'].append(time() - tstart)

            gc.collect()
            print "benching scikit-learn fast_svd: q=%d " % q
            tstart = time()
            fast_svd(X, rank, q=q)
            results['scikit-learn fast_svd (q=%d)' % q].append(time() - tstart)

    return results
Ejemplo n.º 20
0
def hosvd2(T, saveSpace=False):
    """HOSVD    N-mode SVD decomposition of N-way tensor
    (Z, Un, Sn, Vn) = HOSVD(T) decomposes the N-way tensor D into N
    orthogonal matrices stored in Un and a core tensor Z.  Also
    returns the n Tucker1 results in Sn and Vn if saveSpace=False (default)

    Author: Larsson Omberg <*****@*****.**>
    Date:   11-NOV-2007, 28-February-2008, 5-June-2009, 4-Nov-2010"""
    #Find orthogonal matrices
    Un=[]; Vn=[]; Sn=[]
    for n in range(T.ndim):
        Tn = flatten(T, n+1);  #FIX
        if Tn.shape[1] < Tn.shape[0]:
            [U,S,V] = svd(Tn,0);
            V=V.T
        else:
            [V,S,U] = svd(Tn.T,0);
            U=U.T
        Un.append(U);
        if not saveSpace:
            Vn.append(V);
            Sn.append(S);
    Z=T.copy()
    for n in range(len(Un)):
        Z = nmodmult(Z, Un[n].T, n+1)
    for i in range(len(Un)):
        new_T = nmodmult(Z, Un[n], n+1)



    if not saveSpace:
        return [new_T, Z, Un, Sn, Vn]
    return [Z, Un]
Ejemplo n.º 21
0
    def __init__(self,data,cov_matrix=False,loc=None):
        """Parameters
        ----------
        data : array of data, shape=(number points,number dim)
               If cov_matrix is True then data is the covariance matrix (see below)

        Keywords
        --------
        cov_matrix : bool (optional)
                     If True data is treated as a covariance matrix with shape=(number dim, number dim)
        loc        : the mean of the data if a covarinace matrix is given, shape=(number dim)
        """
        if cov_matrix:
            self.dim=data.shape[0]
            self.n=None
            self.data_t=None
            self.mu=loc
            self.evec,eval,V=sl.svd(data,full_matrices=False)
            self.sigma=sqrt(eval)
            self.Sigma=diag(1./self.sigma)
            self.B=dot(self.evec,self.Sigma)
            self.Binv=sl.inv(self.B)
        else:
            self.n,self.dim=data.shape #the shape of input data
            self.mu=data.mean(axis=0) #the mean of the data
            self.data_t=data-self.mu #remove the mean
            self.evec,eval,V=sl.svd(self.data_t.T,full_matrices=False) #get the eigenvectors (axes of the ellipsoid)
            data_p=dot(self.data_t,self.evec) #project the data onto the eigenvectors
            self.sigma=data_p.std(axis=0) #get the spread of the distribution (the axis ratos for the ellipsoid)
            self.Sigma=diag(1./self.sigma) #the eigenvalue matrix for the ellipsoid equation
            self.B=dot(self.evec,self.Sigma) #used in the ellipsoid equation
            self.Binv=sl.inv(self.B) #also useful to have around
def Haffine_from_points(fp, tp, trans_type = 'affine'):
	""" find H, affine transformation, such that 
		tp is affine transf of fp"""

	if fp.shape != tp.shape:
		raise RuntimeError, 'number of points do not match'

	#condition points
	#-from points-
	m = mean(fp[:2], axis=1)
	maxstd = max(std(fp[:2], axis=1))+1e-9
	C1 = diag([1/maxstd, 1/maxstd, 1]) 
	C1[0][2] = -m[0]/maxstd
	C1[1][2] = -m[1]/maxstd
	fp_cond = dot(C1,fp)

	#-to points-
	m = mean(tp[:2], axis=1)
	C2 = C1.copy() #must use same scaling for both point sets
	C2[0][2] = -m[0]/maxstd
	C2[1][2] = -m[1]/maxstd
	tp_cond = dot(C2,tp)

	if (trans_type == 'affine'):
	
		#conditioned points have mean zero, so translation is zero
		A = concatenate((fp_cond[:2],tp_cond[:2]), axis=0)
		U,S,V = linalg.svd(A.T)
	
		#create B and C matrices as Hartley-Zisserman (2:nd ed) p 130.
		tmp = V[:2].T
		B = tmp[:2]
		C = tmp[2:4]
	
		tmp2 = concatenate((dot(C,linalg.pinv(B)),zeros((2,1))), axis=1) 
		H = vstack((tmp2,[0,0,1]))
	
		#decondition
		H = dot(linalg.inv(C2),dot(H,C1))

	elif trans_type == 'homography':
		# create matrix for linear method, 2 rows for each correspondence pair
	    nbr_correspondences = fp.shape[1]
	    A = zeros((2*nbr_correspondences,9))
	    for i in range(nbr_correspondences):        
	        A[2*i] = [-fp[0][i],-fp[1][i],-1,0,0,0,
	                    tp[0][i]*fp[0][i],tp[0][i]*fp[1][i],tp[0][i]]
	        A[2*i+1] = [0,0,0,-fp[0][i],-fp[1][i],-1,
	                    tp[1][i]*fp[0][i],tp[1][i]*fp[1][i],tp[1][i]]
	    
	    U,S,V = linalg.svd(A)
	    H = V[8].reshape((3,3))    
	    
	    # decondition
	    H = dot(linalg.inv(C2),dot(H,C1))
	    
	    # normalize and return
	
	return H / H[2][2]
Ejemplo n.º 23
0
def svd2(T):
    m,n = T.shape
    if m>=n:
        U,S,V = linalg.svd(T,0)
        return U,np.diag(S),V
    else: 
        V,S,U = linalg.svd(T.transpose().conj(),0)
        return U.conj().transpose(),np.diag(S),V.conj().transpose()
Ejemplo n.º 24
0
def null(constraintMat,eps=1e-6,verbose=False): 
    """
    Finds the null space of constraintMat.
    
    Currently does not take into account the fact that
    constraintMat is sparse.
    
    TODO: 1) exploit sparsity
          2) compute only the vectors that go with small sing. vals. 
    """
#    ipshell('hi')
    print "Computing Null space"
    print "constraintMat.shape =",constraintMat.shape
     
    if verbose:
        print 'compuing null space'
    if constraintMat.shape[1]>constraintMat.shape[0]:
        # "short-fat" matrix: add row of zeros
        m = constraintMat.shape[1]-constraintMat.shape[0]
        n = constraintMat.shape[1]
        # zero padding    
        constraintMat_aug = np.vstack([constraintMat,np.zeros((m,n))])
        u,s,vh = svd(constraintMat_aug,full_matrices=True)  
    else:
        # :skinny tall"
        debug = True
        if not debug:
            u,s,vh = svd(constraintMat,full_matrices=True)
        else:
            
            
#            constraintMat=sparse.csr_matrix(constraintMat)
#            tmp=svds(constraintMat,k=2,which='SM')
             
             
            if 1:
                try:
                    print "trying svd on constraintMat"
                    u,s,vh = svd(constraintMat,full_matrices=True)
                except:
                    print "svd on constraintMat failed"
                    print "trying svd on constraintMat.T.dot(constraintMat)"
                    u,s,vh = svd(constraintMat.T.dot(constraintMat),full_matrices=True)
                    vh=u.T
                    del u
            else:
                print "trying svd on constraintMat.T.dot(constraintMat)"
                u,s,vh = svd(constraintMat.T.dot(constraintMat),full_matrices=True)
                vh=u.T
                del u
#                ipshell('svd error')
#                raise
        
    v = vh.T     
    singular_vals_are_small = s < eps
    if verbose:
        print "dim(kernel)=",singular_vals_are_small.sum()
    return v[:,singular_vals_are_small]
Ejemplo n.º 25
0
 def train(self, X):
     m, n = X.shape
     self.mean = np.average(X, axis=0).reshape(X.shape[1],)
     X_ = X - self.mean
     if m > n:
         U, S, V = svd(X_.transpose())
         self.U = U
     else:
         U, S, V = svd(X_)
         self.U = V.transpose()
 def do(self, *args):
     print "applying SVD ..."
     for network in self.main_loop.model.top_bricks[-1].networks:
         forw_rnn_w_svd = svd(network.children[0].W.get_value())
         back_rnn_w_svd = svd(network.children[1].W.get_value())
         self.main_loop.log.current_row['svd_forw_' +
                                        network.name] = forw_rnn_w_svd[1]
         self.main_loop.log.current_row['svd_back_' +
                                        network.name] = back_rnn_w_svd[1]
     print "SVD finished."
Ejemplo n.º 27
0
def randomized_pca(A, n_components, n_oversamples=10, n_iter="auto",
                   flip_sign=True, random_state=0):
    """Compute the randomized PCA decomposition of a given matrix.

    This method differs from the scikit-learn implementation in that it supports
    and handles sparse matrices well.

    """
    if n_iter == "auto":
        # Checks if the number of iterations is explicitly specified
        # Adjust n_iter. 7 was found a good compromise for PCA. See sklearn #5299
        n_iter = 7 if n_components < .1 * min(A.shape) else 4

    n_samples, n_features = A.shape

    c = np.atleast_2d(A.mean(axis=0))

    if n_samples >= n_features:
        Q = random_state.normal(size=(n_features, n_components + n_oversamples))
        Q = safe_sparse_dot(A, Q) - safe_sparse_dot(c, Q)

        # Normalized power iterations
        for _ in range(n_iter):
            Q = safe_sparse_dot(A.T, Q) - safe_sparse_dot(c.T, Q.sum(axis=0)[None, :])
            Q, _ = lu(Q, permute_l=True)
            Q = safe_sparse_dot(A, Q) - safe_sparse_dot(c, Q)
            Q, _ = lu(Q, permute_l=True)

        Q, _ = qr(Q, mode="economic")

        QA = safe_sparse_dot(A.T, Q) - safe_sparse_dot(c.T, Q.sum(axis=0)[None, :])
        R, s, V = svd(QA.T, full_matrices=False)
        U = Q.dot(R)

    else:  # n_features > n_samples
        Q = random_state.normal(size=(n_samples, n_components + n_oversamples))
        Q = safe_sparse_dot(A.T, Q) - safe_sparse_dot(c.T, Q.sum(axis=0)[None, :])

        # Normalized power iterations
        for _ in range(n_iter):
            Q = safe_sparse_dot(A, Q) - safe_sparse_dot(c, Q)
            Q, _ = lu(Q, permute_l=True)
            Q = safe_sparse_dot(A.T, Q) - safe_sparse_dot(c.T, Q.sum(axis=0)[None, :])
            Q, _ = lu(Q, permute_l=True)

        Q, _ = qr(Q, mode="economic")

        QA = safe_sparse_dot(A, Q) - safe_sparse_dot(c, Q)
        U, s, R = svd(QA, full_matrices=False)
        V = R.dot(Q.T)

    if flip_sign:
        U, V = svd_flip(U, V)

    return U[:, :n_components], s[:n_components], V[:n_components, :]
Ejemplo n.º 28
0
def image_svd(n):
    img=mpimg.imread('image.jpg')
    [r,g,b] = [img[:,:,i] for i in range(3)]
    r_1,r_2,r_3 = sp.svd(r)
    g_1,g_2,g_3 = sp.svd(g)
    b_1,b_2,b_3 = sp.svd(b)
    r2_nonzero=(r_2!=0).sum()
    g2_nonzero=(g_2!=0).sum()
    b2_nonzero=(b_2!=0).sum()
    print("The number of non zero elements in decompose sigma of red, green, blue matrices are", r2_nonzero,"," ,g2_nonzero,"and" ,b2_nonzero, "respectively.")
    
    r_2[n:800]=np.zeros_like(r_2[n:800])
    g_2[n:800]=np.zeros_like(g_2[n:800])
    b_2[n:800]=np.zeros_like(b_2[n:800])
    
    # change the dimension to (800,1000) 
    r_2=sp.diagsvd(r_2,800,1000)
    g_2=sp.diagsvd(g_2,800,1000)
    b_2=sp.diagsvd(b_2,800,1000)
    
    #dot multiplication
    r_new=np.dot(r_1, np.dot(r_2,r_3))
    g_new=np.dot(g_1, np.dot(g_2,g_3))
    b_new=np.dot(b_1, np.dot(b_2,b_3))

    img[:,:,0]=r_new
    img[:,:,1]=g_new
    img[:,:,2]=b_new
    
    #plot the images
    fig = plt.figure(2)
    ax1 = fig.add_subplot(2,2,1)
    ax2 = fig.add_subplot(2,2,2)
    ax3 = fig.add_subplot(2,2,3)
    ax4 = fig.add_subplot(2,2,4)
    
    ax1.imshow(img)
    ax2.imshow(r, cmap = 'Reds')
    ax3.imshow(g, cmap = 'Greens')
    ax4.imshow(b, cmap = 'Blues')
    plt.show()
    
    #original image
    img=mpimg.imread('image.jpg')
    [r,g,b]=[img[:,:,i] for i in range(3)]
    fig=plt.figure(1)    
    ax1 =  fig.add_subplot(2,2,1)
    ax2 = fig.add_subplot(2,2,2)
    ax3 = fig.add_subplot(2,2,3)
    ax4 = fig.add_subplot(2,2,4)
    ax1.imshow(img)
    ax2.imshow(r, cmap = 'Reds')
    ax3.imshow(g, cmap = 'Greens')
    ax4.imshow(b, cmap = 'Blues')
    plt.show()
Ejemplo n.º 29
0
def normalizedInputData(data, sfm):
    data_PCA = {}
    corr_region_part = data[sfm.getRegionName()].dot(data[sfm.getRegionName()].T)
    U, S = svd(corr_region_part)[0:2]
    num_factor_sectors = sfm.getNumFactorSectors()
    data_PCA[sfm.getRegionName()] = pd.DataFrame(data= U[:,0:sfm.getNumFactorRegion()].dot(np.diag(S[0:sfm.getNumFactorRegion()]**0.5)))
    for SID in np.arange(len(sfm.getNumFactorSectors())):
        corr_sector_part = data[sfm.getSectorNames(SID)].dot(data[sfm.getSectorNames(SID)].T)
        U, S = svd(corr_sector_part)[0:2]
        data_PCA[sfm.getSectorNames(SID)] = pd.DataFrame(data = U[:,0:num_factor_sectors[SID]].dot(np.diag(S[0:num_factor_sectors[SID]]**0.5)))
    return data_PCA
Ejemplo n.º 30
0
def randproj( m , n ):
    """creates a random matrix in R^{m,n}"""
    # maps from R^n to R^m
    if m >= n:
        A = np.random.rand( m , n )
        [u,s,vt] = svd(A, full_matrices=False)
        return u
    else:
        A = np.random.rand( n , m )
        [u,s,vt] = svd(A, full_matrices=False)
        return np.transpose(u)
Ejemplo n.º 31
0
def ANN(X, y, attributeNames, C, hidden, K):
    """docstring for ANN 
    X: matrix of data
    y: vector of classes 
    attributeNames : set of attributeNames
    C: number of classes to classify 
    hidden : number of hidden attributes
    K: number of crossvalidation folds 
    """

    # Load Matlab data file and extract variables of interest
    #mat_data = loadmat('../Data/wine2.mat')
    #attributeNames = [name[0] for name in mat_data['attributeNames'][0]]
    #X = np.matrix(mat_data['X'])
    #y = np.matrix(mat_data['y'])
    N, M = X.shape
    #C = 2

    # Normalize and compute Principal Components
    Y = stats.zscore(X, 0)
    U, S, V = linalg.svd(Y, full_matrices=False)
    V = mat(V).T

    # Components to be included as features
    k_pca = 8
    X = X * V[:, 0:k_pca]
    N, M = X.shape

    # Parameters for neural network classifier
    n_hidden_units = hidden  # number of hidden units
    n_train = 1  # number of networks trained in each k-fold

    learning_rate = 0.0005  # rate of weights adaptation
    learning_goal = 200.0  # stop criterion 1 (train mse to be reached)
    max_epochs = 32  # stop criterion 2 (max epochs in training)

    # K-fold crossvalidation
    #K = 5                   # only five folds to speed up this example
    CV = cross_validation.KFold(N, K, shuffle=True)

    # Variable for classification error
    errors = np.zeros(K)
    error_hist = np.zeros((max_epochs, K))
    bestnet = list()
    k = 0
    for train_index, test_index in CV:
        print('\nCrossvalidation fold: {0}/{1}'.format(k + 1, K))

        # extract training and test set for current CV fold
        X_train = X[train_index, :]
        y_train = y[train_index, :]
        X_test = X[test_index, :]
        y_test = y[test_index, :]

        best_train_error = 1e100
        for i in range(n_train):
            # Create randomly initialized network with 2 layers
            ann = nl.net.newff(
                [[-1, 1]] * M, [n_hidden_units, 1],
                [nl.trans.LogSig(), nl.trans.LogSig()])
            # train network
            train_error = ann.train(X_train,
                                    y_train,
                                    goal=learning_goal,
                                    epochs=max_epochs,
                                    lr=learning_rate,
                                    show=round(max_epochs / 8))
            if train_error[-1] < best_train_error:
                bestnet.append(ann)
                best_train_error = train_error[-1]
                error_hist[range(len(train_error)), k] = train_error

        y_est = bestnet[k].sim(X_test)
        y_est = (y_est > .5).astype(int)
        errors[k] = (y_est != y_test).sum().astype(float) / y_test.shape[0]
        k += 1

    # Print the average classification error rate
    print('Error rate: {0}%'.format(100 * mean(errors)))

    # Display the decision boundry for the last crossvalidation fold.
    # (create grid of points, compute network output for each point, color-code and plot).

    grid_range = [-1, 20, -1, 5]
    delta = 0.1
    levels = 100
    a = arange(grid_range[0], grid_range[1], delta)
    b = arange(grid_range[2], grid_range[3], delta)
    A, B = meshgrid(a, b)
    values = np.zeros(A.shape)

    show()
    # Display exemplary networks learning curve (best network of each fold)
    #figure(2); hold(True)
    #bn_id = argmax(error_hist[-1,:])
    #error_hist[error_hist==0] = learning_goal
    #for bn_id in range(K):
    #   plot(error_hist[:,bn_id]);
    #   xlabel('epoch');
    #   ylabel('train error (mse)');
    #   title('Learning curve (best for each CV fold)')
    #
    #plot(range(max_epochs), [learning_goal]*max_epochs, '-.')
    #
    #
    #show()
    pass
import numpy as np
import matplotlib.pyplot as plt
from scipy import linalg

from sklearn.decomposition import PCA, FactorAnalysis
from sklearn.covariance import ShrunkCovariance, LedoitWolf
from sklearn.cross_validation import cross_val_score
from sklearn.grid_search import GridSearchCV

###############################################################################
# Create the data

n_samples, n_features, rank = 1000, 50, 10
sigma = 1.
rng = np.random.RandomState(42)
U, _, _ = linalg.svd(rng.randn(n_features, n_features))
X = np.dot(rng.randn(n_samples, rank), U[:, :rank].T)

# Adding homoscedastic noise
X_homo = X + sigma * rng.randn(n_samples, n_features)

# Adding heteroscedastic noise
sigmas = sigma * rng.rand(n_features) + sigma / 2.
X_hetero = X + rng.randn(n_samples, n_features) * sigmas

###############################################################################
# Fit the models

n_components = np.arange(0, n_features, 5)  # options for n_components

Ejemplo n.º 33
0
from numpy import array
from numpy import diag
from numpy import dot
from numpy import zeros
from scipy.linalg import svd

A = array([[1, 2], [4, 5], [7, 8]])
print(A)
U, s, VT = svd(A)
print(U)
print(s)
print(VT)
Sigma = zeros((A.shape[0], A.shape[1]))
Sigma[:A.shape[1], :A.shape[1]] = diag(s)
B = U.dot(Sigma.dot(VT))
Ejemplo n.º 34
0
def bound_pseudo(
    arnoldifyer,
    Wt,
    g_norm=0.0,
    G_norm=0.0,
    GW_norm=0.0,
    WGW_norm=0.0,
    tol=1e-6,
    pseudo_type="auto",
    pseudo_kwargs=None,
    delta_n=20,
    terminate_factor=1.0,
):
    r"""Bound residual norms of next deflated system.

    :param arnoldifyer: an instance of
      :py:class:`~krypy.deflation.Arnoldifyer`.
    :param Wt: coefficients :math:`\tilde{W}\in\mathbb{C}^{n+d,k}` of the
      considered deflation vectors :math:`W` for the basis :math:`[V,U]`
      where ``V=last_solver.V`` and ``U=last_P.U``, i.e.,
      :math:`W=[V,U]\tilde{W}` and
      :math:`\mathcal{W}=\operatorname{colspan}(W)`. Must fulfill
      :math:`\tilde{W}^*\tilde{W}=I_k`.
    :param g_norm: norm :math:`\|g\|` of difference :math:`g=c-b` of
      right hand sides. Has to fulfill :math:`\|g\|<\|b\|`.
    :param G_norm: norm :math:`\|G\|` of difference
      :math:`G=B-A` of operators.
    :param GW_norm: Norm :math:`\|G|_{\mathcal{W}}\|` of difference
      :math:`G=B-A` of operators restricted to :math:`\mathcal{W}`.
    :param WGW_norm: Norm :math:`\|\langle W,GW\rangle\|_2`.
    :param pseudo_type: One of

      * ``'auto'``: determines if :math:`\hat{H}` is non-normal, normal or
        Hermitian and uses the corresponding mode (see other options below).
      * ``'nonnormal'``: the pseudospectrum of the Hessenberg matrix
        :math:`\hat{H}` is used (involves one computation of a pseudospectrum)
      * ``'normal'``: the pseudospectrum of :math:`\hat{H}` is computed
        efficiently by the union of circles around the eigenvalues.
      * ``'hermitian'``: the pseudospectrum of :math:`\hat{H}` is computed
        efficiently by the union of intervals around the eigenvalues.
      * ``'contain'``: the pseudospectrum of the extended Hessenberg matrix
        :math:`\begin{bmatrix}\hat{H}\\S_i\end{bmatrix}` is used
        (pseudospectrum has to be re computed for each iteration).
      * ``'omit'``: do not compute the pseudospectrum at all and just use the
        residual bounds from the approximate Krylov subspace.
    :param pseudo_kwargs: (optional) arguments that are passed to the method
      that computes the pseudospectrum.
    :param terminate_factor: (optional) terminate the computation if the ratio
      of two subsequent residual norms is larger than the provided factor.
      Defaults to 1.
    """
    if pseudo_kwargs is None:
        pseudo_kwargs = {}

    # Arnoldify!
    Hh, Rh, q_norm, vdiff_norm, PWAW_norm = arnoldifyer.get(Wt)

    # get original linear system
    ls_orig = arnoldifyer._deflated_solver.linear_system

    k = Wt.shape[1]
    if k > 0:
        # smallest singular value of W^*AW
        WAW = Wt.T.conj().dot(arnoldifyer.J.dot(arnoldifyer.L.dot(Wt)))
        sigma_min = numpy.min(scipy.linalg.svdvals(WAW))

        if sigma_min <= WGW_norm:
            raise utils.AssumptionError(
                "sigma_min(W^*AW) > ||W^*GW|| not satisfied.")
        eta = GW_norm / (sigma_min - WGW_norm)
    else:
        eta = 0.0
    b_norm = ls_orig.MMlb_norm
    beta = PWAW_norm * (eta * (b_norm + g_norm) + g_norm) + vdiff_norm

    # check assumption on g_norm and b_norm
    if g_norm >= b_norm:
        raise utils.AssumptionError("||g_norm|| < ||b_norm|| not satisfied")

    # compute residual norms of Hh*z=e_1*b_norm
    ls_small = linsys.LinearSystem(
        Hh,
        numpy.eye(Hh.shape[0], 1) * q_norm,
        normal=ls_orig.normal,
        self_adjoint=ls_orig.self_adjoint,
        positive_definite=ls_orig.positive_definite,
    )

    Solver = type(arnoldifyer._deflated_solver)
    if issubclass(Solver, linsys.Minres) or issubclass(Solver, linsys.Gmres):
        aresnorms = utils.get_residual_norms(Hh,
                                             self_adjoint=ls_orig.self_adjoint)
    else:
        # TODO: compute residuals more efficiently for CG
        try:
            solver = Solver(ls_small, tol=tol, maxiter=Hh.shape[0])
        except utils.ConvergenceError as e:
            # use all residuals that have been computed
            # (useful for short recurrences)
            solver = e.solver
        aresnorms = numpy.array(solver.resnorms)
    # absolute residual norm
    aresnorms = aresnorms * q_norm

    if pseudo_type == "omit":
        return aresnorms / (b_norm - g_norm)

    # spectrum of Hh
    evals, evecs = scipy.linalg.eig(Hh)
    if ls_small.self_adjoint:
        evals = numpy.real(evals)

    # norm of Hh
    Hh_norm = numpy.linalg.norm(Hh, 2)

    def _auto():
        """determine pseudo automatically"""
        # is Hh Hermitian?
        if numpy.linalg.norm(Hh - Hh.T.conj(), 2) < 1e-14 * Hh_norm:
            return "hermitian"

        # is Hh normal?
        if numpy.linalg.cond(evecs, 2) < 1 + 1e-14:
            return "normal"

        return "nonnormal"

    if pseudo_type == "auto":
        pseudo_type = _auto()

    # for delta >= min(|\lambda|), the pseudospectrum will contain zero and
    # the thus polymax > 1. nevertheless, the bound may provide useful
    # information in early iterations with large values of delta.
    # Therefore, the maximal perturbation is chosen as the maximal
    # eigenvalue of Hh
    delta_max = 1e2 * numpy.max(numpy.abs(evals))

    # minimal delta is defined via Rh
    # HACK until numpy.linal.svd (and thus numpy.linalg.norm) is fixed
    from scipy.linalg import svd

    _, Rhsvd, _ = svd(Rh[:, :1])
    delta_min = PWAW_norm * (eta *
                             (Hh_norm + G_norm) + G_norm) + numpy.max(Rhsvd)
    if delta_min == 0:
        delta_min = 1e-16

    import pseudopy

    if not ls_small.normal:
        # construct pseudospectrum for the expected range
        pseudo = pseudopy.NonnormalAuto(Hh, delta_min * 0.99, delta_max * 1.01,
                                        **pseudo_kwargs)
    elif not ls_small.self_adjoint:
        pseudo = pseudopy.NormalEvals(evals)
    else:
        pseudo = None

    bounds = [aresnorms[0]]
    for i in range(1, len(aresnorms)):
        # compute roots of polynomial
        if issubclass(Solver, linsys.Cg):
            roots = scipy.linalg.eigvalsh(Hh[:i, :i])
        else:
            # TODO: more stable way of computing the roots of the MINRES
            #       poly with exploitation of symmetry?
            HhQ, HhR = scipy.linalg.qr(Hh[:i + 1, :i], mode="economic")
            roots_inv = scipy.linalg.eigvals(HhQ[:i, :].T.conj(), HhR)
            roots = 1.0 / roots_inv[numpy.abs(roots_inv) > 1e-14]

        if ls_small.self_adjoint:
            roots = numpy.real(roots)

        # compute polynomial
        p = utils.NormalizedRootsPolynomial(roots)
        if ls_small.self_adjoint:
            p_minmax_candidates = p.minmax_candidates()

        # absolute residual
        aresnorm = aresnorms[i]

        # perturbation
        # HACK until numpy.linal.svd (and thus numpy.linalg.norm) is fixed
        _, Rhsvd, _ = svd(Rh[:, :i])
        Rhnrm = numpy.max(Rhsvd)
        epsilon = PWAW_norm * (eta * (Hh_norm + G_norm) + G_norm) + Rhnrm
        # + numpy.linalg.norm(Rh[:, :i], 2)
        if epsilon == 0:
            epsilon = 1e-16

        if pseudo_type == "contain":
            raise NotImplementedError("contain not yet implemented")

        # exit if epsilon >= delta_max
        if epsilon >= delta_max:
            break

        delta_log_range = numpy.linspace(numpy.log10(1.01 * epsilon),
                                         numpy.log10(delta_max),
                                         delta_n + 2)[0:-1]

        def compute_pseudo(delta_log):
            delta = 10**delta_log
            if ls_small.self_adjoint:
                # pseudospectrum are intervals
                pseudo_intervals = utils.Intervals(
                    [utils.Interval(ev - delta, ev + delta) for ev in evals])

                # add roots of first derivative of p
                candidates = []
                for candidate in p_minmax_candidates:
                    if pseudo_intervals.contains(candidate):
                        candidates.append(candidate)
                all_candidates = numpy.r_[pseudo_intervals.get_endpoints(),
                                          numpy.array(candidates)]

                # evaluate polynomial
                polymax = numpy.max(numpy.abs(p(all_candidates)))
                pseudolen = 2 * delta

            else:
                # get pseudospectrum paths
                pseudo_path = pseudo.contour_paths(delta)

                # length of boundary
                pseudolen = pseudo_path.length()

                # evaluate polynomial on points of path
                if pseudolen > 0:
                    polymax = numpy.max(numpy.abs(p(pseudo_path.vertices())))
                else:
                    polymax = numpy.Inf

            # compute THE bound
            return (pseudolen / (2 * numpy.pi * delta) *
                    (epsilon / (delta - epsilon) *
                     (q_norm + beta) + beta) * polymax)

        # minimization
        from scipy.optimize import minimize_scalar

        opt_res = minimize_scalar(
            compute_pseudo,
            bounds=(delta_log_range[0], delta_log_range[-1]),
            method="bounded",
            options={"maxiter": delta_n},
        )
        # the delta with minimal value is min_delta = 10**opt_res.x
        min_val = opt_res.fun

        # minimal bound value
        boundval = aresnorm + min_val

        # if not increasing: append to bounds
        if i > 1 and boundval / bounds[-1] > terminate_factor:
            break
        else:
            bounds.append(numpy.min([boundval, bounds[-1]]))
    return numpy.array(bounds) / (b_norm - g_norm)
Ejemplo n.º 35
0
def mixed_norm(evoked, forward, noise_cov, alpha, loose='auto', depth=0.8,
               maxit=3000, tol=1e-4, active_set_size=10, pca=None,
               debias=True, time_pca=True, weights=None, weights_min=None,
               solver='auto', n_mxne_iter=1, return_residual=False,
               return_as_dipoles=False, dgap_freq=10, rank=None,
               verbose=None):
    """Mixed-norm estimate (MxNE) and iterative reweighted MxNE (irMxNE).

    Compute L1/L2 mixed-norm solution [1]_ or L0.5/L2 [2]_ mixed-norm
    solution on evoked data.

    Parameters
    ----------
    evoked : instance of Evoked or list of instances of Evoked
        Evoked data to invert.
    forward : dict
        Forward operator.
    noise_cov : instance of Covariance
        Noise covariance to compute whitener.
    alpha : float in range [0, 100)
        Regularization parameter. 0 means no regularization, 100 would give 0
        active dipole.
    loose : float in [0, 1] | 'auto'
        Value that weights the source variances of the dipole components
        that are parallel (tangential) to the cortical surface. If loose
        is 0 then the solution is computed with fixed orientation.
        If loose is 1, it corresponds to free orientations.
        The default value ('auto') is set to 0.2 for surface-oriented source
        space and set to 1.0 for volumic or discrete source space.
    %(depth)s
    maxit : int
        Maximum number of iterations.
    tol : float
        Tolerance parameter.
    active_set_size : int | None
        Size of active set increment. If None, no active set strategy is used.
    pca : bool
        If True the rank of the data is reduced to true dimension.
    debias : bool
        Remove coefficient amplitude bias due to L1 penalty.
    time_pca : bool or int
        If True the rank of the concatenated epochs is reduced to
        its true dimension. If is 'int' the rank is limited to this value.
    weights : None | array | SourceEstimate
        Weight for penalty in mixed_norm. Can be None, a
        1d array with shape (n_sources,), or a SourceEstimate (e.g. obtained
        with wMNE, dSPM, or fMRI).
    weights_min : float
        Do not consider in the estimation sources for which weights
        is less than weights_min.
    solver : 'prox' | 'cd' | 'bcd' | 'auto'
        The algorithm to use for the optimization. 'prox' stands for
        proximal iterations using the FISTA algorithm, 'cd' uses
        coordinate descent, and 'bcd' applies block coordinate descent.
        'cd' is only available for fixed orientation.
    n_mxne_iter : int
        The number of MxNE iterations. If > 1, iterative reweighting
        is applied.
    return_residual : bool
        If True, the residual is returned as an Evoked instance.
    return_as_dipoles : bool
        If True, the sources are returned as a list of Dipole instances.
    dgap_freq : int or np.inf
        The duality gap is evaluated every dgap_freq iterations. Ignored if
        solver is 'cd'.
    %(rank_None)s

        .. versionadded:: 0.18
    %(verbose)s

    Returns
    -------
    stc : SourceEstimate | list of SourceEstimate
        Source time courses for each evoked data passed as input.
    residual : instance of Evoked
        The residual a.k.a. data not explained by the sources.
        Only returned if return_residual is True.

    See Also
    --------
    tf_mixed_norm

    References
    ----------
    .. [1] A. Gramfort, M. Kowalski, M. Hamalainen,
       "Mixed-norm estimates for the M/EEG inverse problem using accelerated
       gradient methods", Physics in Medicine and Biology, 2012.
       https://doi.org/10.1088/0031-9155/57/7/1937

    .. [2] D. Strohmeier, Y. Bekhti, J. Haueisen, A. Gramfort,
       "The Iterative Reweighted Mixed-Norm Estimate for Spatio-Temporal
       MEG/EEG Source Reconstruction", IEEE Transactions of Medical Imaging,
       Volume 35 (10), pp. 2218-2228, 2016.
    """
    if not (0. <= alpha < 100.):
        raise ValueError('alpha must be in [0, 100). '
                         'Got alpha = %s' % alpha)
    if n_mxne_iter < 1:
        raise ValueError('MxNE has to be computed at least 1 time. '
                         'Requires n_mxne_iter >= 1, got %d' % n_mxne_iter)
    if dgap_freq <= 0.:
        raise ValueError('dgap_freq must be a positive integer.'
                         ' Got dgap_freq = %s' % dgap_freq)

    if pca is None:
        pca = True
    else:
        warn('pca argument is deprecated and will be removed in 0.19, do '
             'not set it. It should not affect results.', DeprecationWarning)
    if not isinstance(evoked, list):
        evoked = [evoked]

    _check_reference(evoked[0])

    all_ch_names = evoked[0].ch_names
    if not all(all_ch_names == evoked[i].ch_names
               for i in range(1, len(evoked))):
        raise Exception('All the datasets must have the same good channels.')

    forward, gain, gain_info, whitener, source_weighting, mask = _prepare_gain(
        forward, evoked[0].info, noise_cov, pca, depth, loose, rank,
        weights, weights_min)

    sel = [all_ch_names.index(name) for name in gain_info['ch_names']]
    M = np.concatenate([e.data[sel] for e in evoked], axis=1)

    # Whiten data
    logger.info('Whitening data matrix.')
    M = np.dot(whitener, M)

    if time_pca:
        U, s, Vh = linalg.svd(M, full_matrices=False)
        if not isinstance(time_pca, bool) and isinstance(time_pca, int):
            U = U[:, :time_pca]
            s = s[:time_pca]
            Vh = Vh[:time_pca]
        M = U * s

    # Scaling to make setting of alpha easy
    n_dip_per_pos = 1 if is_fixed_orient(forward) else 3
    alpha_max = norm_l2inf(np.dot(gain.T, M), n_dip_per_pos, copy=False)
    alpha_max *= 0.01
    gain /= alpha_max
    source_weighting /= alpha_max

    if n_mxne_iter == 1:
        X, active_set, E = mixed_norm_solver(
            M, gain, alpha, maxit=maxit, tol=tol,
            active_set_size=active_set_size, n_orient=n_dip_per_pos,
            debias=debias, solver=solver, dgap_freq=dgap_freq, verbose=verbose)
    else:
        X, active_set, E = iterative_mixed_norm_solver(
            M, gain, alpha, n_mxne_iter, maxit=maxit, tol=tol,
            n_orient=n_dip_per_pos, active_set_size=active_set_size,
            debias=debias, solver=solver, dgap_freq=dgap_freq, verbose=verbose)

    if time_pca:
        X = np.dot(X, Vh)
        M = np.dot(M, Vh)

    # Compute estimated whitened sensor data
    M_estimated = np.dot(gain[:, active_set], X)

    if mask is not None:
        active_set_tmp = np.zeros(len(mask), dtype=np.bool)
        active_set_tmp[mask] = active_set
        active_set = active_set_tmp
        del active_set_tmp

    if active_set.sum() == 0:
        raise Exception("No active dipoles found. alpha is too big.")

    # Reapply weights to have correct unit
    X = _reapply_source_weighting(X, source_weighting, active_set)

    outs = list()
    residual = list()
    cnt = 0
    for e in evoked:
        tmin = e.times[0]
        tstep = 1.0 / e.info['sfreq']
        Xe = X[:, cnt:(cnt + len(e.times))]
        if return_as_dipoles:
            out = _make_dipoles_sparse(
                Xe, active_set, forward, tmin, tstep,
                M[:, cnt:(cnt + len(e.times))],
                M_estimated[:, cnt:(cnt + len(e.times))], verbose=None)
        else:
            out = _make_sparse_stc(Xe, active_set, forward, tmin, tstep)
        outs.append(out)
        cnt += len(e.times)

        if return_residual:
            residual.append(_compute_residual(forward, e, Xe, active_set,
                                              gain_info))

    logger.info('[done]')

    if len(outs) == 1:
        out = outs[0]
        if return_residual:
            residual = residual[0]
    else:
        out = outs

    if return_residual:
        out = out, residual

    return out
Ejemplo n.º 36
0
print f([3.2, 0.1])
x_min = optimize.minimize(f, [5, 5])
print x_min.x

from scipy import linalg

a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]])
b = np.array([2, 4, -1])

x = linalg.solve(a, b)
print x
print np.dot(a, x)

X = np.random.rand(4, 3)
U, D, V = linalg.svd(X)
print U.shape, D.shape, V.shape
print type(U), type(D), type(V)

from matplotlib import pylab as plt

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.show()

x = np.arange(-10, 10, 0.1)
y = x**3
plt.plot(x, y)
plt.show()

import numpy as np
from scipy import interpolate
Ejemplo n.º 37
0
        ## 3) Between variance scaling
        # Overall mean
        xbar = np.dot(self.priors_, self.means_)
        # Scale weighted centers
<<<<<<< HEAD
        X = np.dot(((np.sqrt((n_samples * self.priors_) * fac)) *
                    (means - xbar).T).T, scaling)
=======
        X = np.dot(((np.sqrt((n_samples * self.priors_)*fac)) *
                          (means - xbar).T).T, scaling)
        
>>>>>>> remote
        # Centers are living in a space with n_classes-1 dim (maximum)
        # Use svd to find projection in the space spanned by the
        # (n_classes) centers
        _, S, V = linalg.svd(X, full_matrices=0)

        rank = np.sum(S > tol * S[0])
<<<<<<< HEAD
=======
        print S, rank
>>>>>>> remote
        # compose the scalings
        self.scaling = np.dot(scaling, V.T[:, :rank])
        self.xbar_ = xbar
        # weight vectors / centroids
        self.coef_ = np.dot(self.means_ - self.xbar_, self.scaling)
        self.intercept_ = -0.5 * np.sum(self.coef_ ** 2, axis=1) + \
                           np.log(self.priors_)

        self.classes = classes
Ejemplo n.º 38
0
    def svd_reduce(self, max_rank, to_retain=None):
        """
        Reduce the rank of the matrix by retaining some SVD components.

        This corresponds to the \"Broyden Rank Reduction Inverse\"
        algorithm described in [1]_.

        Note that the SVD decomposition can be done by solving only a
        problem whose size is the effective rank of this matrix, which
        is viable even for large problems.

        Parameters
        ----------
        max_rank : int
            Maximum rank of this matrix after reduction.
        to_retain : int, optional
            Number of SVD components to retain when reduction is done
            (ie. rank > max_rank). Default is ``max_rank - 2``.

        References
        ----------
        .. [1] B.A. van der Rotten, PhD thesis,
           \"A limited memory Broyden method to solve high-dimensional
           systems of nonlinear equations\". Mathematisch Instituut,
           Universiteit Leiden, The Netherlands (2003).

           https://web.archive.org/web/20161022015821/http://www.math.leidenuniv.nl/scripties/Rotten.pdf

        """
        if self.collapsed is not None:
            return

        p = max_rank
        if to_retain is not None:
            q = to_retain
        else:
            q = p - 2

        if self.cs:
            p = min(p, len(self.cs[0]))
        q = max(0, min(q, p - 1))

        m = len(self.cs)
        if m < p:
            # nothing to do
            return

        C = np.array(self.cs).T
        D = np.array(self.ds).T

        D, R = qr(D, mode='economic')
        C = dot(C, R.T.conj())

        U, S, WH = svd(C, full_matrices=False, compute_uv=True)

        C = dot(C, inv(WH))
        D = dot(D, WH.T.conj())

        for k in range(q):
            self.cs[k] = C[:, k].copy()
            self.ds[k] = D[:, k].copy()

        del self.cs[q:]
        del self.ds[q:]
Ejemplo n.º 39
0
 def calc(self):
     self.U, self.S, self.Vt = svd(self.A)
Ejemplo n.º 40
0
def picard(X,
           fun='tanh',
           n_components=None,
           ortho=True,
           whiten=True,
           return_X_mean=False,
           max_iter=100,
           tol=1e-07,
           m=7,
           ls_tries=10,
           lambda_min=0.01,
           check_fun=True,
           w_init=None,
           fastica_it=None,
           random_state=None,
           verbose=False):
    """Perform Independent Component Analysis.

    Parameters
    ----------
    X : array-like, shape (n_features, n_samples)
        Training vector, where n_samples is the number of samples and
        n_features is the number of features.

    fun : str or class, optional
        Either a built in density model ('tanh', 'exp' and 'cube'), or a custom
        density.
        A custom density is a class that should contain two methods called
        'log_lik' and 'score_and_der'. See examples in the densities.py file.


    n_components : int, optional
        Number of components to extract. If None no dimension reduction
        is performed.

    ortho : bool, optional
        If True, uses Picard-O. Otherwise, uses the standard Picard. Picard-O
        tends to converge in fewer iterations, and finds both super Gaussian
        and sub Gaussian sources.

    whiten : boolean, optional
        If True perform an initial whitening of the data.
        If False, the data is assumed to have already been
        preprocessed: it should be centered, normed and white,
        otherwise you will get incorrect results.
        In this case the parameter n_components will be ignored.

    return_X_mean : bool, optional
        If True, X_mean is returned too.

    max_iter : int, optional
        Maximum number of iterations to perform.

    tol : float, optional
        A positive scalar giving the tolerance at which the
        un-mixing matrix is considered to have converged.

    m : int, optional
        Size of L-BFGS's memory.

    ls_tries : int, optional
        Number of attempts during the backtracking line-search.

    lambda_min : float, optional
        Threshold on the eigenvalues of the Hessian approximation. Any
        eigenvalue below lambda_min is shifted to lambda_min.

    check_fun : bool, optionnal
        Whether to check the fun provided by the user at the beginning of
        the run. Setting it to False is not safe.

    w_init : (n_components, n_components) array, optional
        Initial un-mixing array of dimension (n.comp,n.comp).
        If None (default) then a random rotation is used.

    random_state : int, RandomState instance or None, optional (default=None)
        Used to perform a random initialization when w_init is not provided.
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.

    fastica_it : int or None, optional (default=None)
        If an int, perform `fastica_it` iterations of FastICA before running
        Picard. It might help starting from a better point.

    verbose : bool, optional
        Prints informations about the state of the algorithm if True.

    Returns
    -------
    K : array, shape (n_components, n_features) | None.
        If whiten is 'True', K is the pre-whitening matrix that projects data
        onto the first n_components principal components. If whiten is 'False',
        K is 'None'.

    W : array, shape (n_components, n_components)
        Estimated un-mixing matrix.
        The mixing matrix can be obtained by::

            w = np.dot(W, K.T)
            A = w.T * (w * w.T).I

    Y : array, shape (n_components, n_samples) | None
        Estimated source matrix

    X_mean : array, shape (n_features,)
        The mean over features. Returned only if return_X_mean is True.
    """
    random_state = check_random_state(random_state)
    if not type(ortho) is bool:
        warnings.warn('ortho should be a boolean, got (ortho={}).'
                      'ortho is set to default: ortho=True.'.format(ortho))
    n, p = X.shape
    if fun == 'tanh':
        fun = Tanh()
    elif fun == 'exp':
        fun = Exp()
    elif fun == 'cube':
        fun = Cube()
    elif check_fun:
        check_density(fun)

    if not whiten and n_components is not None:
        warnings.warn('Whiten is set to false, ignoring parameter '
                      'n_components')
        n_components = None

    if n_components is None:
        n_components = min(n, p)

    # Centering the columns (ie the variables)
    X_mean = X.mean(axis=-1)
    X -= X_mean[:, np.newaxis]
    if whiten:
        # Whitening and preprocessing by PCA
        u, d, _ = linalg.svd(X, full_matrices=False)

        del _
        K = (u / d).T[:n_components]
        del u, d
        K *= np.sqrt(p)
        X1 = np.dot(K, X)
    else:
        # X must be casted to floats to avoid typing issues with numpy 2.0
        X1 = X.astype('float')

    # Initialize
    if w_init is None:
        w_init = np.asarray(random_state.normal(size=(n_components,
                                                      n_components)),
                            dtype=X1.dtype)
        # decorrelate w_init to make it white
        w_init = _sym_decorrelation(w_init)
    else:
        w_init = np.asarray(w_init)
        if w_init.shape != (n_components, n_components):
            raise ValueError(
                'w_init has invalid shape -- should be %(shape)s' %
                {'shape': (n_components, n_components)})

    if fastica_it is not None:
        w_init = _ica_par(X1, fun, fastica_it, w_init, verbose)

    X1 = np.dot(w_init, X1)

    args = (fun, m, max_iter, tol, lambda_min, ls_tries, verbose)
    if ortho:
        Y, W, infos = picardo(X1, *args)
    else:
        Y, W, infos = picard_standard(X1, *args)
    del X1
    W = np.dot(W, w_init)
    converged = infos['converged']
    if not converged:
        gradient_norm = infos['gradient_norm']
        warnings.warn(
            'Picard did not converge. Final gradient norm : %.4g.'
            ' Requested tolerance : %.4g. Consider'
            ' increasing the number of iterations or the tolerance.' %
            (gradient_norm, tol))
    if not whiten:
        K = None
    if return_X_mean:
        return K, W, Y, X_mean
    else:
        return K, W, Y
Ejemplo n.º 41
0
    def reduced_likelihood_function(self, theta=None):
        """
        This function determines the BLUP parameters and evaluates the reduced
        likelihood function for the given autocorrelation parameters theta.

        Maximizing this function wrt the autocorrelation parameters theta is
        equivalent to maximizing the likelihood of the assumed joint Gaussian
        distribution of the observations y evaluated onto the design of
        experiments X.

        Parameters
        ----------
        theta : array_like, optional
            An array containing the autocorrelation parameters at which the
            Gaussian Process model parameters should be determined.
            Default uses the built-in autocorrelation parameters
            (ie ``theta = self.theta_``).

        Returns
        -------
        reduced_likelihood_function_value : double
            The value of the reduced likelihood function associated to the
            given autocorrelation parameters theta.

        par : dict
            A dictionary containing the requested Gaussian Process model
            parameters:

                sigma2
                        Gaussian Process variance.
                beta
                        Generalized least-squares regression weights for
                        Universal Kriging or given beta0 for Ordinary
                        Kriging.
                gamma
                        Gaussian Process weights.
                C
                        Cholesky decomposition of the correlation matrix [R].
                Ft
                        Solution of the linear equation system : [R] x Ft = F
                G
                        QR decomposition of the matrix Ft.
        """
        check_is_fitted(self, "X")

        if theta is None:
            # Use built-in autocorrelation parameters
            theta = self.theta_

        # Initialize output
        reduced_likelihood_function_value = -np.inf
        par = {}

        # Retrieve data
        n_samples = self.X.shape[0]
        D = self.D
        ij = self.ij
        F = self.F

        if D is None:
            # Light storage mode (need to recompute D, ij and F)
            D, ij = l1_cross_distances(self.X)
            if (np.min(np.sum(D, axis=1)) == 0.
                    and self.corr != correlation.pure_nugget):
                raise Exception("Multiple X are not allowed")
            F = self.regr(self.X)

        # Set up R
        r = self.corr(theta, D)
        R = np.eye(n_samples) * (1. + self.nugget)
        R[ij[:, 0], ij[:, 1]] = r
        R[ij[:, 1], ij[:, 0]] = r

        # Cholesky decomposition of R
        try:
            C = linalg.cholesky(R, lower=True)
        except linalg.LinAlgError:
            return reduced_likelihood_function_value, par

        # Get generalized least squares solution
        Ft = linalg.solve_triangular(C, F, lower=True)
        try:
            Q, G = linalg.qr(Ft, econ=True)
        except:
            #/usr/lib/python2.6/dist-packages/scipy/linalg/decomp.py:1177:
            # DeprecationWarning: qr econ argument will be removed after scipy
            # 0.7. The economy transform will then be available through the
            # mode='economic' argument.
            Q, G = linalg.qr(Ft, mode='economic')

        sv = linalg.svd(G, compute_uv=False)
        rcondG = sv[-1] / sv[0]
        if rcondG < 1e-10:
            # Check F
            sv = linalg.svd(F, compute_uv=False)
            condF = sv[0] / sv[-1]
            if condF > 1e15:
                raise Exception("F is too ill conditioned. Poor combination "
                                "of regression model and observations.")
            else:
                # Ft is too ill conditioned, get out (try different theta)
                return reduced_likelihood_function_value, par

        Yt = linalg.solve_triangular(C, self.y, lower=True)
        if self.beta0 is None:
            # Universal Kriging
            beta = linalg.solve_triangular(G, np.dot(Q.T, Yt))
        else:
            # Ordinary Kriging
            beta = np.array(self.beta0)

        rho = Yt - np.dot(Ft, beta)
        sigma2 = (rho**2.).sum(axis=0) / n_samples
        # The determinant of R is equal to the squared product of the diagonal
        # elements of its Cholesky decomposition C
        detR = (np.diag(C)**(2. / n_samples)).prod()

        # Compute/Organize output
        reduced_likelihood_function_value = -sigma2.sum() * detR
        par['sigma2'] = sigma2 * self.y_std**2.
        par['beta'] = beta
        par['gamma'] = linalg.solve_triangular(C.T, rho)
        par['C'] = C
        par['Ft'] = Ft
        par['G'] = G

        return reduced_likelihood_function_value, par
Ejemplo n.º 42
0
def dcaFuse(X, Y, L):
    """
    X (p*n)
    Y (q*n)
    L (n)
    """

    p, n = X.shape
    q = Y.shape[0]

    # Normalize
    X = Normalize_feature(X)
    Y = Normalize_feature(Y)
    # X = (X - np.tile(np.mean(X, axis=1, dtype=np.float64).reshape([p,1]), n)) / np.tile(np.std(X, axis=1, dtype=np.float64).reshape([p,1]), n)
    # Y = (Y - np.tile(np.mean(Y, axis=1, dtype=np.float64).reshape([q,1]), n)) / np.tile(np.std(Y, axis=1, dtype=np.float64).reshape([q,1]), n)

    classes = np.unique(L)
    c = len(classes)
    nSample = np.zeros([c])

    cellX = []
    cellY = []

    for i in range(c):
        idx = np.squeeze(np.argwhere(L == classes[i]), axis=1)
        cellX.append(X[:, idx])
        cellY.append(Y[:, idx])
        nSample[i] = len(idx)

    meanX = np.mean(X, axis=1)
    meanY = np.mean(Y, axis=1)

    classMeanX = np.zeros([p, c])
    classMeanY = np.zeros([q, c])

    for i in range(c):
        classMeanX[:, i] = np.mean(cellX[i], axis=1)
        classMeanY[:, i] = np.mean(cellY[i], axis=1)

    PhibX = np.zeros([p, c])
    PhibY = np.zeros([q, c])

    for i in range(c):
        PhibX[:, i] = np.sqrt(nSample[i]) * (classMeanX[:, i] - meanX)
        PhibY[:, i] = np.sqrt(nSample[i]) * (classMeanY[:, i] - meanY)

    del L, idx, cellX, cellY, meanX, meanY, classMeanX, classMeanY
    """
        Diagolalize the between-class scatter matrix (Sb) for X and Y
    """

    cx, Wbx = Diag_Bx(PhibX)
    cy, Wby = Diag_Bx(PhibY)
    """
        Project data in a space, where the between-class scatter matrices are 
        identity and the classes are separated
    """

    r = min(cx, cy)
    Wbx = Wbx[:, :r]
    Wby = Wby[:, :r]

    Xp = np.dot(Wbx.T, X)
    Yp = np.dot(Wby.T, Y)
    """
        Unitize the between-set covariance matrix (Sxy)
        Note that Syx == Sxy'
    """

    Sxy = np.dot(Xp, Yp.T)  # Between-set covariance matrix
    Wcx, S_val, Wcy = svd(Sxy)
    S = np.diag(S_val)

    Wcx = np.dot(Wcx, fractional_matrix_power(S, -0.5))
    Wcy = np.dot(Wcy, fractional_matrix_power(S, -0.5))

    Xs = np.dot(Wcx.T, Xp)
    Ys = np.dot(Wcy.T, Yp)

    Ax = np.dot(Wcx.T, Wbx.T)
    Ay = np.dot(Wcy.T, Wby.T)

    return Xs, Ys, Ax, Ay
Ejemplo n.º 43
0
def SVD_ranking_scores(vect_sents, sub_dict, list_of_sentences_full,
                       list_of_sentences):
    from scipy.linalg import svd
    from numpy import diag
    from numpy import dot
    from numpy import zeros

    vect_sents = vect_sents.drop(columns='sentence')
    A = np.array(vect_sents)

    U, S, VT = svd(A)
    Concepts = ['Concept ' + str(x) for x in range(S.shape[0])]
    U_df = pd.DataFrame(U, list_of_sentences)
    VT_df = pd.DataFrame(VT, Concepts)

    # create m x n Sigma matrix
    Sigma = zeros((A.shape[0], A.shape[1]))
    # populate Sigma with n x n diagonal matrix

    Sigma[:A.shape[1], :A.shape[1]] = diag(S)
    Sigma_df = pd.DataFrame(
        Sigma, ['C_sent ' + str(x) for x in range(A.shape[0])],
        columns=['C_dict ' + str(x) for x in range(A.shape[1])])
    Sigma_df[3:] = 0
    # reconstruct matrix
    B = U.dot(Sigma.dot(VT))
    vect_sents_SVD = pd.DataFrame(B, columns=vect_sents.columns)
    topic_mean_SVD = np.mean(vect_sents_SVD)
    topic_std_SVD = np.std(vect_sents_SVD)
    topic_dot_SVD = np.multiply(topic_mean_SVD, topic_std_SVD)

    #%% study the singular vectors
    # VT
    topic_VT = pd.DataFrame(VT.T)
    topic_VT['Dictionaries'] = pd.DataFrame(vect_sents.columns)

    # U
    U0 = U
    U = np.abs(U)
    topic_U = pd.DataFrame(U[:, 0:A.shape[1]], columns=vect_sents.columns)
    topic_U = np.abs(topic_U)

    topic_mean_U = np.mean(topic_U)
    topic_std_U = np.std(topic_U)
    topic_dot_U = np.multiply(topic_mean_U, topic_std_U)

    topic_dot_U.plot()  #[['diseases','function','drugs']].plot()
    plt.title('topic_dot_U vectors of SVD')
    plt.xlabel('sentences')
    plt.show()

    #%%

    #topic_U[topic_U>0.1][sub_dict][:10].plot.bar()#[['diseases','function','drugs']].plot()
    topic_U[topic_U > 0.1][sub_dict][:-1].plot.bar(
    )  #[['diseases','function','drugs']].plot()
    plt.title('Topics using SVD')
    plt.xlabel('Sentences')
    plt.ylabel(' U with Threshold 0.1')
    plt.show()

    topic_mean_U_Th = np.mean(topic_U[topic_U > 0.1][sub_dict])

    #%% get the top low ranked sentences
    sentences_SVD_Ranking = pd.DataFrame(index=vect_sents.index)
    sentences_SVD_Ranking = pd.DataFrame(
        data={
            'Full Sentences': list_of_sentences_full,
            'Processed words': list_of_sentences
        })

    sub_dict1 = ['diseases', 'genes']
    cc = vect_sents_SVD[sub_dict1]
    sentence_sumk = cc.sum(axis=1)
    vect_sents_SVD_DiGe = pd.DataFrame(index=vect_sents.index)
    vect_sents_SVD_DiGe['Rank'] = sentence_sumk.values
    sentences_SVD_Ranking['Rank DiGe'] = sentence_sumk.values

    sub_dict2 = ['diseases', 'drugs']
    cc = vect_sents_SVD[sub_dict2]
    sentence_sumk = cc.sum(axis=1)
    vect_sents_SVD_DiDr = pd.DataFrame(index=vect_sents.index)
    vect_sents_SVD_DiDr['Rank'] = sentence_sumk.values
    sentences_SVD_Ranking['Rank DiDr'] = sentence_sumk.values

    sub_dict3 = ['drugs', 'genes']
    cc = vect_sents_SVD[sub_dict3]
    sentence_sumk = cc.sum(axis=1)
    vect_sents_SVD_DrGe = pd.DataFrame(index=vect_sents.index)
    vect_sents_SVD_DrGe['Rank'] = sentence_sumk.values
    sentences_SVD_Ranking['Rank DrGe'] = sentence_sumk.values

    #%% covariance matrix of U
    C = np.cov(U[:, 0:A.shape[1]])
    C = C - np.mean(C)
    np.mean(C)
    C[C < 0.2 * np.max(C)] = 0
    C_df = pd.DataFrame(C, )

    return sentences_SVD_Ranking
Ejemplo n.º 44
0
    def fit(self, X, y, store_covariance=False, tol=1.0e-4, **params):
        """
        Fit the LDA model according to the given training data and parameters.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
            Training vector, where n_samples in the number of samples and
            n_features is the number of features.
        y : array, shape = [n_samples]
            Target values (integers)
        store_covariance : boolean
            If True the covariance matrix (shared by all classes) is computed
            and stored in self.covariance_ attribute.
        """
        self._set_params(**params)
        X = np.asanyarray(X)
        y = np.asanyarray(y)
        if y.dtype.char.lower() not in ('b', 'h', 'i'):
            # We need integer values to be able to use
            # ndimage.measurements and np.bincount on numpy >= 2.0.
            # We currently support (u)int8, (u)int16 and (u)int32.
            # Note that versions of scipy >= 0.8 can also accept
            # (u)int64. We however don't support it for backwards
            # compatibility.
            y = y.astype(np.int32)
        if X.ndim != 2:
            raise ValueError('X must be a 2D array')
        if X.shape[0] != y.shape[0]:
            raise ValueError(
                'Incompatible shapes: X has %s samples, while y '
                'has %s' % (X.shape[0], y.shape[0]))
        n_samples = X.shape[0]
        n_features = X.shape[1]
        classes = np.unique(y)
        n_classes = classes.size
        if n_classes < 2:
            raise ValueError('y has less than 2 classes')
        classes_indices = [(y == c).ravel() for c in classes]
        if self.priors is None:
            counts = np.array(ndimage.measurements.sum(
                np.ones(n_samples, dtype=y.dtype), y, index=classes))
            self.priors_ = counts / float(n_samples)
        else:
            self.priors_ = self.priors
        
        # Group means n_classes*n_features matrix
        means = []
        Xc = []
        cov = None
        if store_covariance:
            cov = np.zeros((n_features, n_features))
        for group_indices in classes_indices:
            Xg = X[group_indices, :]
            meang = Xg.mean(0)
            means.append(meang)
            # centered group data
            Xgc = Xg - meang
            Xc.append(Xgc)
            if store_covariance:
                cov += np.dot(Xgc.T, Xgc)
        if store_covariance:
            cov /= (n_samples - n_classes)
            self.covariance_ = cov

        self.means_ = np.asarray(means)
        Xc = np.concatenate(Xc, 0)

        # ----------------------------
        # 1) within (univariate) scaling by with classes std-dev
        scaling = 1. / Xc.std(0)
        fac = float(1) / (n_samples - n_classes)
        # ----------------------------
        # 2) Within variance scaling
        X = np.sqrt(fac) * (Xc * scaling)
        # SVD of centered (within)scaled data
        U, S, V = linalg.svd(X, full_matrices=0)

        rank = np.sum(S > tol)
        if rank < n_features:
            warnings.warn("Variables are collinear")
        # Scaling of within covariance is: V' 1/S
        scaling = (scaling * V.T[:, :rank].T).T / S[:rank]
        
        ## ----------------------------
        ## 3) Between variance scaling
        # Overall mean
        xbar = np.dot(self.priors_, self.means_)
Ejemplo n.º 45
0
def pca_numpy(data):
    """Compute the principle components of a set of data points.

    Parameters
    ----------
    data : list
        A list of `m` observations, measuring `n` variables.
        For example, if the data are points in 2D space, the data parameter
        should contain `m` nested lists of `2` variables, the `x` and `y`
        coordinates.

    Returns
    -------
    tuple
        * The ``mean of the data points``.
        * The principle directions.
          The number of principle directions is equal to the dimensionality of the data.
          For example, if the data points are locations in 3D space, three principle components will be returned.
          If the data points are locations in 2D space, only two principle components will be returned.
        * The *spread* of the data along the principle directions.

    Notes
    -----
    PCA of a dataset finds the directions along which the variance of the data
    is largest, i.e. the directions along which the data is most spread out.

    Examples
    --------
    >>>

    """
    X = asarray(data)
    n, dim = X.shape

    assert n >= dim, "The number of observations (n) should be higher than the number of measured variables (dimensions)."

    # the average of the observations for each of the variables
    # for example, if the data are 2D point coordinates,
    # the average is the average of the x-coordinate across all observations
    # and the average of the y-coordinate across all observations
    mean = (X.sum(axis=0) / n).reshape((-1, dim))

    # the spread matrix
    # i.e. the variation of each variable compared to the average of the variable
    # across all observations
    Y = X - mean

    # covariance matrix of spread
    # note: there is a covariance function in NumPy...
    # the shape of the covariance matrix is dim x dim
    # for example, if the data are 2D point coordinates, the shape of C is 2 x 2
    # the diagonal of the covariance matrix contains the variance of each variable
    # the off-diagonal elements of the covariance matrix contain the covariance
    # of two independent variables
    C = Y.T.dot(Y) / (n - 1)

    assert C.shape[0] == dim, "The shape of the covariance matrix is not correct."

    # SVD of covariance matrix
    u, s, vT = svd(C, full_matrices=False)

    # eigenvectors
    # ------------
    # note: the eigenvectors are normalized
    # note: vT is exactly what it says it will be => the transposed eigenvectors
    # => take the rows of vT, or the columns of v
    # the right-singular vectors of C (the columns of V or the rows of Vt)
    # are the eigenvectors of CtC
    eigenvectors = vT

    # eigenvalues
    # -----------
    # the nonzero singular values of C are the square roots
    # of the nonzero eigenvalues of CtC and CCt
    eigenvalues = s

    # return
    return mean[0], eigenvectors, eigenvalues
Ejemplo n.º 46
0
def randomized_svd(M,
                   n_components,
                   n_oversamples=10,
                   n_iter='auto',
                   power_iteration_normalizer='auto',
                   transpose='auto',
                   flip_sign=True,
                   random_state=0):
    """Computes a truncated randomized SVD

    Parameters
    ----------
    M : ndarray or sparse matrix
        Matrix to decompose

    n_components : int
        Number of singular values and vectors to extract.

    n_oversamples : int (default is 10)
        Additional number of random vectors to sample the range of M so as
        to ensure proper conditioning. The total number of random vectors
        used to find the range of M is n_components + n_oversamples. Smaller
        number can improve speed but can negatively impact the quality of
        approximation of singular vectors and singular values.

    n_iter : int or 'auto' (default is 'auto')
        Number of power iterations. It can be used to deal with very noisy
        problems. When 'auto', it is set to 4, unless `n_components` is small
        (< .1 * min(X.shape)) `n_iter` in which case is set to 7.
        This improves precision with few components.

        .. versionchanged:: 0.18

    power_iteration_normalizer : 'auto' (default), 'QR', 'LU', 'none'
        Whether the power iterations are normalized with step-by-step
        QR factorization (the slowest but most accurate), 'none'
        (the fastest but numerically unstable when `n_iter` is large, e.g.
        typically 5 or larger), or 'LU' factorization (numerically stable
        but can lose slightly in accuracy). The 'auto' mode applies no
        normalization if `n_iter` <= 2 and switches to LU otherwise.

        .. versionadded:: 0.18

    transpose : True, False or 'auto' (default)
        Whether the algorithm should be applied to M.T instead of M. The
        result should approximately be the same. The 'auto' mode will
        trigger the transposition if M.shape[1] > M.shape[0] since this
        implementation of randomized SVD tend to be a little faster in that
        case.

        .. versionchanged:: 0.18

    flip_sign : boolean, (True by default)
        The output of a singular value decomposition is only unique up to a
        permutation of the signs of the singular vectors. If `flip_sign` is
        set to `True`, the sign ambiguity is resolved by making the largest
        loadings for each component in the left singular vectors positive.

    random_state : int, RandomState instance or None, optional (default=None)
        The seed of the pseudo random number generator to use when shuffling
        the data.  If int, random_state is the seed used by the random number
        generator; If RandomState instance, random_state is the random number
        generator; If None, the random number generator is the RandomState
        instance used by `np.random`.

    Notes
    -----
    This algorithm finds a (usually very good) approximate truncated
    singular value decomposition using randomization to speed up the
    computations. It is particularly fast on large matrices on which
    you wish to extract only a small number of components. In order to
    obtain further speed up, `n_iter` can be set <=2 (at the cost of
    loss of precision).

    References
    ----------
    * Finding structure with randomness: Stochastic algorithms for constructing
      approximate matrix decompositions
      Halko, et al., 2009 https://arxiv.org/abs/0909.4061

    * A randomized algorithm for the decomposition of matrices
      Per-Gunnar Martinsson, Vladimir Rokhlin and Mark Tygert

    * An implementation of a randomized algorithm for principal component
      analysis
      A. Szlam et al. 2014
    """
    if isinstance(M, (sparse.lil_matrix, sparse.dok_matrix)):
        warnings.warn(
            "Calculating SVD of a {} is expensive. "
            "csr_matrix is more efficient.".format(type(M).__name__),
            sparse.SparseEfficiencyWarning)

    random_state = check_random_state(random_state)
    n_random = n_components + n_oversamples
    n_samples, n_features = M.shape

    if n_iter == 'auto':
        # Checks if the number of iterations is explicitly specified
        # Adjust n_iter. 7 was found a good compromise for PCA. See #5299
        n_iter = 7 if n_components < .1 * min(M.shape) else 4

    if transpose == 'auto':
        transpose = n_samples < n_features
    if transpose:
        # this implementation is a bit faster with smaller shape[1]
        M = M.T

    Q = randomized_range_finder(M, n_random, n_iter,
                                power_iteration_normalizer, random_state)

    # project M to the (k + p) dimensional space using the basis vectors
    B = safe_sparse_dot(Q.T, M)

    # compute the SVD on the thin matrix: (k + p) wide
    Uhat, s, V = linalg.svd(B, full_matrices=False)

    del B
    U = np.dot(Q, Uhat)

    if flip_sign:
        if not transpose:
            U, V = svd_flip(U, V)
        else:
            # In case of transpose u_based_decision=false
            # to actually flip based on u and not v.
            U, V = svd_flip(U, V, u_based_decision=False)

    if transpose:
        # transpose back the results according to the input convention
        return V[:n_components, :].T, s[:n_components], U[:, :n_components].T
    else:
        return U[:, :n_components], s[:n_components], V[:n_components, :]
Ejemplo n.º 47
0
def _make_projector(projs,
                    ch_names,
                    bads=(),
                    include_active=True,
                    inplace=False):
    """Subselect projs based on ch_names and bads.

    Use inplace=True mode to modify ``projs`` inplace so that no
    warning will be raised next time projectors are constructed with
    the given inputs. If inplace=True, no meaningful data are returned.
    """
    nchan = len(ch_names)
    if nchan == 0:
        raise ValueError('No channel names specified')

    default_return = (np.eye(nchan, nchan), 0, np.empty((nchan, 0)))

    #   Check trivial cases first
    if projs is None:
        return default_return

    nvec = 0
    nproj = 0
    for p in projs:
        if not p['active'] or include_active:
            nproj += 1
            nvec += p['data']['nrow']

    if nproj == 0:
        return default_return

    #   Pick the appropriate entries
    vecs = np.zeros((nchan, nvec))
    nvec = 0
    nonzero = 0
    bads = set(bads)
    for k, p in enumerate(projs):
        if not p['active'] or include_active:
            if (len(p['data']['col_names']) != len(
                    np.unique(p['data']['col_names']))):
                raise ValueError('Channel name list in projection item %d'
                                 ' contains duplicate items' % k)

            # Get the two selection vectors to pick correct elements from
            # the projection vectors omitting bad channels
            sel = []
            vecsel = []
            p_set = set(p['data']['col_names'])  # faster membership access
            for c, name in enumerate(ch_names):
                if name not in bads and name in p_set:
                    sel.append(c)
                    vecsel.append(p['data']['col_names'].index(name))

            # If there is something to pick, pickit
            nrow = p['data']['nrow']
            this_vecs = vecs[:, nvec:nvec + nrow]
            if len(sel) > 0:
                this_vecs[sel] = p['data']['data'][:, vecsel].T

            # Rescale for better detection of small singular values
            for v in range(p['data']['nrow']):
                psize = sqrt(np.sum(this_vecs[:, v] * this_vecs[:, v]))
                if psize > 0:
                    orig_n = p['data']['data'].any(axis=0).sum()
                    # Average ref still works if channels are removed
                    if len(vecsel) < 0.9 * orig_n and not inplace and \
                            (p['kind'] != FIFF.FIFFV_PROJ_ITEM_EEG_AVREF or
                             len(vecsel) == 1):
                        warn('Projection vector "%s" has magnitude %0.2f '
                             '(should be unity), applying projector with '
                             '%s/%s of the original channels available may '
                             'be dangerous, consider recomputing and adding '
                             'projection vectors for channels that are '
                             'eventually used. If this is intentional, '
                             'consider using info.normalize_proj()' %
                             (p['desc'], psize, len(vecsel), orig_n))
                    this_vecs[:, v] /= psize
                    nonzero += 1
            # If doing "inplace" mode, "fix" the projectors to only operate
            # on this subset of channels.
            if inplace:
                p['data']['data'] = this_vecs[sel].T
                p['data']['col_names'] = [
                    p['data']['col_names'][ii] for ii in vecsel
                ]
            nvec += p['data']['nrow']

    #   Check whether all of the vectors are exactly zero
    if nonzero == 0 or inplace:
        return default_return

    # Reorthogonalize the vectors
    U, S, V = linalg.svd(vecs[:, :nvec], full_matrices=False)

    # Throw away the linearly dependent guys
    nproj = np.sum((S / S[0]) > 1e-2)
    U = U[:, :nproj]

    # Here is the celebrated result
    proj = np.eye(nchan, nchan) - np.dot(U, U.T)
    if nproj >= nchan:  # e.g., 3 channels and 3 projectors
        raise RuntimeError('Application of %d projectors for %d channels '
                           'will yield no components.' % (nproj, nchan))

    return proj, nproj, U
Ejemplo n.º 48
0
# Taking a sample data set
edata = edata.iloc[:10000, :]

evocab.index = evocab.index + 1

wc = edata.groupby('wordid')['freq'].sum()

# Applying pivot

bag_of_words = edata.pivot(index='docid', columns='wordid', values='freq')

bag_of_words = bag_of_words.fillna(0)

sparse = bag_of_words.to_sparse(fill_value=0)

U, s, V = linalg.svd(sparse, full_matrices=False)

red_U = U[:, :100]
red_V = V[:100, :]
red_s = np.diag(s[:100])
reconstructedMatrix = np.dot(np.dot(red_U, red_s), red_V)

df_trans = pd.DataFrame(reconstructedMatrix, columns=bag_of_words.columns)

LSA = df_trans.apply(sum, 0)

# from sklearn.decomposition import TruncatedSVD
#
# svd = TruncatedSVD(n_components=100,algorithm="randomized",n_iter=5,random_state=42)
#
# svd.fit(sparse)
Ejemplo n.º 49
0
#================= ADD YOUR CODE HERE ====================================
## TODO: Implement MDS
## Add your code here
# Instructions: Use MDS to reduce the dimensionality of the data
#				while preserving the distances between all pairs
#				of points. Use the steps given in the description
#				of the assignment. Initially, calculate matrix J
# 				and subsequently, matrix B. Perform SVD decomposition
#				of B. Calculate new representation. Save the new
#				representation in variable X

D = pow(D, 2)
J = identity(10) - ones((10, 10)) / 10
B = -0.5 * J.dot(D.dot(J))
try:
    U, S, V = linalg.svd(B, full_matrices=True, compute_uv=True)
except LinAlgError:
    print("SVD computation does not converge.")
except:
    print('Check linalg.svd function')

sigma = diag(S)


def disntance(k):
    s = sigma[:k, :k]
    u = U[:, :k]
    v = V[:k, :]
    return u.dot(scipy.linalg.fractional_matrix_power(s, 0.5))

Ejemplo n.º 50
0
    w2k_nn.w2knn(case)

    sortfunc = sort_octahedral_cage

    # reads case.outputnn_ and case.rotlm_
    cages = readcages2(case, 6)

    for text, cage in cages:
        #print text, 'cage=', cage

        # cage must be sorted so atoms are listed in same order
        # as vertices of polyhedron
        sortedcage = sortfunc(cage)

        nv = []
        for v in sortedcage:
            nv.append(v / sqrt(dot(v, v)))

        R0 = array([nv[0], nv[1], nv[2]])

        U, S, V = linalg.svd(R0)
        #print 'S=', S
        R = dot(U, V)
        #print
        print text
        print 'Rotation to input into case.indmfl by locrot=-1 : '
        print "%12.8f " * 3 % tuple(R[0, :])
        print "%12.8f " * 3 % tuple(R[1, :])
        print "%12.8f " * 3 % tuple(R[2, :])
        print
Ejemplo n.º 51
0
    def fit(self, X, y, store_covariance=False, tol=1.0e-4):
        """
        Fit the LDA model according to the given training data and parameters.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
            Training vector, where n_samples in the number of samples and
            n_features is the number of features.

        y : array, shape = [n_samples]
            Target values (integers)

        store_covariance : boolean
            If True the covariance matrix (shared by all classes) is computed
            and stored in `self.covariance_` attribute.
        """
        X, y = check_arrays(X, y, sparse_format='dense')
        self.classes_, y = unique(y, return_inverse=True)
        n_samples, n_features = X.shape
        n_classes = len(self.classes_)
        if n_classes < 2:
            raise ValueError('y has less than 2 classes')
        if self.priors is None:
            self.priors_ = np.bincount(y) / float(n_samples)
        else:
            self.priors_ = self.priors

        # Group means n_classes*n_features matrix
        means = []
        Xc = []
        cov = None
        if store_covariance:
            cov = np.zeros((n_features, n_features))
        for ind in xrange(n_classes):
            Xg = X[y == ind, :]
            meang = Xg.mean(0)
            means.append(meang)
            # centered group data
            Xgc = Xg - meang
            Xc.append(Xgc)
            if store_covariance:
                cov += np.dot(Xgc.T, Xgc)
        if store_covariance:
            cov /= (n_samples - n_classes)
            self.covariance_ = cov

        self.means_ = np.asarray(means)
        Xc = np.concatenate(Xc, 0)

        # ----------------------------
        # 1) within (univariate) scaling by with classes std-dev
        std = Xc.std(axis=0)
        # avoid division by zero in normalization
        std[std == 0] = 1.
        fac = float(1) / (n_samples - n_classes)
        # ----------------------------
        # 2) Within variance scaling
        X = np.sqrt(fac) * (Xc / std)
        # SVD of centered (within)scaled data
        U, S, V = linalg.svd(X, full_matrices=0)

        rank = np.sum(S > tol)
        if rank < n_features:
            warnings.warn("Variables are collinear")
        # Scaling of within covariance is: V' 1/S
        scalings = (V[:rank] / std).T / S[:rank]

        ## ----------------------------
        ## 3) Between variance scaling
        # Overall mean
        xbar = np.dot(self.priors_, self.means_)
        # Scale weighted centers
        X = np.dot(((np.sqrt(
            (n_samples * self.priors_) * fac)) * (means - xbar).T).T, scalings)
        # Centers are living in a space with n_classes-1 dim (maximum)
        # Use svd to find projection in the space spanned by the
        # (n_classes) centers
        _, S, V = linalg.svd(X, full_matrices=0)

        rank = np.sum(S > tol * S[0])
        # compose the scalings
        self.scalings_ = np.dot(scalings, V.T[:, :rank])
        self.xbar_ = xbar
        # weight vectors / centroids
        self.coef_ = np.dot(self.means_ - self.xbar_, self.scalings_)
        self.intercept_ = (-0.5 * np.sum(self.coef_**2, axis=1) +
                           np.log(self.priors_))
        return self
Ejemplo n.º 52
0
def sensitivity_map(fwd,
                    projs=None,
                    ch_type='grad',
                    mode='fixed',
                    exclude=[],
                    verbose=None):
    """Compute sensitivity map

    Such maps are used to know how much sources are visible by a type
    of sensor, and how much projections shadow some sources.

    Parameters
    ----------
    fwd : dict
        The forward operator.
    projs : list
        List of projection vectors.
    ch_type : 'grad' | 'mag' | 'eeg'
        The type of sensors to use.
    mode : str
        The type of sensitivity map computed. See manual. Should be 'free',
        'fixed', 'ratio', 'radiality', 'angle', 'remaining', or 'dampening'
        corresponding to the argument --map 1, 2, 3, 4, 5, 6 and 7 of the
        command mne_sensitivity_map.
    exclude : list of string | str
        List of channels to exclude. If empty do not exclude any (default).
        If 'bads', exclude channels in fwd['info']['bads'].
    verbose : bool, str, int, or None
        If not None, override default verbose level (see mne.verbose).

    Return
    ------
    stc : SourceEstimate
        The sensitivity map as a SourceEstimate instance for
        visualization.
    """
    # check strings
    if not ch_type in ['eeg', 'grad', 'mag']:
        raise ValueError("ch_type should be 'eeg', 'mag' or 'grad (got %s)" %
                         ch_type)
    if not mode in [
            'free', 'fixed', 'ratio', 'radiality', 'angle', 'remaining',
            'dampening'
    ]:
        raise ValueError('Unknown mode type (got %s)' % mode)

    # check forward
    if is_fixed_orient(fwd, orig=True):
        raise ValueError('fwd should must be computed with free orientation')
    fwd = convert_forward_solution(fwd,
                                   surf_ori=True,
                                   force_fixed=False,
                                   verbose=False)
    if not fwd['surf_ori'] or is_fixed_orient(fwd):
        raise RuntimeError('Error converting solution, please notify '
                           'mne-python developers')

    # limit forward
    if ch_type == 'eeg':
        fwd = pick_types_forward(fwd, meg=False, eeg=True, exclude=exclude)
    else:
        fwd = pick_types_forward(fwd, meg=ch_type, eeg=False, exclude=exclude)

    gain = fwd['sol']['data']

    # Make sure EEG has average
    if ch_type == 'eeg':
        if projs is None or not _has_eeg_average_ref_proj(projs):
            eeg_ave = [make_eeg_average_ref_proj(fwd['info'])]
        else:
            eeg_ave = []
        projs = eeg_ave if projs is None else projs + eeg_ave

    # Construct the projector
    if projs is not None:
        proj, ncomp, U = make_projector(projs,
                                        fwd['sol']['row_names'],
                                        include_active=True)
        # do projection for most types
        if mode not in ['angle', 'remaining', 'dampening']:
            gain = np.dot(proj, gain)

    # can only eggie the last couple methods if there are projectors
    elif mode in ['angle', 'remaining', 'dampening']:
        raise ValueError('No projectors used, cannot compute %s' % mode)

    n_sensors, n_dipoles = gain.shape
    n_locations = n_dipoles // 3
    sensitivity_map = np.empty(n_locations)

    for k in range(n_locations):
        gg = gain[:, 3 * k:3 * (k + 1)]
        if mode != 'fixed':
            s = linalg.svd(gg, full_matrices=False, compute_uv=False)
        if mode == 'free':
            sensitivity_map[k] = s[0]
        else:
            gz = linalg.norm(gg[:, 2])  # the normal component
            if mode == 'fixed':
                sensitivity_map[k] = gz
            elif mode == 'ratio':
                sensitivity_map[k] = gz / s[0]
            elif mode == 'radiality':
                sensitivity_map[k] = 1. - (gz / s[0])
            else:
                if mode == 'angle':
                    co = linalg.norm(np.dot(gg[:, 2], U))
                    sensitivity_map[k] = co / gz
                else:
                    p = linalg.norm(np.dot(proj, gg[:, 2]))
                    if mode == 'remaining':
                        sensitivity_map[k] = p / gz
                    elif mode == 'dampening':
                        sensitivity_map[k] = 1. - p / gz
                    else:
                        raise ValueError('Unknown mode type (got %s)' % mode)

    # only normalize fixed and free methods
    if mode in ['fixed', 'free']:
        sensitivity_map /= np.max(sensitivity_map)

    vertices = [fwd['src'][0]['vertno'], fwd['src'][1]['vertno']]
    subject = _subject_from_forward(fwd)
    stc = SourceEstimate(sensitivity_map[:, np.newaxis],
                         vertices=vertices,
                         tmin=0,
                         tstep=1,
                         subject=subject)
    return stc
Ejemplo n.º 53
0
# exercise 2.1.3
# (requires data structures from ex. 2.2.1)
from ex2_1_1 import *

import matplotlib.pyplot as plt
from scipy.linalg import svd

# Subtract mean value from data
Y = X - np.ones((N, 1)) * X.mean(axis=0)

# PCA by computing SVD of Y
U, S, V = svd(Y, full_matrices=False)

# Compute variance explained by principal components
rho = (S * S) / (S * S).sum()

threshold = 0.9

# Plot variance explained
plt.figure()
plt.plot(range(1, len(rho) + 1), rho, 'x-')
plt.plot(range(1, len(rho) + 1), np.cumsum(rho), 'o-')
plt.plot([1, len(rho)], [threshold, threshold], 'k--')
plt.title('Variance explained by principal components')
plt.xlabel('Principal component')
plt.ylabel('Variance explained')
plt.legend(['Individual', 'Cumulative', 'Threshold'])
plt.grid()
plt.show()

print('Ran Exercise 2.1.3')
Ejemplo n.º 54
0
def dict_learning(X,
                  n_components,
                  alpha,
                  max_iter=100,
                  tol=1e-8,
                  method='lars',
                  n_jobs=1,
                  dict_init=None,
                  code_init=None,
                  callback=None,
                  verbose=False,
                  random_state=None):
    """Solves a dictionary learning matrix factorization problem.

    Finds the best dictionary and the corresponding sparse code for
    approximating the data matrix X by solving::

        (U^*, V^*) = argmin 0.5 || X - U V ||_2^2 + alpha * || U ||_1
                     (U,V)
                    with || V_k ||_2 = 1 for all  0 <= k < n_components

    where V is the dictionary and U is the sparse code.

    Parameters
    ----------
    X: array of shape (n_samples, n_features)
        Data matrix.

    n_components: int,
        Number of dictionary atoms to extract.

    alpha: int,
        Sparsity controlling parameter.

    max_iter: int,
        Maximum number of iterations to perform.

    tol: float,
        Tolerance for the stopping condition.

    method: {'lars', 'cd'}
        lars: uses the least angle regression method to solve the lasso problem
        (linear_model.lars_path)
        cd: uses the coordinate descent method to compute the
        Lasso solution (linear_model.Lasso). Lars will be faster if
        the estimated components are sparse.

    n_jobs: int,
        Number of parallel jobs to run, or -1 to autodetect.

    dict_init: array of shape (n_components, n_features),
        Initial value for the dictionary for warm restart scenarios.

    code_init: array of shape (n_samples, n_components),
        Initial value for the sparse code for warm restart scenarios.

    callback:
        Callable that gets invoked every five iterations.

    verbose:
        Degree of output the procedure will print.

    random_state: int or RandomState
        Pseudo number generator state used for random sampling.

    Returns
    -------
    code: array of shape (n_samples, n_components)
        The sparse code factor in the matrix factorization.

    dictionary: array of shape (n_components, n_features),
        The dictionary factor in the matrix factorization.

    errors: array
        Vector of errors at each iteration.

    See also
    --------
    dict_learning_online
    DictionaryLearning
    MiniBatchDictionaryLearning
    SparsePCA
    MiniBatchSparsePCA
    """

    if method not in ('lars', 'cd'):
        raise ValueError('Coding method %r not supported as a fit algorithm.' %
                         method)
    method = 'lasso_' + method

    t0 = time.time()
    # Avoid integer division problems
    alpha = float(alpha)
    random_state = check_random_state(random_state)

    if n_jobs == -1:
        n_jobs = cpu_count()

    # Init the code and the dictionary with SVD of Y
    if code_init is not None and dict_init is not None:
        code = np.array(code_init, order='F')
        # Don't copy V, it will happen below
        dictionary = dict_init
    else:
        code, S, dictionary = linalg.svd(X, full_matrices=False)
        dictionary = S[:, np.newaxis] * dictionary
    r = len(dictionary)
    if n_components <= r:  # True even if n_components=None
        code = code[:, :n_components]
        dictionary = dictionary[:n_components, :]
    else:
        code = np.c_[code, np.zeros((len(code), n_components - r))]
        dictionary = np.r_[dictionary,
                           np.zeros((n_components - r, dictionary.shape[1]))]

    # Fortran-order dict, as we are going to access its row vectors
    dictionary = np.array(dictionary, order='F')

    residuals = 0

    errors = []
    current_cost = np.nan

    if verbose == 1:
        print('[dict_learning]', end=' ')

    for ii in range(max_iter):
        dt = (time.time() - t0)
        if verbose == 1:
            sys.stdout.write(".")
            sys.stdout.flush()
        elif verbose:
            print("Iteration % 3i "
                  "(elapsed time: % 3is, % 4.1fmn, current cost % 7.3f)" %
                  (ii, dt, dt / 60, current_cost))

        # Update code
        code = sparse_encode(X,
                             dictionary,
                             algorithm=method,
                             alpha=alpha,
                             init=code,
                             n_jobs=n_jobs)
        # Update dictionary
        dictionary, residuals = _update_dict(dictionary.T,
                                             X.T,
                                             code.T,
                                             verbose=verbose,
                                             return_r2=True,
                                             random_state=random_state)
        dictionary = dictionary.T

        # Cost function
        current_cost = 0.5 * residuals + alpha * np.sum(np.abs(code))
        errors.append(current_cost)

        if ii > 0:
            dE = errors[-2] - errors[-1]
            # assert(dE >= -tol * errors[-1])
            if dE < tol * errors[-1]:
                if verbose == 1:
                    # A line return
                    print("")
                elif verbose:
                    print("--- Convergence reached after %d iterations" % ii)
                break
        if ii % 5 == 0 and callback is not None:
            callback(locals())

    return code, dictionary, errors
Ejemplo n.º 55
0
    cloud = d[cloudInds]

    # Only need the coordinates of the cells
    loc = cloud[['x', 'y', 'z']]

    # Normalize the cloud locations
    locM = loc - loc.mean()

    # Compute the covariance matrix
    covar = locM.cov()

    # Compute the single value decomposition of the
    # covariance matrix.
    # s = eignenvalues
    # u = eigenvectors
    (u, s, v) = sl.svd(covar)

    f.write(
        string.format(n, s[0], u[0, 0], u[1, 0], u[2, 0], s[1], u[0, 1],
                      u[1, 1], u[2, 1], s[2], u[0, 2], u[1, 2], u[2, 2],
                      len(cloud)))

    eVal.append(s[0])
    number.append(len(cloud))
f.close()

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(ns, eVal, 'xk', label='Eigenvals')
ax2.plot(ns, number, 'ob', label='Number')
ax1.set_ylabel('Largest Eigenvalue')
Ejemplo n.º 56
0
    def _fit(self, X, compute_sources=False):
        """Fit the model

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Training data, where n_samples is the number of samples
            and n_features is the number of features.

        compute_sources : bool, default=False
            If False, sources are not computes but only the rotation matrix.
            This can save memory when working with big data. Defaults to False.

        Returns
        -------
            X_new : ndarray of shape (n_samples, n_components)
        """

        X = self._validate_data(X,
                                copy=self.whiten,
                                dtype=FLOAT_DTYPES,
                                ensure_min_samples=2).T
        fun_args = {} if self.fun_args is None else self.fun_args
        random_state = check_random_state(self.random_state)

        alpha = fun_args.get('alpha', 1.0)
        if not 1 <= alpha <= 2:
            raise ValueError('alpha must be in [1,2]')

        if self.fun == 'logcosh':
            g = _logcosh
        elif self.fun == 'exp':
            g = _exp
        elif self.fun == 'cube':
            g = _cube
        elif callable(self.fun):

            def g(x, fun_args):
                return self.fun(x, **fun_args)
        else:
            exc = ValueError if isinstance(self.fun, str) else TypeError
            raise exc(
                "Unknown function %r;"
                " should be one of 'logcosh', 'exp', 'cube' or callable" %
                self.fun)

        n_samples, n_features = X.shape

        n_components = self.n_components
        if not self.whiten and n_components is not None:
            n_components = None
            warnings.warn('Ignoring n_components with whiten=False.')

        if n_components is None:
            n_components = min(n_samples, n_features)
        if (n_components > min(n_samples, n_features)):
            n_components = min(n_samples, n_features)
            warnings.warn('n_components is too large: it will be set to %s' %
                          n_components)

        if self.whiten:
            # Centering the columns (ie the variables)
            X_mean = X.mean(axis=-1)
            X -= X_mean[:, np.newaxis]

            # Whitening and preprocessing by PCA
            u, d, _ = linalg.svd(X, full_matrices=False)

            del _
            K = (u / d).T[:n_components]  # see (6.33) p.140
            del u, d
            X1 = np.dot(K, X)
            # see (13.6) p.267 Here X1 is white and data
            # in X has been projected onto a subspace by PCA
            X1 *= np.sqrt(n_features)
        else:
            # X must be casted to floats to avoid typing issues with numpy
            # 2.0 and the line below
            X1 = as_float_array(X, copy=False)  # copy has been taken care of

        w_init = self.w_init
        if w_init is None:
            w_init = np.asarray(random_state.normal(size=(n_components,
                                                          n_components)),
                                dtype=X1.dtype)

        else:
            w_init = np.asarray(w_init)
            if w_init.shape != (n_components, n_components):
                raise ValueError(
                    'w_init has invalid shape -- should be %(shape)s' %
                    {'shape': (n_components, n_components)})

        kwargs = {
            'tol': self.tol,
            'g': g,
            'fun_args': fun_args,
            'max_iter': self.max_iter,
            'w_init': w_init
        }

        if self.algorithm == 'parallel':
            W, n_iter = _ica_par(X1, **kwargs)
        elif self.algorithm == 'deflation':
            W, n_iter = _ica_def(X1, **kwargs)
        else:
            raise ValueError('Invalid algorithm: must be either `parallel` or'
                             ' `deflation`.')
        del X1

        if compute_sources:
            if self.whiten:
                S = np.linalg.multi_dot([W, K, X]).T
            else:
                S = np.dot(W, X).T
        else:
            S = None

        self.n_iter_ = n_iter

        if self.whiten:
            self.components_ = np.dot(W, K)
            self.mean_ = X_mean
            self.whitening_ = K
        else:
            self.components_ = W

        self.mixing_ = linalg.pinv(self.components_)
        self._unmixing = W

        return S
Ejemplo n.º 57
0
# needs to be normalized. Since 'eTIV' is so much larger
# than everything else, we normalize with the standard deviation as well

# If we only normalize with mean, there is still a size difference between
# the largest and smallest absolute value of 15200.
X_norm = X - np.ones((N, 1)) * X.mean(axis=0)
print(np.abs(X_norm[0]).max() / np.abs(X_norm[0]).min())

# Reducing furthermore subtracting the standard deviation reduces
# the factor down to 3400
X_norm = (X - np.ones((N, 1)) * X.mean(axis=0)) / X.std(axis=0)
print(np.abs(X_norm[0]).max() / np.abs(X_norm[0]).min())

# %% As the data is now set, we can do a singular value decomposition (SVD)

U, S, V = svd(X_norm, full_matrices=False)

# Variance explained:
rho = (S * S) / (S * S).sum()
print(S)
print("Sigma matrix:", np.round(S, 2))

print("V:", np.round(V, 2))

print(rho)

threshold = 0.9

# Plot variance explained
plt.figure(figsize=(width, height))
plt.plot(range(1, len(rho) + 1), rho, 'x-')
Ejemplo n.º 58
0
def locally_linear_embedding(X,
                             n_neighbors,
                             n_components,
                             reg=1e-3,
                             eigen_solver='auto',
                             tol=1e-6,
                             max_iter=100,
                             method='standard',
                             hessian_tol=1E-4,
                             modified_tol=1E-12,
                             random_state=None,
                             n_jobs=None):
    """Perform a Locally Linear Embedding analysis on the data.

    Read more in the :ref:`User Guide <locally_linear_embedding>`.

    Parameters
    ----------
    X : {array-like, NearestNeighbors}
        Sample data, shape = (n_samples, n_features), in the form of a
        numpy array or a NearestNeighbors object.

    n_neighbors : integer
        number of neighbors to consider for each point.

    n_components : integer
        number of coordinates for the manifold.

    reg : float
        regularization constant, multiplies the trace of the local covariance
        matrix of the distances.

    eigen_solver : string, {'auto', 'arpack', 'dense'}
        auto : algorithm will attempt to choose the best method for input data

        arpack : use arnoldi iteration in shift-invert mode.
                    For this method, M may be a dense matrix, sparse matrix,
                    or general linear operator.
                    Warning: ARPACK can be unstable for some problems.  It is
                    best to try several random seeds in order to check results.

        dense  : use standard dense matrix operations for the eigenvalue
                    decomposition.  For this method, M must be an array
                    or matrix type.  This method should be avoided for
                    large problems.

    tol : float, optional
        Tolerance for 'arpack' method
        Not used if eigen_solver=='dense'.

    max_iter : integer
        maximum number of iterations for the arpack solver.

    method : {'standard', 'hessian', 'modified', 'ltsa'}
        standard : use the standard locally linear embedding algorithm.
                   see reference [1]_
        hessian  : use the Hessian eigenmap method.  This method requires
                   n_neighbors > n_components * (1 + (n_components + 1) / 2.
                   see reference [2]_
        modified : use the modified locally linear embedding algorithm.
                   see reference [3]_
        ltsa     : use local tangent space alignment algorithm
                   see reference [4]_

    hessian_tol : float, optional
        Tolerance for Hessian eigenmapping method.
        Only used if method == 'hessian'

    modified_tol : float, optional
        Tolerance for modified LLE method.
        Only used if method == 'modified'

    random_state : int, RandomState instance, default=None
        Determines the random number generator when ``solver`` == 'arpack'.
        Pass an int for reproducible results across multiple function calls.
        See :term: `Glossary <random_state>`.

    n_jobs : int or None, optional (default=None)
        The number of parallel jobs to run for neighbors search.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

    Returns
    -------
    Y : array-like, shape [n_samples, n_components]
        Embedding vectors.

    squared_error : float
        Reconstruction error for the embedding vectors. Equivalent to
        ``norm(Y - W Y, 'fro')**2``, where W are the reconstruction weights.

    References
    ----------

    .. [1] Roweis, S. & Saul, L. Nonlinear dimensionality reduction
        by locally linear embedding.  Science 290:2323 (2000).
    .. [2] Donoho, D. & Grimes, C. Hessian eigenmaps: Locally
        linear embedding techniques for high-dimensional data.
        Proc Natl Acad Sci U S A.  100:5591 (2003).
    .. [3] Zhang, Z. & Wang, J. MLLE: Modified Locally Linear
        Embedding Using Multiple Weights.
        http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.70.382
    .. [4] Zhang, Z. & Zha, H. Principal manifolds and nonlinear
        dimensionality reduction via tangent space alignment.
        Journal of Shanghai Univ.  8:406 (2004)
    """
    if eigen_solver not in ('auto', 'arpack', 'dense'):
        raise ValueError("unrecognized eigen_solver '%s'" % eigen_solver)

    if method not in ('standard', 'hessian', 'modified', 'ltsa'):
        raise ValueError("unrecognized method '%s'" % method)

    nbrs = NearestNeighbors(n_neighbors=n_neighbors + 1, n_jobs=n_jobs)
    nbrs.fit(X)
    X = nbrs._fit_X

    N, d_in = X.shape

    if n_components > d_in:
        raise ValueError("output dimension must be less than or equal "
                         "to input dimension")
    if n_neighbors >= N:
        raise ValueError("Expected n_neighbors <= n_samples, "
                         " but n_samples = %d, n_neighbors = %d" %
                         (N, n_neighbors))

    if n_neighbors <= 0:
        raise ValueError("n_neighbors must be positive")

    M_sparse = (eigen_solver != 'dense')

    if method == 'standard':
        W = barycenter_kneighbors_graph(nbrs,
                                        n_neighbors=n_neighbors,
                                        reg=reg,
                                        n_jobs=n_jobs)

        # we'll compute M = (I-W)'(I-W)
        # depending on the solver, we'll do this differently
        if M_sparse:
            M = eye(*W.shape, format=W.format) - W
            M = (M.T * M).tocsr()
        else:
            M = (W.T * W - W.T - W).toarray()
            M.flat[::M.shape[0] + 1] += 1  # W = W - I = W - I

    elif method == 'hessian':
        dp = n_components * (n_components + 1) // 2

        if n_neighbors <= n_components + dp:
            raise ValueError("for method='hessian', n_neighbors must be "
                             "greater than "
                             "[n_components * (n_components + 3) / 2]")

        neighbors = nbrs.kneighbors(X,
                                    n_neighbors=n_neighbors + 1,
                                    return_distance=False)
        neighbors = neighbors[:, 1:]

        Yi = np.empty((n_neighbors, 1 + n_components + dp), dtype=np.float64)
        Yi[:, 0] = 1

        M = np.zeros((N, N), dtype=np.float64)

        use_svd = (n_neighbors > d_in)

        for i in range(N):
            Gi = X[neighbors[i]]
            Gi -= Gi.mean(0)

            # build Hessian estimator
            if use_svd:
                U = svd(Gi, full_matrices=0)[0]
            else:
                Ci = np.dot(Gi, Gi.T)
                U = eigh(Ci)[1][:, ::-1]

            Yi[:, 1:1 + n_components] = U[:, :n_components]

            j = 1 + n_components
            for k in range(n_components):
                Yi[:, j:j + n_components - k] = (U[:, k:k + 1] *
                                                 U[:, k:n_components])
                j += n_components - k

            Q, R = qr(Yi)

            w = Q[:, n_components + 1:]
            S = w.sum(0)

            S[np.where(abs(S) < hessian_tol)] = 1
            w /= S

            nbrs_x, nbrs_y = np.meshgrid(neighbors[i], neighbors[i])
            M[nbrs_x, nbrs_y] += np.dot(w, w.T)

        if M_sparse:
            M = csr_matrix(M)

    elif method == 'modified':
        if n_neighbors < n_components:
            raise ValueError("modified LLE requires "
                             "n_neighbors >= n_components")

        neighbors = nbrs.kneighbors(X,
                                    n_neighbors=n_neighbors + 1,
                                    return_distance=False)
        neighbors = neighbors[:, 1:]

        # find the eigenvectors and eigenvalues of each local covariance
        # matrix. We want V[i] to be a [n_neighbors x n_neighbors] matrix,
        # where the columns are eigenvectors
        V = np.zeros((N, n_neighbors, n_neighbors))
        nev = min(d_in, n_neighbors)
        evals = np.zeros([N, nev])

        # choose the most efficient way to find the eigenvectors
        use_svd = (n_neighbors > d_in)

        if use_svd:
            for i in range(N):
                X_nbrs = X[neighbors[i]] - X[i]
                V[i], evals[i], _ = svd(X_nbrs, full_matrices=True)
            evals **= 2
        else:
            for i in range(N):
                X_nbrs = X[neighbors[i]] - X[i]
                C_nbrs = np.dot(X_nbrs, X_nbrs.T)
                evi, vi = eigh(C_nbrs)
                evals[i] = evi[::-1]
                V[i] = vi[:, ::-1]

        # find regularized weights: this is like normal LLE.
        # because we've already computed the SVD of each covariance matrix,
        # it's faster to use this rather than np.linalg.solve
        reg = 1E-3 * evals.sum(1)

        tmp = np.dot(V.transpose(0, 2, 1), np.ones(n_neighbors))
        tmp[:, :nev] /= evals + reg[:, None]
        tmp[:, nev:] /= reg[:, None]

        w_reg = np.zeros((N, n_neighbors))
        for i in range(N):
            w_reg[i] = np.dot(V[i], tmp[i])
        w_reg /= w_reg.sum(1)[:, None]

        # calculate eta: the median of the ratio of small to large eigenvalues
        # across the points.  This is used to determine s_i, below
        rho = evals[:, n_components:].sum(1) / evals[:, :n_components].sum(1)
        eta = np.median(rho)

        # find s_i, the size of the "almost null space" for each point:
        # this is the size of the largest set of eigenvalues
        # such that Sum[v; v in set]/Sum[v; v not in set] < eta
        s_range = np.zeros(N, dtype=int)
        evals_cumsum = stable_cumsum(evals, 1)
        eta_range = evals_cumsum[:, -1:] / evals_cumsum[:, :-1] - 1
        for i in range(N):
            s_range[i] = np.searchsorted(eta_range[i, ::-1], eta)
        s_range += n_neighbors - nev  # number of zero eigenvalues

        # Now calculate M.
        # This is the [N x N] matrix whose null space is the desired embedding
        M = np.zeros((N, N), dtype=np.float64)
        for i in range(N):
            s_i = s_range[i]

            # select bottom s_i eigenvectors and calculate alpha
            Vi = V[i, :, n_neighbors - s_i:]
            alpha_i = np.linalg.norm(Vi.sum(0)) / np.sqrt(s_i)

            # compute Householder matrix which satisfies
            #  Hi*Vi.T*ones(n_neighbors) = alpha_i*ones(s)
            # using prescription from paper
            h = np.full(s_i, alpha_i) - np.dot(Vi.T, np.ones(n_neighbors))

            norm_h = np.linalg.norm(h)
            if norm_h < modified_tol:
                h *= 0
            else:
                h /= norm_h

            # Householder matrix is
            #  >> Hi = np.identity(s_i) - 2*np.outer(h,h)
            # Then the weight matrix is
            #  >> Wi = np.dot(Vi,Hi) + (1-alpha_i) * w_reg[i,:,None]
            # We do this much more efficiently:
            Wi = (Vi - 2 * np.outer(np.dot(Vi, h), h) +
                  (1 - alpha_i) * w_reg[i, :, None])

            # Update M as follows:
            # >> W_hat = np.zeros( (N,s_i) )
            # >> W_hat[neighbors[i],:] = Wi
            # >> W_hat[i] -= 1
            # >> M += np.dot(W_hat,W_hat.T)
            # We can do this much more efficiently:
            nbrs_x, nbrs_y = np.meshgrid(neighbors[i], neighbors[i])
            M[nbrs_x, nbrs_y] += np.dot(Wi, Wi.T)
            Wi_sum1 = Wi.sum(1)
            M[i, neighbors[i]] -= Wi_sum1
            M[neighbors[i], i] -= Wi_sum1
            M[i, i] += s_i

        if M_sparse:
            M = csr_matrix(M)

    elif method == 'ltsa':
        neighbors = nbrs.kneighbors(X,
                                    n_neighbors=n_neighbors + 1,
                                    return_distance=False)
        neighbors = neighbors[:, 1:]

        M = np.zeros((N, N))

        use_svd = (n_neighbors > d_in)

        for i in range(N):
            Xi = X[neighbors[i]]
            Xi -= Xi.mean(0)

            # compute n_components largest eigenvalues of Xi * Xi^T
            if use_svd:
                v = svd(Xi, full_matrices=True)[0]
            else:
                Ci = np.dot(Xi, Xi.T)
                v = eigh(Ci)[1][:, ::-1]

            Gi = np.zeros((n_neighbors, n_components + 1))
            Gi[:, 1:] = v[:, :n_components]
            Gi[:, 0] = 1. / np.sqrt(n_neighbors)

            GiGiT = np.dot(Gi, Gi.T)

            nbrs_x, nbrs_y = np.meshgrid(neighbors[i], neighbors[i])
            M[nbrs_x, nbrs_y] -= GiGiT
            M[neighbors[i], neighbors[i]] += 1

    return null_space(M,
                      n_components,
                      k_skip=1,
                      eigen_solver=eigen_solver,
                      tol=tol,
                      max_iter=max_iter,
                      random_state=random_state)
Ejemplo n.º 59
0
    def fit(self, x,
            augment=False,
            rounds=1,
            seed=None):
        """Required for featurewise_center, featurewise_std_normalization
        and zca_whitening.

        # Arguments
            x: Numpy array, the data to fit on. Should have rank 4.
                In case of grayscale data,
                the channels axis should have value 1, and in case
                of RGB data, it should have value 3.
            augment: Whether to fit on randomly augmented samples
            rounds: If `augment`,
                how many augmentation passes to do over the data
            seed: random seed.

        # Raises
            ValueError: in case of invalid input `x`.
        """
        x = np.asarray(x, dtype=K.floatx())
        if x.ndim != 4:
            raise ValueError('Input to `.fit()` should have rank 4. '
                             'Got array with shape: ' + str(x.shape))
        if x.shape[self.channel_axis] not in {1, 3, 4}:
            raise ValueError(
                'Expected input to be images (as Numpy array) '
                'following the dimension ordering convention "' + self.dim_ordering + '" '
                '(channels on axis ' + str(self.channel_axis) + '), i.e. expected '
                'either 1, 3 or 4 channels on axis ' + str(self.channel_axis) + '. '
                'However, it was passed an array with shape ' + str(x.shape) +
                ' (' + str(x.shape[self.channel_axis]) + ' channels).')

        if seed is not None:
            np.random.seed(seed)

        x = np.copy(x)
        if augment:
            ax = np.zeros(tuple([rounds * x.shape[0]] + list(x.shape)[1:]), dtype=K.floatx())
            for r in range(rounds):
                for i in range(x.shape[0]):
                    ax[i + r * x.shape[0]] = self.random_transform(x[i])
            x = ax

        if self.featurewise_center:
            self.mean = np.mean(x, axis=(0, self.row_axis, self.col_axis))
            broadcast_shape = [1, 1, 1]
            broadcast_shape[self.channel_axis - 1] = x.shape[self.channel_axis]
            self.mean = np.reshape(self.mean, broadcast_shape)
            x -= self.mean

        if self.featurewise_std_normalization:
            self.std = np.std(x, axis=(0, self.row_axis, self.col_axis))
            broadcast_shape = [1, 1, 1]
            broadcast_shape[self.channel_axis - 1] = x.shape[self.channel_axis]
            self.std = np.reshape(self.std, broadcast_shape)
            x /= (self.std + K.epsilon())

        if self.zca_whitening:
            flat_x = np.reshape(x, (x.shape[0], x.shape[1] * x.shape[2] * x.shape[3]))
            sigma = np.dot(flat_x.T, flat_x) / flat_x.shape[0]
            u, s, _ = linalg.svd(sigma)
            self.principal_components = np.dot(np.dot(u, np.diag(1. / np.sqrt(s + 10e-7))), u.T)
Ejemplo n.º 60
0
    def update_transformation_matrix(self):
        """
            Smoothing Skining Decomposition with Rigid Bones
            Section 3.3 Update Bone Transformations
        """


        p = self.V[ 0 ] #rest pose            
        R = np.zeros( (self.F_,self.B_,3,3) )
        T = np.zeros( (self.F_,self.B_,3) )

        insignificant_first_encounter = True #initialize
        cur_max_err = 0

        for j in range(self.B_):

            wj = self.W[:,j]
            sigma_wwj = np.sum( wj**2, axis=0 )
            

            if sigma_wwj > self.theta:
                wwj = wj**2
                p_star = wwj.dot(p) / sigma_wwj
                _p = p - p_star

                P = np.multiply(  wj, _p.T )

                for t in range( 1, self.F_ ):
                    Rt = self.R[ t ]
                    Tt = self.T[ t ]
                    qt = self.V[ t ].copy()
                    for i in range(self.B_):
                        if i == j:continue
                        pt =  p.dot( Rt[ i ].T ) + Tt[ i ]
                        qt -= np.multiply( self.W[:,i] , pt.T ).T
                    qt_star =wj.dot(qt) / sigma_wwj
                    _qt = qt - np.multiply( wj, np.repeat( qt_star[ np.newaxis, : ], self.N_, axis=0 ).T ).T

                    PQT = P.dot(_qt)
                    U, _, Vt = svd(PQT)

                    R[ t, j ] = Vt.T.dot(U.T)
                    T[ t, j ] = qt_star - R[ t, j ].dot( p_star )

            else:
                """
                    insignificant bone, re-initialization, use Kabsch algorithm
                """

                print "insignificant bone %d, using kabsch algorithm"%j

                if insignificant_first_encounter==True: #compute vertex idx with largest reconstruction error
                    err = []
                    for v in range(self.N_):
                        e = 0
                        for t in range(1,self.F_):
                            qt = self.V[t,v].copy()
                            for b in range(self.B_):
                                qt -= self.W[v, b]*( self.R[t,b].dot( self.V[0,v] ) + self.T[t,b] )
                            e += qt.dot(qt)
                        err.append( e )
                    err_sort_list = np.argsort( err )[::-1]
                    insignificant_first_encounter = False



                #search with one bone
                #for t in range( 1,self.F_):
                #    qt = self.V[t] - np.multiply( self.W[:,j], ( p.dot( self.R[ t, j ].T ) + np.repeat( self.T[ t, j ][np.newaxis, :], self.N_, axis= 0  ) ).T ).T
                #    max_err_vtx_idx = np.argmax( np.sum( qt**2, axis=-1 ), axis=0 ) # find the vertex with largest reconstruction error
                max_err_vtx_idx = err_sort_list[cur_max_err]
                cur_max_err += 1
                neighbours = np.argsort( self.compute_distance( max_err_vtx_idx ) )[ 1:21 ]  #find the 20 nearest neighbourhood
                for t in range( 1,self.F_):
                    ka_algo = KabschAlgorithm(p, self.V[t])
                    R[ t, j ], T[t, j] = ka_algo(neighbours)


        self.R = R.copy()
        self.T = T.copy()