SVM classification EXAMPLE cvxpy¶

import numpy as np
import cvxpy as cp
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-961a1519bf5d> in <module>
      1 import numpy as np
----> 2 import cvxpy as cp
      3 import matplotlib.pyplot as plt
      4 from sklearn import datasets
      5 from sklearn.model_selection import train_test_split

/opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages/cvxpy/__init__.py in <module>
     16 
     17 __version__ = "1.1.7"
---> 18 from cvxpy.atoms import *
     19 from cvxpy.constraints import NonPos, Zero, SOC, PSD
     20 from cvxpy.expressions.expression import Expression

/opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages/cvxpy/atoms/__init__.py in <module>
     18 from cvxpy.atoms.dist_ratio import dist_ratio
     19 from cvxpy.atoms.eye_minus_inv import eye_minus_inv, resolvent
---> 20 from cvxpy.atoms.geo_mean import geo_mean
     21 from cvxpy.atoms.gen_lambda_max import gen_lambda_max
     22 from cvxpy.atoms.gmatmul import gmatmul

/opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages/cvxpy/atoms/geo_mean.py in <module>
     18 import numpy as np
     19 import scipy.sparse as sp
---> 20 from cvxpy.utilities.power_tools import (fracify, decompose, approx_error, lower_bound,
     21                                          over_bound, prettydict)
     22 

/opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages/cvxpy/utilities/power_tools.py in <module>
     16 
     17 from fractions import Fraction
---> 18 from cvxpy.atoms.affine.reshape import reshape
     19 from cvxpy.atoms.affine.vstack import vstack
     20 from cvxpy.constraints.second_order import SOC

/opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages/cvxpy/atoms/affine/reshape.py in <module>
     16 
     17 from cvxpy.expressions.expression import Expression
---> 18 from cvxpy.atoms.affine.hstack import hstack
     19 from cvxpy.atoms.affine.affine_atom import AffAtom
     20 import cvxpy.lin_ops.lin_utils as lu

/opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages/cvxpy/atoms/affine/hstack.py in <module>
     16 
     17 import cvxpy.lin_ops.lin_utils as lu
---> 18 from cvxpy.atoms.affine.affine_atom import AffAtom
     19 import numpy as np
     20 

/opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages/cvxpy/atoms/affine/affine_atom.py in <module>
     20 import cvxpy.lin_ops.lin_utils as lu
     21 from cvxpy.atoms.atom import Atom
---> 22 from cvxpy.cvxcore.python import canonInterface
     23 from cvxpy.expressions.constants import Constant
     24 from cvxpy.utilities import performance_utils as perf

/opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages/cvxpy/cvxcore/python/__init__.py in <module>
      1 # TODO(akshayka): This is a hack; the swig-auto-generated cvxcore.py
      2 # tries to import cvxcore as `from . import _cvxcore`
----> 3 import _cvxcore

ImportError: numpy.core.multiarray failed to import
iris = datasets.load_iris()
y = np.where(iris.target[iris.target < 2] == 1, 1, -1).reshape((100,1))
X = iris.data[:y.shape[0], :]

X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=1)

\(f(\beta, v) = (1/m) \sum_i (1 - y_i(\beta^Tx_i - v))_+ + \lambda ||\beta||_1\)

beta = cp.Variable((X_train.shape[1],1))
v = cp.Variable()
loss = cp.sum(cp.pos(1 - cp.multiply(y_train, X_train @ beta - v)))
reg = cp.norm(beta, 1)
lambd = cp.Parameter(nonneg=True)
prob = cp.Problem(cp.Minimize(loss/m + lambd*reg))
TRIALS = 100
train_error = np.zeros(TRIALS)
test_error = np.zeros(TRIALS)
lambda_vals = np.logspace(-2, 0, TRIALS)
beta_vals = []
for i in range(TRIALS):
    lambd.value = lambda_vals[i]
    prob.solve()
    train_error[i] = (y_train != np.sign(X_train.dot(beta.value) - v.value)).sum()/m
    test_error[i] = (y_test != np.sign(X_test.dot(beta.value) - v.value)).sum()/TEST
    beta_vals.append(beta.value)
plt.plot(lambda_vals, train_error, label="Train error")
plt.plot(lambda_vals, test_error, label="Test error")
plt.xscale('log')
plt.legend(loc='upper left')
plt.xlabel(r"$\lambda$", fontsize=16)
plt.show()
../_images/svm_classification_example_7_0.svg
for i in range(4):
    plt.plot(lambda_vals, [wi[i,0] for wi in beta_vals])
plt.xlabel(r"$\lambda$", fontsize=16)
plt.xscale("log")
../_images/svm_classification_example_8_0.svg