8000 Revert "Common check for sample weight invariance with removed sample… · viclafargue/scikit-learn@3d94656 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3d94656

Browse files
rthviclafargue
authored andcommitted
Revert "Common check for sample weight invariance with removed samples (scikit-learn#16507)"
This reverts commit 77279d6.
1 parent dcb4090 commit 3d94656

File tree

10 files changed

+14
-173
lines changed

10 files changed

+14
-173
lines changed

sklearn/calibration.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,6 @@ class that has the highest probability, and can thus be different
233233
check_is_fitted(self)
234234
return self.classes_[np.argmax(self.predict_proba(X), axis=1)]
235235

236-
def _more_tags(self):
237-
return {
238-
'_xfail_test': {
239-
'check_sample_weights_invariance(kind=zeros)':
240-
'zero sample_weight is not equivalent to removing samples',
241-
}
242-
}
243-
244236

245237
class _CalibratedClassifier:
246238
"""Probability calibration with isotonic regression or sigmoid.

sklearn/cluster/_kmeans.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,14 +1209,6 @@ def score(self, X, y=None, sample_weight=None):
12091209
return -_labels_inertia(X, sample_weight, x_squared_norms,
12101210
self.cluster_centers_)[1]
12111211

1212-
def _more_tags(self):
1213-
return {
1214-
'_xfail_test': {
1215-
'check_sample_weights_invariance(kind=zeros)':
1216-
'zero sample_weight is not equivalent to removing samples',
1217-
}
1218-
}
1219-
12201212

12211213
def _mini_batch_step(X, sample_weight, x_squared_norms, centers, weight_sums,
12221214
old_center_buffer, compute_squared_diff,
@@ -1873,11 +1865,3 @@ def predict(self, X, sample_weight=None):
18731865

18741866
X = self._check_test_data(X)
18751867
return self._labels_inertia_minibatch(X, sample_weight)[0]
1876-
1877-
def _more_tags(self):
1878-
return {
1879-
'_xfail_test': {
1880-
'check_sample_weights_invariance(kind=zeros)':
1881-
'zero sample_weight is not equivalent to removing samples',
1882-
}
1883-
}

sklearn/ensemble/_iforest.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -476,14 +476,6 @@ def _compute_score_samples(self, X, subsample_features):
476476
)
477477
return scores
478478

479-
def _more_tags(self):
480-
return {
481-
'_xfail_test': {
482-
'check_sample_weights_invariance(kind=zeros)':
483-
'zero sample_weight is not equivalent to removing samples',
484-
}
485-
}
486-
487479

488480
def _average_path_length(n_samples_leaf):
489481
"""

sklearn/linear_model/_logistic.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,11 +2083,3 @@ def score(self, X, y, sample_weight=None):
20832083
scoring = get_scorer(scoring)
20842084

20852085
return scoring(self, X, y, sample_weight=sample_weight)
2086-
2087-
def _more_tags(self):
2088-
return {
2089-
'_xfail_test': {
2090-
'check_sample_weights_invariance(kind=zeros)':
2091-
'zero sample_weight is not equivalent to removing samples',
2092-
}
2093-
}

sklearn/linear_model/_ransac.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,3 @@ def score(self, X, y):
502502
check_is_fitted(self)
503503

504504
return self.estimator_.score(X, y)
505-
506-
def _more_tags(self):
507-
return {
508-
'_xfail_test': {
509-
'check_sample_weights_invariance(kind=zeros)':
510-
'zero sample_weight is not equivalent to removing samples',
511-
}
512-
}

sklearn/linear_model/_ridge.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,11 +1909,3 @@ def fit(self, X, y, sample_weight=None):
19091909
@property
19101910
def classes_(self):
19111911
return self._label_binarizer.classes_
1912-
1913-
def _more_tags(self):
1914-
return {
1915-
'_xfail_test': {
1916-
'check_sample_weights_invariance(kind=zeros)':
1917-
'zero sample_weight is not equivalent to removing samples',
1918-
}
1919-
}

sklearn/linear_model/_stochastic_gradient.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,14 +1094,6 @@ def predict_log_proba(self):
10941094
def _predict_log_proba(self, X):
10951095
return np.log(self.predict_proba(X))
10961096

1097-
def _more_tags(self):
1098-
return {
1099-
'_xfail_test': {
1100-
'check_sample_weights_invariance(kind=zeros)':
1101-
'zero sample_weight is not equivalent to removing samples',
1102-
}
1103-
}
1104-
11051097

11061098
class BaseSGDRegressor(RegressorMixin, BaseSGD):
11071099

@@ -1583,11 +1575,3 @@ def __init__(self, loss="squared_loss", *, penalty="l2", alpha=0.0001,
15831575
validation_fraction=validation_fraction,
15841576
n_iter_no_change=n_iter_no_change, warm_start=warm_start,
15851577
average=average)
1586-
1587-
def _more_tags(self):
1588-
return {
1589-
'_xfail_test': {
1590-
'check_sample_weights_invariance(kind=zeros)':
1591-
'zero sample_weight is not equivalent to removing samples',
1592-
}
1593-
}

sklearn/neighbors/_kde.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,3 @@ def sample(self, n_samples=1, random_state=None):
274274
correction = (gammainc(0.5 * dim, 0.5 * s_sq) ** (1. / dim)
275275
* self.bandwidth / np.sqrt(s_sq))
276276
return data[i] + X * correction[:, np.newaxis]
277-
278-
def _more_tags(self):
279-
return {
280-
'_xfail_test': {
281-
'check_sample_weights_invariance(kind=zeros)':
282-
'sample_weight must have positive values',
283-
}
284-
}

sklearn/svm/_classes.py

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,6 @@ def fit(self, X, y, sample_weight=None):
244244

245245
return self
246246

247-
def _more_tags(self):
248-
return {
249-
'_xfail_test': {
250-
'check_sample_weights_invariance(kind=zeros)':
251-
'zero sample_weight is not equivalent to removing samples',
252-
}
253-
}
254-
255247

256248
class LinearSVR(RegressorMixin, LinearModel):
257249
"""Linear Support Vector Regression.
@@ -432,14 +424,6 @@ def fit(self, X, y, sample_weight=None):
432424

433425
return self
434426

435-
def _more_tags(self):
436-
return {
437-
'_xfail_test': {
438-
'check_sample_weights_invariance(kind=zeros)':
439-
'zero sample_weight is not equivalent to removing samples',
440-
}
441-
}
442-
443427

444428
class SVC(BaseSVC):
445429
"""C-Support Vector Classification.
@@ -666,14 +650,6 @@ def __init__(self, *, C=1.0, kernel='rbf', degree=3, gamma='scale',
666650
break_ties=break_ties,
667651
random_state=random_state)
668652

669-
def _more_tags(self):
670-
return {
671-
'_xfail_test': {
672-
'check_sample_weights_invariance(kind=zeros)':
673-
'zero sample_weight is not equivalent to removing samples',
674-
}
675-
}
676-
677653

678654
class NuSVC(BaseSVC):
679655
"""Nu-Support Vector Classification.
@@ -890,9 +866,7 @@ def _more_tags(self):
890866
'_xfail_checks': {
891867
'check_methods_subset_invariance':
892868
'fails for the decision_function method',
893-
'check_class_weight_classifiers': 'class_weight is ignored.',
894-
'check_sample_weights_invariance(kind=zeros)':
895-
'zero sample_weight is not equivalent to removing samples',
869+
'check_class_weight_classifiers': 'class_weight is ignored.'
896870
}
897871
}
898872

@@ -1053,14 +1027,6 @@ def probA_(self):
10531027
def probB_(self):
10541028
return self._probB
10551029

1056-
def _more_tags(self):
1057-
return {
1058-
'_xfail_test': {
1059-
'check_sample_weights_invariance(kind=zeros)':
1060-
'zero sample_weight is not equivalent to removing samples',
1061-
}
1062-
}
1063-
10641030

10651031
class NuSVR(RegressorMixin, BaseLibSVM):
10661032
"""Nu Support Vector Regression.
@@ -1191,14 +1157,6 @@ def __init__(self, *, nu=0.5, C=1.0, kernel='rbf', degree=3,
11911157
probability=False, cache_size=cache_size, class_weight=None,
11921158
verbose=verbose, max_iter=max_iter, random_state=None)
11931159

1194-
def _more_tags(self):
1195-
return {
1196-
'_xfail_test': {
1197-
'check_sample_weights_invariance(kind=zeros)':
1198-
'zero sample_weight is not equivalent to removing samples',
1199-
}
1200-
}
1201-
12021160

12031161
class OneClassSVM(OutlierMixin, BaseLibSVM):
12041162
"""Unsupervised Outlier Detection.
@@ -1413,11 +1371,3 @@ def probA_(self):
14131371
@property
14141372
def probB_(self):
14151373
return self._probB
1416-
1417-
def _more_tags(self):
1418-
return {
1419-
'_xfail_test': {
1420-
'check_sample_weights_invariance(kind=zeros)':
1421-
'zero sample_weight is not equivalent to removing samples',
1422-
}
1423-
}

sklearn/utils/estimator_checks.py

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ def _yield_checks(name, estimator):
6868
yield check_sample_weights_not_an_array
6969
yield check_sample_weights_list
7070
yield check_sample_weights_shape
71-
yield partial(check_sample_weights_invariance, kind='ones')
72-
yield partial(check_sample_weights_invariance, kind='zeros')
71+
yield check_sample_weights_invariance
7372
yield check_estimators_fit_returns_self
7473
yield partial(check_estimators_fit_returns_self, readonly_memmap=True)
7574

@@ -489,7 +488,6 @@ def check_estimator(Estimator, generate_only=False):
489488
warnings.warn(msg, FutureWarning)
490489

491490
checks_generator = _generate_class_checks(Estimator)
492-
estimator = _construct_instance(Estimator)
493491
else:
494492
# got an instance
495493
estimator = Estimator
@@ -499,19 +497,12 @@ def check_estimator(Estimator, generate_only=False):
499497
if generate_only:
500498
return checks_generator
501499

502-
xfail_checks = _safe_tags(estimator, '_xfail_test')
503-
504500
for estimator, check in checks_generator:
505-
check_name = _set_check_estimator_ids(check)
506-
if xfail_checks and check_name in xfail_checks:
507-
# skip tests marked as a known failure and raise a warning
508-
msg = xfail_checks[check_name]
509-
warnings.warn(f'Skipping {check_name}: {msg}', SkipTestWarning)
510-
continue
511501
try:
512502
check(estimator)
513503
except SkipTest as exception:
514-
# raise warning for tests that are are skipped
504+
# the only SkipTest thrown currently results from not
505+
# being able to import pandas.
515506
warnings.warn(str(exception), SkipTestWarning)
516507

517508

@@ -870,7 +861,7 @@ def check_sample_weights_shape(name, estimator_orig):
870861

871862

872863
@ignore_warnings(category=FutureWarning)
873-
def check_sample_weights_invariance(name, estimator_orig, kind="ones"):
864+
def check_sample_weights_invariance(name, estimator_orig):
874865
# check that the estimators yield same results for
875866
# unit weights and no weights
876867
if (has_fit_parameter(estimator_orig, "sample_weight") and
@@ -886,45 +877,25 @@ def check_sample_weights_invariance(name, estimator_orig, kind="ones"):
886877
X = np.array([[1, 3], [1, 3], [1, 3], [1, 3],
887878
[2, 1], [2, 1], [2, 1], [2, 1],
888879
[3, 3], [3, 3], [3, 3], [3, 3],
889-
[4, 1], [4, 1], [4, 1], [4, 1]], dtype=np.float64)
880+
[4, 1], [4, 1], [4, 1], [4, 1]], dtype=np.dtype('float'))
890881
y = np.array([1, 1, 1, 1, 2, 2, 2, 2,
891-
1, 1, 1, 1, 2, 2, 2, 2], dtype=np.int)
892-
893-
if kind == 'ones':
894-
X2 = X
895-
y2 = y
896-
sw2 = np.ones(shape=len(y))
897-
err_msg = (f"For {name} sample_weight=None is not equivalent to "
898-
f"sample_weight=ones")
899-
elif kind == 'zeros':
900-
# Construct a dataset that is very different to (X, y) if weights
901-
# are disregarded, but identical to (X, y) given weights.
902-
X2 = np.vstack([X, X + 1])
903-
y2 = np.hstack([y, 3 - y])
904-
sw2 = np.ones(shape=len(y) * 2)
905-
sw2[len(y):] = 0
906-
X2, y2, sw2 = shuffle(X2, y2, sw2, random_state=0)
907-
908-
err_msg = (f"For {name} sample_weight is not equivalent "
909-
f"to removing samples")
910-
else:
911-
raise ValueError
912-
882+
1, 1, 1, 1, 2, 2, 2, 2], dtype=np.dtype('int'))
913883
y = _enforce_estimator_tags_y(estimator1, y)
914-
y2 = _enforce_estimator_tags_y(estimator2, y2)
915884

916-
estimator1.fit(X, y=y, sample_weight=None)
917-
estimator2.fit(X2, y=y2, sample_weight=sw2)
885+
estimator1.fit(X, y=y, sample_weight=np.ones(shape=len(y)))
886+
estimator2.fit(X, y=y, sample_weight=None)
918887

919-
for method in ["predict", "predict_proba",
920-
"decision_function", "transform"]:
888+
for method in ["predict", "transform"]:
921889
if hasattr(estimator_orig, method):
922890
X_pred1 = getattr(estimator1, method)(X)
923891
X_pred2 = getattr(estimator2, method)(X)
924892
if sparse.issparse(X_pred1):
925893
X_pred1 = X_pred1.toarray()
926894
X_pred2 = X_pred2.toarray()
927-
assert_allclose(X_pred1, X_pred2, err_msg=err_msg)
895+
assert_allclose(X_pred1, X_pred2,
896+
err_msg="For %s sample_weight=None is not"
897+
" equivalent to sample_weight=ones"
898+
% name)
928899

929900

930901
@ignore_warnings(category=(FutureWarning, UserWarning))

0 commit comments

Comments
 (0)
0