8000 remove ransac's residual_metric · scikit-learn/scikit-learn@2ec39c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ec39c0

Browse files
author
Joan Massich
committed
remove ransac's residual_metric
1 parent 59e3f7d commit 2ec39c0

File tree

3 files changed

+40
-62
lines changed

3 files changed

+40
-62
lines changed

sklearn/linear_model/ransac.py

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,6 @@ class RANSACRegressor(BaseEstimator, MetaEstimatorMixin, RegressorMixin):
135135
as 0.99 (the default) and e is the current fraction of inliers w.r.t.
136136
the total number of samples.
137137
138-
residual_metric : callable, optional
139-
Metric to reduce the dimensionality of the residuals to 1 for
140-
multi-dimensional target values ``y.shape[1] > 1``. By default the sum
141-
of absolute differences is used::
142-
143-
lambda dy: np.sum(np.abs(dy), axis=1)
144-
145-
.. deprecated:: 0.18
146-
``residual_metric`` is deprecated from 0.18 and will be removed in
147-
0.20. Use ``loss`` instead.
148-
149138
loss : string, callable, optional, default "absolute_loss"
150139
String inputs, "absolute_loss" and "squared_loss" are supported which
151140
find the absolute loss and squared loss per sample
@@ -205,8 +194,8 @@ def __init__(self, base_estimator=None, min_samples=None,
205194
residual_threshold=None, is_data_valid=None,
206195
is_model_valid=None, max_trials=100, max_skips=np.inf,
207196
stop_n_inliers=np.inf, stop_score=np.inf,
208-
stop_probability=0.99, residual_metric=None,
209-
loss='absolute_loss', random_state=None):
197+
stop_probability=0.99, loss='absolute_loss',
198+
random_state=None):
210199

211200
self.base_estimator = base_estimator
212201
self.min_samples = min_samples
@@ -218,7 +207,6 @@ def __init__(self, base_estimator=None, min_samples=None,
218207
self.stop_n_inliers = stop_n_inliers
219208
self.stop_score = stop_score
220209
self.stop_probability = stop_probability
221-
self.residual_metric = residual_metric
222210
self.random_state = random_state
223211
self.loss = loss
224212

@@ -281,12 +269,6 @@ def fit(self, X, y, sample_weight=None):
281269
else:
282270
residual_threshold = self.residual_threshold
283271

284-
if self.residual_metric is not None:
285-
warnings.warn(
286-
"'residual_metric' was deprecated in version 0.18 and "
287-
"will be removed in version 0.20. Use 'loss' instead.",
288-
DeprecationWarning)
289-
290272
if self.loss == "absolute_loss":
291273
if y.ndim == 1:
292274
loss_function = lambda y_true, y_pred: np.abs(y_true - y_pred)
@@ -379,15 +361,7 @@ def fit(self, X, y, sample_weight=None):
379361

380362
# residuals of all data for current random sample model
381363
y_pred = base_estimator.predict(X)
382-
383-
# XXX: Deprecation: Remove this if block in 0.20
384-
if self.residual_metric is not None:
385-
diff = y_pred - y
386-
if diff.ndim == 1:
387-
diff = diff.reshape(-1, 1)
388-
residuals_subset = self.residual_metric(diff)
389-
else:
390-
residuals_subset = loss_function(y, y_pred)
364+
residuals_subset = loss_function(y, y_pred)
391365

392366
# classify data into inliers and outliers
393367
inlier_mask_subset = residuals_subset < residual_threshold

sklearn/linear_model/tests/test_ransac.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -352,39 +352,6 @@ def test_ransac_multi_dimensional_targets():
352352
assert_equal(ransac_estimator.inlier_mask_, ref_inlier_mask)
353353

354354

355-
# XXX: Remove in 0.20
356-
def test_ransac_residual_metric():
357-
residual_metric1 = lambda dy: np.sum(np.abs(dy), axis=1)
358-
residual_metric2 = lambda dy: np.sum(dy ** 2, axis=1)
359-
360-
yyy = np.column_stack([y, y, y])
361-
362-
base_estimator = LinearRegression()
363-
ransac_estimator0 = RANSACRegressor(base_estimator, min_samples=2,
364-
residual_threshold=5, random_state=0)
365-
ransac_estimator1 = RANSACRegressor(base_estimator, min_samples=2,
366-
residual_threshold=5, random_state=0,
367-
residual_metric=residual_metric1)
368-
ransac_estimator2 = RANSACRegressor(base_estimator, min_samples=2,
369-
residual_threshold=5, random_state=0,
370-
residual_metric=residual_metric2)
371-
372-
# multi-dimensional
373-
ransac_estimator0.fit(X, yyy)
374-
assert_warns(DeprecationWarning, ransac_estimator1.fit, X, yyy)
375-
assert_warns(DeprecationWarning, ransac_estimator2.fit, X, yyy)
376-
assert_array_almost_equal(ransac_estimator0.predict(X),
377-
ransac_estimator1.predict(X))
378-
assert_array_almost_equal(ransac_estimator0.predict(X),
379-
ransac_estimator2.predict(X))
380-
381-
# one-dimensional
382-
ransac_estimator0.fit(X, y)
383-
assert_warns(DeprecationWarning, ransac_estimator2.fit, X, y)
384-
assert_array_almost_equal(ransac_estimator0.predict(X),
385-
ransac_estimator2.predict(X))
386-
387-
388355
def test_ransac_residual_loss():
389356
loss_multi1 = lambda y_true, y_pred: np.sum(np.abs(y_true - y_pred), axis=1)
390357
loss_multi2 = lambda y_true, y_pred: np.sum((y_true - y_pred) ** 2, axis=1)

sklearn/multioutput.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,43 @@ def partial_fit(self, X, y, sample_weight=None):
247247
super(MultiOutputRegressor, self).partial_fit(
248248
X, y, sample_weight=sample_weight)
249249

250+
def score(self, X, y, sample_weight=None):
251+
"""Returns the coefficient of determination R^2 of the prediction.
252+
253+
The coefficient R^2 is defined as (1 - u/v), where u is the residual
254+
sum of squares ((y_true - y_pred) ** 2).sum() and v is the regression
255+
sum of squares ((y_true - y_true.mean()) ** 2).sum().
256+
Best possible score is 1.0 and it can be negative (because the
257+
model can be arbitrarily worse). A constant model that always
258+
predicts the expected value of y, disregarding the input features,
259+
would get a R^2 score of 0.0.
260+
261+
Notes
262+
-----
263+
R^2 is calculated by weighting all the targets equally using
264+
`multioutput='uniform_average'`.
265+
266+
Parameters
267+
----------
268+
X : array-like, shape (n_samples, n_features)
269+
Test samples.
270+
271+
y : array-like, shape (n_samples) or (n_samples, n_outputs)
272+
True values for X.
273+
274+
sample_weight : array-like, shape [n_samples], optional
275+
Sample weights.
276+
277+
Returns
278+
-------
279+
score : float
280+
R^2 of self.predict(X) wrt. y.
281+
"""
282+
# XXX remove in 0.19 when r2_score default for multioutput changes
283+
from .metrics import r2_score
284+
return r2_score(y, self.predict(X), sample_weight=sample_weight,
285+
multioutput='uniform_average')
286+
250287

251288
class MultiOutputClassifier(MultiOutputEstimator, ClassifierMixin):
252289
"""Multi target classification

0 commit comments

Comments
 (0)
0