Skip to content

TST use global_random_seed in sklearn/tests/test_dummy.py #25884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 39 additions & 35 deletions sklearn/tests/test_dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ def test_most_frequent_and_prior_strategy_multioutput():
_check_behavior_2d(clf)


def test_stratified_strategy():
def test_stratified_strategy(global_random_seed):
X = [[0]] * 5 # ignored
y = [1, 2, 1, 1, 2]
clf = DummyClassifier(strategy="stratified", random_state=0)
clf = DummyClassifier(strategy="stratified", random_state=global_random_seed)
clf.fit(X, y)

X = [[0]] * 500
Expand All @@ -138,11 +138,11 @@ def test_stratified_strategy():
_check_predict_proba(clf, X, y)


def test_stratified_strategy_multioutput():
def test_stratified_strategy_multioutput(global_random_seed):
X = [[0]] * 5 # ignored
y = np.array([[2, 1], [2, 2], [1, 1], [1, 2], [1, 1]])

clf = DummyClassifier(strategy="stratified", random_state=0)
clf = DummyClassifier(strategy="stratified", random_state=global_random_seed)
clf.fit(X, y)

X = [[0]] * 500
Expand All @@ -157,10 +157,10 @@ def test_stratified_strategy_multioutput():
_check_behavior_2d(clf)


def test_uniform_strategy():
def test_uniform_strategy(global_random_seed):
X = [[0]] * 4 # ignored
y = [1, 2, 1, 1]
clf = DummyClassifier(strategy="uniform", random_state=0)
clf = DummyClassifier(strategy="uniform", random_state=global_random_seed)
clf.fit(X, y)

X = [[0]] * 500
Expand All @@ -171,10 +171,10 @@ def test_uniform_strategy():
_check_predict_proba(clf, X, y)


def test_uniform_strategy_multioutput():
def test_uniform_strategy_multioutput(global_random_seed):
X = [[0]] * 4 # ignored
y = np.array([[2, 1], [2, 2], [1, 2], [1, 1]])
clf = DummyClassifier(strategy="uniform", random_state=0)
clf = DummyClassifier(strategy="uniform", random_state=global_random_seed)
clf.fit(X, y)

X = [[0]] * 500
Expand Down Expand Up @@ -216,24 +216,28 @@ def test_classifier_score_with_None(y, y_test):
@pytest.mark.parametrize(
"strategy", ["stratified", "most_frequent", "prior", "uniform", "constant"]
)
def test_classifier_prediction_independent_of_X(strategy):
def test_classifier_prediction_independent_of_X(strategy, global_random_seed):
y = [0, 2, 1, 1]
X1 = [[0]] * 4
clf1 = DummyClassifier(strategy=strategy, random_state=0, constant=0)
clf1 = DummyClassifier(
strategy=strategy, random_state=global_random_seed, constant=0
)
clf1.fit(X1, y)
predictions1 = clf1.predict(X1)

X2 = [[1]] * 4
clf2 = DummyClassifier(strategy=strategy, random_state=0, constant=0)
clf2 = DummyClassifier(
strategy=strategy, random_state=global_random_seed, constant=0
)
clf2.fit(X2, y)
predictions2 = clf2.predict(X2)

assert_array_equal(predictions1, predictions2)


def test_mean_strategy_regressor():
def test_mean_strategy_regressor(global_random_seed):

random_state = np.random.RandomState(seed=1)
random_state = np.random.RandomState(seed=global_random_seed)

X = [[0]] * 4 # ignored
y = random_state.randn(4)
Expand All @@ -243,9 +247,9 @@ def test_mean_strategy_regressor():
assert_array_equal(reg.predict(X), [np.mean(y)] * len(X))


def test_mean_strategy_multioutput_regressor():
def test_mean_strategy_multioutput_regressor(global_random_seed):

random_state = np.random.RandomState(seed=1)
random_state = np.random.RandomState(seed=global_random_seed)

X_learn = random_state.randn(10, 10)
y_learn = random_state.randn(10, 5)
Expand All @@ -271,9 +275,9 @@ def test_regressor_exceptions():
reg.predict([])


def test_median_strategy_regressor():
def test_median_strategy_regressor(global_random_seed):

random_state = np.random.RandomState(seed=1)
random_state = np.random.RandomState(seed=global_random_seed)

X = [[0]] * 5 # ignored
y = random_state.randn(5)
Expand All @@ -283,9 +287,9 @@ def test_median_strategy_regressor():
assert_array_equal(reg.predict(X), [np.median(y)] * len(X))


def test_median_strategy_multioutput_regressor():
def test_median_strategy_multioutput_regressor(global_random_seed):

random_state = np.random.RandomState(seed=1)
random_state = np.random.RandomState(seed=global_random_seed)

X_learn = random_state.randn(10, 10)
y_learn = random_state.randn(10, 5)
Expand All @@ -305,9 +309,9 @@ def test_median_strategy_multioutput_regressor():
_check_behavior_2d(est)


def test_quantile_strategy_regressor():
def test_quantile_strategy_regressor(global_random_seed):

random_state = np.random.RandomState(seed=1)
random_state = np.random.RandomState(seed=global_random_seed)

X = [[0]] * 5 # ignored
y = random_state.randn(5)
Expand All @@ -329,9 +333,9 @@ def test_quantile_strategy_regressor():
assert_array_equal(reg.predict(X), [np.percentile(y, q=30)] * len(X))


def test_quantile_strategy_multioutput_regressor():
def test_quantile_strategy_multioutput_regressor(global_random_seed):

random_state = np.random.RandomState(seed=1)
random_state = np.random.RandomState(seed=global_random_seed)

X_learn = random_state.randn(10, 10)
y_learn = random_state.randn(10, 5)
Expand Down Expand Up @@ -382,9 +386,9 @@ def test_quantile_strategy_empty_train():
est.fit([], [])


def test_constant_strategy_regressor():
def test_constant_strategy_regressor(global_random_seed):

random_state = np.random.RandomState(seed=1)
random_state = np.random.RandomState(seed=global_random_seed)

X = [[0]] * 5 # ignored
y = random_state.randn(5)
Expand All @@ -401,9 +405,9 @@ def test_constant_strategy_regressor():
assert not isinstance(reg.constant, np.ndarray)


def test_constant_strategy_multioutput_regressor():
def test_constant_strategy_multioutput_regressor(global_random_seed):

random_state = np.random.RandomState(seed=1)
random_state = np.random.RandomState(seed=global_random_seed)

X_learn = random_state.randn(10, 10)
y_learn = random_state.randn(10, 5)
Expand Down Expand Up @@ -444,8 +448,8 @@ def test_constants_not_specified_regressor():
est.fit(X, y)


def test_constant_size_multioutput_regressor():
random_state = np.random.RandomState(seed=1)
def test_constant_size_multioutput_regressor(global_random_seed):
random_state = np.random.RandomState(seed=global_random_seed)
X = random_state.randn(10, 10)
y = random_state.randn(10, 5)

Expand Down Expand Up @@ -547,11 +551,11 @@ def test_constant_strategy_sparse_target():
)


def test_uniform_strategy_sparse_target_warning():
def test_uniform_strategy_sparse_target_warning(global_random_seed):
X = [[0]] * 5 # ignored
y = sp.csc_matrix(np.array([[2, 1], [2, 2], [1, 4], [4, 2], [1, 1]]))

clf = DummyClassifier(strategy="uniform", random_state=0)
clf = DummyClassifier(strategy="uniform", random_state=global_random_seed)
with pytest.warns(UserWarning, match="the uniform strategy would not save memory"):
clf.fit(X, y)

Expand All @@ -565,11 +569,11 @@ def test_uniform_strategy_sparse_target_warning():
assert_almost_equal(p[4], 1 / 3, decimal=1)


def test_stratified_strategy_sparse_target():
def test_stratified_strategy_sparse_target(global_random_seed):
X = [[0]] * 5 # ignored
y = sp.csc_matrix(np.array([[4, 1], [0, 0], [1, 1], [1, 4], [1, 1]]))

clf = DummyClassifier(strategy="stratified", random_state=0)
clf = DummyClassifier(strategy="stratified", random_state=global_random_seed)
clf.fit(X, y)

X = [[0]] * 500
Expand Down Expand Up @@ -599,8 +603,8 @@ def test_most_frequent_and_prior_strategy_sparse_target():
assert_array_equal(y_pred.toarray(), y_expected)


def test_dummy_regressor_sample_weight(n_samples=10):
random_state = np.random.RandomState(seed=1)
def test_dummy_regressor_sample_weight(global_random_seed, n_samples=10):
random_state = np.random.RandomState(seed=global_random_seed)

X = [[0]] * n_samples
y = random_state.rand(n_samples)
Expand Down