Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

python - 在python中使用cvxpy包构造融合的套索惩罚(Constructing fused lasso penalty with cvxpy package in python)

Introduced by Tibshirani, 2005, encourages sparsity of the coefficients and also sparsity of their differences.

(Tibshirani于2005年提出,鼓励系数的稀疏性以及它们之间差异的稀疏性。)

(article)

((文章))

Screenshot of the formula

(公式的屏幕截图)

How can I imitate this with cvxpy package?

(我该如何使用cvxpy软件包来模仿它?)

There are example codes for Lasso and Ridge Penalties seperately.

(分别有套索罚的示例代码。)

I get a general idea how the codes work but there are some fuctions I do not get how I'm supposed to decide which one to use.For example let's compare Lasso and Ridge penalty codes.

(我大致了解了代码的工作原理,但有一些功能我不知道应该如何决定使用哪一个代码,例如让我们比较Lasso和Ridge罚码。)

# Lasso

import cvxpy as cp
import numpy as np
import matplotlib.pyplot as plt

def loss_fn(X, Y, beta):
    return cp.norm2(cp.matmul(X, beta) - Y)**2

def regularizer(beta):
    return cp.norm1(beta)

def objective_fn(X, Y, beta, lambd):
    return loss_fn(X, Y, beta) + lambd * regularizer(beta)

def mse(X, Y, beta):
    return (1.0 / X.shape[0]) * loss_fn(X, Y, beta).value

# Ridge

import cvxpy as cp
import numpy as np
import matplotlib.pyplot as plt

def loss_fn(X, Y, beta):
    return cp.pnorm(cp.matmul(X, beta) - Y, p=2)**2

def regularizer(beta):
    return cp.pnorm(beta, p=2)**2

def objective_fn(X, Y, beta, lambd):
    return loss_fn(X, Y, beta) + lambd * regularizer(beta)

def mse(X, Y, beta):
    return (1.0 / X.shape[0]) * loss_fn(X, Y, beta).value

For both of them we create a loss function but they are created with different commands?

(对于它们两者,我们都创建了损失函数,但是它们是用不同的命令创建的?)

  ask by moli translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From what I understand, you're having trouble expressing the penalty on the differences of coefficients $\sum_k|b_k - b_{k-1}|$.

(据我了解,您很难表达对系数$ \ sum_k | b_k-b_ {k-1} | $的影响。)

To do this, you just need to define the vector difference matrix as follows:

(为此,您只需要定义矢量差矩阵,如下所示:)

def diff_op(shape):
    mat = np.zeros(shape)
    mat[range(2, shape[0]), range(1, shape[1])] = 1
    mat -= np.eye(shape)
    return mat

def difference_pen(beta, epsilon):
    return cvx.sum(cvx.abs((epsilon, diff_op(beta.shape) @ beta))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...