def _compute_codes(X, D, sc_mode, W, lambda1, lambda2, n_jobs, pos_coef, **args): """ Deprecated for very-large datasets! Use sparse_decode instead. """ X = np.asfortranarray(X) D = np.asfortranarray(D) gram = None cov = None if W is None: gram = np.dot(D.T, D) gram = np.asfortranarray(gram) cov = np.dot(D.T, X) cov = np.asfortranarray(cov) if sc_mode in [0, 1, 2]: if W is None: A = spams.lasso(X , D, gram, cov, lambda1=lambda1, lambda2=lambda2, numThreads=n_jobs, mode=sc_mode, pos=pos_coef) else: A = spams.lassoWeighted(X , D, W, lambda1=lambda1,mode=sc_mode, pos=pos_coef, numThreads=n_jobs) else: L = lambda1 if sc_mode == 3 else None eps = lambda1 if sc_mode == 4 else None lambda_1 = lambda1 if sc_mode == 5 else None A = spams.omp(X, D, L, eps, lambda_1, numThreads=n_jobs) return A.toarray()
def learn_sparse_coeffs(self, matrix, D, params, weighting=None): if weighting is not None: alphas = spams.lassoWeighted(matrix, D=D, W=weighting, **params) else: alphas = spams.lasso(matrix, D=D, **params) #logging.info('Alphas shape and sparsity:\t{}\t{:.4f}'.format(alphas.shape, 100*alphas.nnz/np.prod(alphas.shape))) return alphas
def solve(self, A, y, x0, as_signs): #print 'dense_solver: y.shape=',y.shape #print 'dense_solver: A.shape=',A.shape fy = np.asfortranarray(y.reshape(len(y), 1)) fA = np.asfortranarray(A) #print 'fy.shape=',fy.shape #print 'fA.shape=',fA.shape if not self.positive: xnew = spams.lasso(fy, fA, mode=2, lambda1=self.lambda1, lambda2=self.lambda2) else: W = np.ones(len(x0)) params = {'lambda1':self.lambda1, 'pos':True} xnew = spams.lassoWeighted(fy, fA, W, **params) xnew = np.array(xnew.todense()).reshape(x0.shape) #print 'dense_solver: xnew.shape=',xnew.shape return xnew
def test_lassoWeighted(): np.random.seed(0) print("test lasso weighted") ############################################## # Decomposition of a large number of signals ############################################## # data generation X = np.asfortranarray(np.random.normal(size=(64,10000))) X = np.asfortranarray(X / np.tile(np.sqrt((X*X).sum(axis=0)),(X.shape[0],1)),dtype= myfloat) D = np.asfortranarray(np.random.normal(size=(64,256))) D = np.asfortranarray(D / np.tile(np.sqrt((D*D).sum(axis=0)),(D.shape[0],1)),dtype= myfloat) param = { 'L' : 20, 'lambda1' : 0.15, 'numThreads' : 8, 'mode' : spams.PENALTY} W = np.asfortranarray(np.random.random(size = (D.shape[1],X.shape[1])),dtype= myfloat) tic = time.time() alpha = spams.lassoWeighted(X,D,W,**param) tac = time.time() t = tac - tic print("%f signals processed per second\n" %(float(X.shape[1]) / t)) return None