from cvxpy import *import numpy as npimport pandas as pdimport cvxoptfrom multiprocessing import Poolfrom scipy import sparseimport picklefrom sklearn.cross_validation import train_test_splitimport matplotlib.pyplot as pltfrom sklearn import linear_model, datasetsimport matplotlib.pyplot as pltiris = datasets.load_iris()X = iris.data[:, :5]y = iris.targety[y>0]=1y[y==0]=-1clf = linear_model.LogisticRegression(C=1.0,penalty='l1')m=clf.fit(X,np.ravel(y))acc = m.score(X,np.ravel(y))coefs = m.coef_intercept = m.intercept_coefs = np.hstack((intercept.reshape(1,1),coefs))#### now compare coefs with CVXPYgamma=Parameter(sign='positive')gamma.value=1beta=Variable(X.shape[1]+1) ## accomodate for interceptloss = sum(logistic(vstack(1, X[i,:]).T*beta*-y[i]) for i in range(X.shape[0]))/X.shape[0] # multiply each row of X with the variable vector beta and take the average logistic losspenalty = gamma*norm(beta,1)objective = Minimize(loss + penalty)p = Problem(objective)res = p.solve(verbose=True, max_iters=10000)res2=np.column_stack((np.ones(X.shape[0]),X)).dot(coefs.T) #res1=np.column_stack((np.ones(X.shape[0]),X)).dot(beta.value)
from cvxpy import *
import numpy as np
from sklearn import linear_model, datasets
import matplotlib.pyplot as plt
iris = datasets.load_iris()
X = iris.data[:, :5]
y = iris.target
y[y>0]=1
y[y==0]=-1
reg_params = [1.0,0.1,10,0.5,2,0.25,4]
coefs1=[]
for c in [1.0,0.1,10,0.5,2,0.25,4]:
clf = linear_model.LogisticRegression (C=1.0,penalty='l1')
m=clf.fit(X,np.ravel(y))
#acc = m.score(X,np.ravel(y))
coefs = m.coef_
intercept = m.intercept_
coefs = np.hstack((intercept.reshape(1,1),coefs))
coefs1.append(coefs)
#### now compare coefs with CVXPY
gamma=Parameter(sign='positive')
beta=Variable(X.shape[1]+1) ## accomodate for intercept
loss = sum(logistic(vstack(1, X[i,:]).T*beta*-y[i]) for i in range(X.shape[0]))/X.shape[0] # multiply each row of X with the variable vector beta and take the average logistic loss
penalty = gamma*norm(beta,1)
objective = Minimize(loss + penalty)
p = Problem(objective)
coefs2=[]
for c in [1.0,0.1,10,0.5,2,0.25,4]:
gamma.value=c
res = p.solve(verbose=False, max_iters=10000)
coefs2.append(beta.value)
# res2=np.column_stack((np.ones(X.shape[0]),X)).dot(coefs.T) #
# res1=np.column_stack((np.ones(X.shape[0]),X)).dot(beta.value)
for coef in zip([1.0,0.1,10,0.5,2,0.25,4],coefs1,coefs2):
print "regularization param="+str(coef[0])
print "scikit-learn LoigsticRegression - L1-norm of solution :"+str(np.linalg.norm(coef[1]))
print "cvxpy - L1-norm of solution:"+str(np.linalg.norm(coef[2]))
print "============================="
Should be
clf = linear_model.LogisticRegression
(C=c,penalty='l1')