[英]Sklearn SelectFromModel with L1 regularized Logistic Regression
[英]solve L2 regularized logistic regression using CVX/CVXPY
我已經嘗試了2-3天,以使L2正則化邏輯回歸能夠在Matlab(CVX)和Python(CVXPY)中工作,但沒有成功。 我對凸優化還很陌生,所以很沮喪。 以下是我嘗試使用CVX / CVXPY解決的方程式。 我從論文https://intentmedia.github.io/assets/2013-10-09-presenting-at-ieee-big-data/pld_js_ieee_bigdata_2013_admm.pdf中得出了這個等式
我的Matlab(CVX)代碼是
function L2
m = 800; N = 5;
lambda =0.000001;
A = load('/path/to/training/file');
b= A(:,6); //Label Matrix (800x1)
A = A(:,1:5); //Feature matrix (800x5)
cvx_begin
variable x(N)
minimize( (1/m * sum( log(1+ exp(-1* A' * (b * x')) ) ) ) + lambda*(norm(x,2)))
cvx_end
CVX返回一個錯誤的說法,這很有意義,但是本文提到了上面的公式。 我該如何解決?
您的目標函數不是標量。
在嘗試使用Matlab之后,我嘗試了CVXPY。 這是python代碼
from cvxopt import solvers, matrix,log, exp,mul
from cvxopt.modeling import op,variable
import numpy as np
n = 5
m=800
data = np.ndarray(shape=(m,n), dtype=float,)
bArray = []
file = open('/path/to/training/file')
i = 0;
j=0;
for line in file:
for num in line.split():
if(j==5):
bArray.append(float(num))
else:
data[i][j] = num
j = j + 1
j=0
i = i + 1
A = matrix(data)
b_mat= matrix(bArray)
m, n = A.size
lamb_default = 0.000001
x=variable(n)
b = -1*b_mat
w = exp(A.T*b*x)
f = (1/m) + sum(log(1+w)) + lamb_default*mul(x,x)
lp1 = op(f)
lp1.solve()
lp1.status
print(lp1.objective.value())
我得到錯誤
TypeError:尺寸不兼容
所以,我的問題是:在CVX / CVXPY中計算L2問題的代碼中我做錯了什么?
MATLAB代碼中的目標是輸出向量,而不是數字(標量)。 更改為:
(1/m * sum( log(1+ exp(-b.* (A * x)) ) ) )
它將返回一個數字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.