Skip to content

Commit 54931e3

Browse files
committed
renamed weight to sample_weight in sklearn/isotonic.py
1 parent 22e1f69 commit 54931e3

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

sklearn/isotonic.py

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
from .base import BaseEstimator, TransformerMixin, RegressorMixin
99
from .utils import as_float_array, check_arrays
1010
from ._isotonic import _isotonic_regression
11+
import warnings
1112

1213

13-
def isotonic_regression(y, weight=None, y_min=None, y_max=None):
14+
def isotonic_regression(y, weight=None, y_min=None, y_max=None,
15+
sample_weight=None):
1416
"""Solve the isotonic regression model::
1517
1618
min sum w[i] (y[i] - y_[i]) ** 2
@@ -27,7 +29,7 @@ def isotonic_regression(y, weight=None, y_min=None, y_max=None):
2729
y : iterable of floating-point values
2830
The data.
2931
30-
weight : iterable of floating-point values, optional, default: None
32+
sample_weight : iterable of floating-point values, optional, default: None
3133
Weights on each point of the regression.
3234
If None, weight is set to 1 (equal weights).
3335
@@ -47,24 +49,31 @@ def isotonic_regression(y, weight=None, y_min=None, y_max=None):
4749
"Active set algorithms for isotonic regression; A unifying framework"
4850
by Michael J. Best and Nilotpal Chakravarti, section 3.
4951
"""
52+
if weight is not None:
53+
warnings.warn("'weight' was renamed to 'sample_weight' and will "
54+
"be removed in 0.16.",
55+
DeprecationWarning)
56+
sample_weight = weight
57+
5058
y = np.asarray(y, dtype=np.float)
51-
if weight is None:
52-
weight = np.ones(len(y), dtype=y.dtype)
59+
if sample_weight is None:
60+
sample_weight = np.ones(len(y), dtype=y.dtype)
5361
else:
54-
weight = np.asarray(weight, dtype=np.float)
62+
sample_weight = np.asarray(sample_weight, dtype=np.float)
5563
if y_min is not None or y_max is not None:
5664
y = np.copy(y)
57-
weight = np.copy(weight)
58-
C = np.dot(weight, y * y) * 10 # upper bound on the cost function
65+
sample_weight = np.copy(sample_weight)
66+
# upper bound on the cost function
67+
C = np.dot(sample_weight, y * y) * 10
5968
if y_min is not None:
6069
y[0] = y_min
61-
weight[0] = C
70+
sample_weight[0] = C
6271
if y_max is not None:
6372
y[-1] = y_max
64-
weight[-1] = C
73+
sample_weight[-1] = C
6574

6675
solution = np.empty(len(y))
67-
return _isotonic_regression(y, weight, solution)
76+
return _isotonic_regression(y, sample_weight, solution)
6877

6978

7079
class IsotonicRegression(BaseEstimator, TransformerMixin, RegressorMixin):
@@ -111,11 +120,11 @@ def __init__(self, y_min=None, y_max=None):
111120
self.y_min = y_min
112121
self.y_max = y_max
113122

114-
def _check_fit_data(self, X, y, weight=None):
123+
def _check_fit_data(self, X, y, sample_weight=None):
115124
if len(X.shape) != 1:
116125
raise ValueError("X should be a vector")
117126

118-
def fit(self, X, y, weight=None):
127+
def fit(self, X, y, weight=None, sample_weight=None):
119128
"""Fit the model using X, y as training data.
120129
121130
Parameters
@@ -126,7 +135,7 @@ def fit(self, X, y, weight=None):
126135
y : array-like, shape=(n_samples,)
127136
Training target.
128137
129-
weight : array-like, shape=(n_samples,), optional, default: None
138+
sample_weight : array-like, shape=(n_samples,), optional, default: None
130139
Weights. If set to None, all weights will be set to 1 (equal
131140
weights).
132141
@@ -140,12 +149,20 @@ def fit(self, X, y, weight=None):
140149
X is stored for future use, as `transform` needs X to interpolate
141150
new input data.
142151
"""
143-
X, y, weight = check_arrays(X, y, weight, sparse_format='dense')
152+
if weight is not None:
153+
warnings.warn("'weight' was renamed to 'sample_weight' and will "
154+
"be removed in 0.16.",
155+
DeprecationWarning)
156+
sample_weight = weight
157+
158+
X, y, sample_weight = check_arrays(X, y, sample_weight,
159+
sparse_format='dense')
144160
y = as_float_array(y)
145-
self._check_fit_data(X, y, weight)
161+
self._check_fit_data(X, y, sample_weight)
146162
order = np.argsort(X)
147163
self.X_ = as_float_array(X[order], copy=False)
148-
self.y_ = isotonic_regression(y[order], weight, self.y_min, self.y_max)
164+
self.y_ = isotonic_regression(y[order], sample_weight, self.y_min,
165+
self.y_max)
149166
return self
150167

151168
def transform(self, T):
@@ -169,7 +186,7 @@ def transform(self, T):
169186
bounds_error=True)
170187
return f(T)
171188

172-
def fit_transform(self, X, y, weight=None):
189+
def fit_transform(self, X, y, weight=None, sample_weight=None):
173190
"""Fit model and transform y by linear interpolation.
174191
175192
Parameters
@@ -180,7 +197,7 @@ def fit_transform(self, X, y, weight=None):
180197
y : array-like, shape=(n_samples,)
181198
Training target.
182199
183-
weight : array-like, shape=(n_samples,), optional, default: None
200+
sample_weight : array-like, shape=(n_samples,), optional, default: None
184201
Weights. If set to None, all weights will be equal to 1 (equal
185202
weights).
186203
@@ -195,13 +212,21 @@ def fit_transform(self, X, y, weight=None):
195212
for future use, as `transform` needs X to interpolate new input
196213
data.
197214
"""
198-
X, y, weight = check_arrays(X, y, weight, sparse_format='dense')
215+
if weight is not None:
216+
warnings.warn("'weight' was renamed to 'sample_weight' and will "
217+
"be removed in 0.16.",
218+
DeprecationWarning)
219+
sample_weight = weight
220+
221+
X, y, sample_weight = check_arrays(X, y, sample_weight,
222+
sparse_format='dense')
199223
y = as_float_array(y)
200-
self._check_fit_data(X, y, weight)
224+
self._check_fit_data(X, y, sample_weight)
201225
order = np.lexsort((y, X))
202226
order_inv = np.argsort(order)
203227
self.X_ = as_float_array(X[order], copy=False)
204-
self.y_ = isotonic_regression(y[order], weight, self.y_min, self.y_max)
228+
self.y_ = isotonic_regression(y[order], sample_weight, self.y_min,
229+
self.y_max)
205230
return self.y_[order_inv]
206231

207232
def predict(self, T):

0 commit comments

Comments
 (0)