Skip to content

Commit 3c92686

Browse files
committed
Merge pull request scikit-learn#3564 from jnothman/bounds
[MRG+1] update sklearn.svm.bounds to use recent helpers
2 parents 45e82c3 + 56c539e commit 3c92686

File tree

1 file changed

+16
-30
lines changed

1 file changed

+16
-30
lines changed

sklearn/svm/bounds.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import operator
1+
"""Determination of parameter bounds"""
2+
# Author: Paolo Losi
3+
# License: BSD 3 clause
4+
25
import numpy as np
36

7+
from ..preprocessing import LabelBinarizer
8+
from ..utils.validation import check_consistent_length, check_array
9+
from ..utils.extmath import safe_sparse_dot
10+
411

512
def l1_min_c(X, y, loss='l2', fit_intercept=True, intercept_scaling=1.0):
613
"""
@@ -41,44 +48,23 @@ def l1_min_c(X, y, loss='l2', fit_intercept=True, intercept_scaling=1.0):
4148
l1_min_c: float
4249
minimum value for C
4350
"""
44-
import scipy.sparse as sp
4551

4652
if loss not in ('l2', 'log'):
4753
raise ValueError('loss type not in ("l2", "log")')
4854

49-
y = np.asarray(y)
50-
51-
if sp.issparse(X):
52-
X = sp.csc_matrix(X)
53-
hstack = sp.hstack
54-
dot = operator.mul
55-
else:
56-
X = np.asarray(X)
57-
hstack = np.hstack
58-
dot = np.dot
55+
X = check_array(X, accept_sparse='csc')
56+
check_consistent_length(X, y)
5957

58+
Y = LabelBinarizer(neg_label=-1).fit_transform(y).T
59+
# maximum absolute value over classes and features
60+
den = np.max(np.abs(safe_sparse_dot(Y, X)))
6061
if fit_intercept:
6162
bias = intercept_scaling * np.ones((np.size(y), 1))
62-
X = hstack((X, bias))
63-
64-
classes = np.unique(y)
65-
n_classes = np.size(classes)
66-
if n_classes <= 2:
67-
c = classes[0]
68-
y = y.reshape((1, -1))
69-
_y = np.empty(y.shape)
70-
_y[y == c] = 1
71-
_y[y != c] = -1
72-
else:
73-
_y = np.empty((n_classes, np.size(y)))
74-
for i, c in enumerate(classes):
75-
_y[i, y == c] = 1
76-
_y[i, y != c] = -1
77-
78-
den = np.max(np.abs(dot(_y, X)))
63+
den = max(den, abs(np.dot(Y, bias)).max())
7964

8065
if den == 0.0:
81-
raise ValueError('Ill-posed l1_min_c calculation')
66+
raise ValueError('Ill-posed l1_min_c calculation: l1 will always '
67+
'select zero coefficients for this data')
8268
if loss == 'l2':
8369
return 0.5 / den
8470
else: # loss == 'log':

0 commit comments

Comments
 (0)