8000 Fix for old scipy · scikit-learn/scikit-learn@096ecef · GitHub
[go: up one dir, main page]

Skip to content

Commit 096ecef

Browse files
committed
Fix for old scipy
1 parent 9fbc38a commit 096ecef

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

sklearn/linear_model/huber.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from scipy import optimize
44

55
from sklearn.base import BaseEstimator, RegressorMixin
6-
from sklearn.linear_model.base import center_data
6+
from sklearn.linear_model.base import center_data, LinearModel
7+
from sklearn.preprocessing import StandardScaler
78
from sklearn.utils import check_X_y
89

910
def _huber_loss_and_gradient(w, X, y, epsilon, alpha):
@@ -31,21 +32,31 @@ def _huber_loss_and_gradient(w, X, y, epsilon, alpha):
3132
return loss + alpha * np.dot(w, w), grad
3233

3334

34-
class HuberRegressor(RegressorMixin, BaseEstimator):
35-
def __init__(self, epsilon=0.1, n_iter=100, alpha=0.0001, warm_start=False):
35+
class HuberRegressor(LinearModel, RegressorMixin, BaseEstimator):
36+
def __init__(self, epsilon=0.1, n_iter=100, alpha=0.0001,
37+
warm_start=False, copy=True):
3638
self.epsilon = epsilon
3739
self.n_iter = n_iter
3840
self.alpha = alpha
3941
self.warm_start = warm_start
42+
self.copy = copy
4043

4144
def fit(self, X, y):
42-
X, y = check_X_y(X, y)
45+
X, y = check_X_y(X, y, copy=self.copy)
4346

44-
coef = getattr(self, 'coef', None)
47+
coef = getattr(self, 'coef_', None)
4548
if not self.warm_start or (self.warm_start and coef is None):
4649
self.coef_ = np.zeros(X.shape[1])
47-
self.coef, f, _ = optimize.fmin_l_bfgs_b(
48-
_huber_loss_and_gradient, self.coef_,
49-
args=(X, y, self.epsilon, self.alpha), maxiter=self.n_iter)
50+
51+
try:
52+
self.coef_, f, self.dict_ = optimize.fmin_l_bfgs_b(
53+
_huber_loss_and_gradient, self.coef_, approx_grad=True,
54+
args=(X, y, self.epsilon, self.alpha), maxiter=self.n_iter)
55+
except TypeError:
56+
self.coef_, f, self.dict_ = optimize.fmin_l_bfgs_b(
57+
_huber_loss_and_gradient, self.coef_,
58+
args=(X, y, self.epsilon, self.alpha))
59+
60+
self.intercept_ = 0.0
5061
self.loss_ = f
5162
return self

sklearn/linear_model/tests/test_huber.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from sklearn.datasets import make_regression
77
from sklearn.preprocessing import StandardScaler
8+
from sklearn.linear_model import HuberRegressor
89
from sklearn.linear_model.huber import _huber_loss_and_gradient
910

1011
X, y = make_regression(n_samples=50, n_features=20)
@@ -22,15 +23,15 @@ def test_huber_loss():
2223
w = rng.randn(X.shape[1])
2324
huber_loss = _huber_loss_and_gradient(w, X, y, 10000.0, 0.1)[0]
2425
squared_loss = 0.5 * np.sum((y - np.dot(X, w))**2) + 0.1 * np.dot(w, w)
25-
assert_equal(huber_loss, squared_loss)
26+
assert_equal(huber_loss, squared_loss, 4)
2627

2728
# When epsilon is very small, this should reduce to the linear loss.
2829
epsilon = 0.01
2930
huber_loss = _huber_loss_and_gradient(w, X, y, 0.01, 0.1)[0]
3031
linear_loss = (epsilon * np.sum(np.abs(y - np.dot(X, w))) -
3132
X.shape[0] * epsilon * epsilon / 2 +
3233
0.1 * np.dot(w, w))
33-
assert_equal(huber_loss, linear_loss)
34+
assert_almost_equal(huber_loss, linear_loss, 4)
3435

3536

3637
def test_huber_gradient():
@@ -45,3 +46,7 @@ def test_huber_gradient():
4546
grad_same = optimize.check_grad(
4647
loss_func, grad_func, w, X, y, 0.01, 0.1)
4748
assert_almost_equal(grad_same, 1e-6, 4)
49+
50+
51+
# def test_huber_convergence():
52+
# """Test that the loss function converges to zero."""

0 commit comments

Comments
 (0)
0