Skip to content

Commit eec4449

Browse files
mohitthakur13Mohit Singh ThakurMohit Singh Thakurjeremiedbb
authored
FIX Raise informative error when validation set is too small in MLPRegressor (#24788)
Co-authored-by: Mohit Singh Thakur <mohit.thakur@tum.de> Co-authored-by: Mohit Singh Thakur <thakur.mohit@gmail.com> Co-authored-by: Jérémie du Boisberranger <jeremie@probabl.ai>
1 parent b90e09d commit eec4449

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`neural_network.MLPRegressor` now raises an informative error when
2+
`early_stopping` is set and the computed validation set is too small.
3+
By :user:`David Shumway <davidshumway>`.

sklearn/neural_network/_multilayer_perceptron.py

+6
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,12 @@ def _fit_stochastic(
668668
test_size=self.validation_fraction,
669669
stratify=stratify,
670670
)
671+
if X_val.shape[0] < 2:
672+
raise ValueError(
673+
"The validation set is too small. Increase 'validation_fraction' "
674+
"or the size of your dataset."
675+
)
676+
671677
if is_classifier(self):
672678
y_val = self._label_binarizer.inverse_transform(y_val)
673679
else:

sklearn/neural_network/tests/test_mlp.py

+8
Original file line numberDiff line numberDiff line change
@@ -1084,3 +1084,11 @@ def test_mlp_vs_poisson_glm_equivalent(global_random_seed):
10841084
random_state=np.random.RandomState(global_random_seed + 1),
10851085
).fit(X, y)
10861086
assert not np.allclose(mlp.predict(X), glm.predict(X), rtol=1e-4)
1087+
1088+
1089+
def test_minimum_input_sample_size():
1090+
"""Check error message when the validation set is too small."""
1091+
X, y = make_regression(n_samples=2, n_features=5, random_state=0)
1092+
model = MLPRegressor(early_stopping=True, random_state=0)
1093+
with pytest.raises(ValueError, match="The validation set is too small"):
1094+
model.fit(X, y)

0 commit comments

Comments
 (0)