示例#1
0
def fused_lasso(n=100, l1=2., **control):

    D = (np.identity(n) - np.diag(np.ones(n - 1), -1))[1:]
    Dsp = scipy.sparse.lil_matrix(D)
    M = np.linalg.eigvalsh(np.dot(D.T, D)).max()

    Y = np.random.standard_normal(n)
    Y[int(0.1 * n):int(0.3 * n)] += 6.

    p1 = sapprox.gengrad_sparse((Dsp, Y), L=M)
    p1.assign_penalty(l1=l1)

    p2 = sapprox.gengrad((D, Y), L=M)
    p2.assign_penalty(l1=l1)

    t1 = time.time()
    opt1 = regreg.FISTA(p1)
    opt1.debug = True
    opt1.fit(tol=control['tol'], max_its=control['max_its'])
    beta1 = opt1.problem.coefs
    t2 = time.time()
    ts1 = t2 - t1

    t1 = time.time()
    opt2 = regreg.FISTA(p2)
    opt2.fit(tol=control['tol'], max_its=control['max_its'])
    beta2 = opt2.problem.coefs
    t2 = time.time()
    ts2 = t2 - t1

    beta1, _ = opt1.output
    beta2, _ = opt2.output
    np.testing.assert_almost_equal(beta1, beta2)

    X = np.arange(n)
    if control['plot']:
        pylab.clf()
        pylab.step(X, beta1, linewidth=3, c='red')
        pylab.step(X, beta2, linewidth=3, c='green')
        pylab.scatter(X, Y)
        pylab.show()
示例#2
0
def fused_lasso(n=100,l1=2.,**control):

    D = (np.identity(n) - np.diag(np.ones(n-1),-1))[1:]
    Dsp = scipy.sparse.lil_matrix(D)
    M = np.linalg.eigvalsh(np.dot(D.T, D)).max() 

    Y = np.random.standard_normal(n)
    Y[int(0.1*n):int(0.3*n)] += 6.

    p1 = sapprox.gengrad_sparse((Dsp, Y),L=M)
    p1.assign_penalty(l1=l1)

    p2 = sapprox.gengrad((D, Y),L=M)
    p2.assign_penalty(l1=l1)
    
    t1 = time.time()
    opt1 = regreg.FISTA(p1)
    opt1.debug=True
    opt1.fit(tol=control['tol'], max_its=control['max_its'])
    beta1 = opt1.problem.coefs
    t2 = time.time()
    ts1 = t2-t1

    t1 = time.time()
    opt2 = regreg.FISTA(p2)
    opt2.fit(tol=control['tol'], max_its=control['max_its'])
    beta2 = opt2.problem.coefs
    t2 = time.time()
    ts2 = t2-t1

    beta1, _ = opt1.output
    beta2, _ = opt2.output
    np.testing.assert_almost_equal(beta1, beta2)

    X = np.arange(n)
    if control['plot']:
        pylab.clf()
        pylab.step(X, beta1, linewidth=3, c='red')
        pylab.step(X, beta2, linewidth=3, c='green')
        pylab.scatter(X, Y)
        pylab.show()
示例#3
0
def test_fused_lasso(n=100, l1=15., ratio=0.1):


    Y = np.random.standard_normal(n)
    Y[int(0.1*n):int(0.3*n)] += 6.
    D1 = (np.identity(n) - np.diag(np.ones(n-1),-1))[1:]
    D2 = np.dot(D1[1:,1:], D1)
    D2 = np.vstack([D1, ratio*np.identity(n)])

    M = np.linalg.eigvalsh(np.dot(D2.T, D2)).max() 
    D1sp = scipy.sparse.csr_matrix(D1)
    Dsp = scipy.sparse.csr_matrix(D2)

    D3 = np.identity(n)
    D3sp =  scipy.sparse.csr_matrix(D3)



    semi = seminorm(l1norm(l1)) #+ seminorm(genl1norm(D1sp,l1))
    p2 = squaredloss((np.eye(n),Y),semi)
    opt2 = regreg.FISTA(p2)
    opt2.debug = True
    obj2 = opt2.fit(tol=1.0e-10, max_its=5000)
    beta2, _ = opt2.output
    print beta2


    p1 = sapprox.gengrad_sparse((D3sp, Y),L=M)
    p1.assign_penalty(l1=l1)


    opt1 = regreg.FISTA(p1)
    #opt1.debug = True
    obj1 = opt1.fit(tol=1.0e-10, max_its=5000, alpha=1.05)
    beta1, _ = opt1.output
    print beta1
    
    print np.allclose(beta1,beta2,rtol=1e-4)

    """