def sum_flat(a): """Return the sum of all the elements of a, flattened out. It uses a.flat, and if a is not contiguous, a call to ravel(a) is made.""" if iscontiguous(a): return asum(a.flat) else: return asum(ravel(a))
def sum_flat(a): """Return the sum of all the elements of a, flattened out. It uses a.flat, and if a is not contiguous, a call to ravel(a) is made.""" if a.iscontiguous(): return asum(a.flat) else: return asum(ravel(a))
def orth(A): """ Orthogonalization procedure by Matlab. The description is taken from its help: Q = ORTH(A) is an orthonormal basis for the range of A. That is, Q'*Q = I, the columns of Q span the same space as the columns of A, and the number of columns of Q is the rank of A. """ A = array(A) U,S,V = numerix.mlab.svd(A) m,n = numerix.shape(A) if m > 1: s = S elif m == 1: s = S[0] else: s = 0 tol = numerix.mlab.max((m,n)) * numerix.mlab.max(s) * _eps_approx r = asum(s > tol) Q = take(U,range(r),1) return Q
def entropy(y, bins): """ Return the entropy of the data in y \sum p_i log2(p_i) where p_i is the probability of observing y in the ith bin of bins. bins can be a number of bins or a range of bins; see hist Compare S with analytic calculation for a Gaussian x = mu + sigma*randn(200000) Sanalytic = 0.5 * ( 1.0 + log(2*pi*sigma**2.0) ) """ n,bins = hist(y, bins) n = n.astype(Float) n = take(n, nonzero(n)) # get the positive p = divide(n, len(y)) delta = bins[1]-bins[0] S = -1.0*asum(p*log(p)) + log(delta) #S = -1.0*asum(p*log(p)) return S
def orth(A): """ Orthogonalization procedure by Matlab. The description is taken from its help: Q = ORTH(A) is an orthonormal basis for the range of A. That is, Q'*Q = I, the columns of Q span the same space as the columns of A, and the number of columns of Q is the rank of A. """ A = array(A) U, S, V = numerix.mlab.svd(A) m, n = numerix.shape(A) if m > 1: s = S elif m == 1: s = S[0] else: s = 0 tol = numerix.mlab.max((m, n)) * numerix.mlab.max(s) * _eps_approx r = asum(s > tol) Q = take(U, range(r), 1) return Q
def entropy(y, bins): """ Return the entropy of the data in y \sum p_i log2(p_i) where p_i is the probability of observing y in the ith bin of bins. bins can be a number of bins or a range of bins; see hist Compare S with analytic calculation for a Gaussian x = mu + sigma*randn(200000) Sanalytic = 0.5 * ( 1.0 + log(2*pi*sigma**2.0) ) """ n, bins = hist(y, bins) n = n.astype(Float) n = take(n, nonzero(n)) # get the positive p = divide(n, len(y)) delta = bins[1] - bins[0] S = -1.0 * asum(p * log(p)) + log(delta) #S = -1.0*asum(p*log(p)) return S
def norm(x, y=2): """ Norm of a matrix or a vector according to Matlab. The description is taken from Matlab: For matrices... NORM(X) is the largest singular value of X, max(svd(X)). NORM(X,2) is the same as NORM(X). NORM(X,1) is the 1-norm of X, the largest column sum, = max(sum(abs((X)))). NORM(X,inf) is the infinity norm of X, the largest row sum, = max(sum(abs((X')))). NORM(X,'fro') is the Frobenius norm, sqrt(sum(diag(X'*X))). NORM(X,P) is available for matrix X only if P is 1, 2, inf or 'fro'. For vectors... NORM(V,P) = sum(abs(V).^P)^(1/P). NORM(V) = norm(V,2). NORM(V,inf) = max(abs(V)). NORM(V,-inf) = min(abs(V)). """ x = asarray(x) if numerix.mlab.rank(x) == 2: if y == 2: return numerix.mlab.max(numerix.mlab.svd(x)[1]) elif y == 1: return numerix.mlab.max(asum(absolute((x)))) elif y == 'inf': return numerix.mlab.max(asum(absolute((transpose(x))))) elif y == 'fro': return numerix.mlab.sqrt( asum(numerix.mlab.diag(matrixmultiply(transpose(x), x)))) else: verbose.report_error('Second argument not permitted for matrices') return None else: if y == 'inf': return numerix.mlab.max(absolute(x)) elif y == '-inf': return numerix.mlab.min(absolute(x)) else: return power(asum(power(absolute(x), y)), 1 / float(y))
def norm(x,y=2): """ Norm of a matrix or a vector according to Matlab. The description is taken from Matlab: For matrices... NORM(X) is the largest singular value of X, max(svd(X)). NORM(X,2) is the same as NORM(X). NORM(X,1) is the 1-norm of X, the largest column sum, = max(sum(abs((X)))). NORM(X,inf) is the infinity norm of X, the largest row sum, = max(sum(abs((X')))). NORM(X,'fro') is the Frobenius norm, sqrt(sum(diag(X'*X))). NORM(X,P) is available for matrix X only if P is 1, 2, inf or 'fro'. For vectors... NORM(V,P) = sum(abs(V).^P)^(1/P). NORM(V) = norm(V,2). NORM(V,inf) = max(abs(V)). NORM(V,-inf) = min(abs(V)). """ x = asarray(x) if numerix.mlab.rank(x)==2: if y==2: return numerix.mlab.max(numerix.mlab.svd(x)[1]) elif y==1: return numerix.mlab.max(asum(absolute((x)))) elif y=='inf': return numerix.mlab.max(asum(absolute((transpose(x))))) elif y=='fro': return numerix.mlab.sqrt(asum(numerix.mlab.diag(matrixmultiply(transpose(x),x)))) else: verbose.report_error('Second argument not permitted for matrices') return None else: if y == 'inf': return numerix.mlab.max(absolute(x)) elif y == '-inf': return numerix.mlab.min(absolute(x)) else: return power(asum(power(absolute(x),y)),1/float(y))
def prepca(P, frac=0): """ Compute the principal components of P. P is a numVars x numObservations numeric array. frac is the minimum fraction of variance that a component must contain to be included Return value are Pcomponents : a num components x num observations numeric array Trans : the weights matrix, ie, Pcomponents = Trans*P fracVar : the fraction of the variance accounted for by each component returned """ U,s,v = svd(P) varEach = s**2/P.shape[1] totVar = asum(varEach) fracVar = divide(varEach,totVar) ind = int(asum(fracVar>=frac)) # select the components that are greater Trans = transpose(U[:,:ind]) # The transformed data Pcomponents = matrixmultiply(Trans,P) return Pcomponents, Trans, fracVar[:ind]
def prepca(P, frac=0): """ Compute the principal components of P. P is a numVars x numObservations numeric array. frac is the minimum fraction of variance that a component must contain to be included Return value are Pcomponents : a num components x num observations numeric array Trans : the weights matrix, ie, Pcomponents = Trans*P fracVar : the fraction of the variance accounted for by each component returned """ U, s, v = svd(P) varEach = s**2 / P.shape[1] totVar = asum(varEach) fracVar = divide(varEach, totVar) ind = int(asum(fracVar >= frac)) # select the components that are greater Trans = transpose(U[:, :ind]) # The transformed data Pcomponents = matrixmultiply(Trans, P) return Pcomponents, Trans, fracVar[:ind]
def rank(x): """ Returns the rank of a matrix. The rank is understood here as the an estimation of the number of linearly independent rows or columns (depending on the size of the matrix). Note that numerix.mlab.rank() is not equivalent to Matlab's rank. This function is! """ x = asarray(x) u,s,v = numerix.mlab.svd(x) # maxabs = numerix.mlab.max(numerix.absolute(s)) is also possible. maxabs = norm(x) maxdim = numerix.mlab.max(numerix.shape(x)) tol = maxabs*maxdim*_eps_approx r = s>tol return asum(r)
def rank(x): """ Returns the rank of a matrix. The rank is understood here as the an estimation of the number of linearly independent rows or columns (depending on the size of the matrix). Note that numerix.mlab.rank() is not equivalent to Matlab's rank. This function is! """ x = asarray(x) u, s, v = numerix.mlab.svd(x) # maxabs = numerix.mlab.max(numerix.absolute(s)) is also possible. maxabs = norm(x) maxdim = numerix.mlab.max(numerix.shape(x)) tol = maxabs * maxdim * _eps_approx r = s > tol return asum(r)
def trapz(x, y): if len(x)!=len(y): raise ValueError, 'x and y must have the same length' if len(x)<2: raise ValueError, 'x and y must have > 1 element' return asum(0.5*diff(x)*(y[1:]+y[:-1]))
def trapz(x, y): if len(x) != len(y): raise ValueError, 'x and y must have the same length' if len(x) < 2: raise ValueError, 'x and y must have > 1 element' return asum(0.5 * diff(x) * (y[1:] + y[:-1]))