Skip to content

Commit f7109c5

Browse files
fix(machine_learning): call features.any() in linear_discriminant_analysis (#15606)
In linear_discriminant_analysis, line 146 wrote if features.any: instead of calling if features.any():. Because the method object is always truthy, the else branch handling empty datasets was unreachable dead code, triggering ty warning[redundant-condition]. Call eatures.any() (matching line 107 in principal_component_analysis) and add a test case in test_linear_discriminant_analysis verifying that an empty dataset properly enters the else branch. Validation: - uvx ty check machine_learning/dimensionality_reduction.py -> All checks passed (0 diagnostics) - uvx --with scipy --with scikit-learn --with numpy pytest machine_learning/dimensionality_reduction.py --doctest-modules -> 9 passed - uvx ruff check / format -> clean
1 parent 25c712f commit f7109c5

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

‎machine_learning/dimensionality_reduction.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def linear_discriminant_analysis(
143143
assert classes > dimensions
144144

145145
# Check if features have been already loaded
146-
if features.any:
146+
if features.any():
147147
_, eigenvectors = eigh(
148148
covariance_between_classes(features, labels, classes),
149149
covariance_within_classes(features, labels, classes),
@@ -363,6 +363,11 @@ def test_linear_discriminant_analysis() -> None:
363363
)
364364
assert error_info.type is AssertionError
365365

366+
with pytest.raises(AssertionError):
367+
linear_discriminant_analysis(
368+
np.array([]), np.array([]), classes=2, dimensions=1
369+
)
370+
366371

367372
def test_principal_component_analysis() -> None:
368373
features = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

0 commit comments

Comments
 (0)