Skip to content

fix(machine_learning): call features.any() in linear_discriminant_analysis - #15606

Open
hardikkaurani wants to merge 1 commit into
TheAlgorithms:masterfrom
hardikkaurani:fix/lda-features-any-check
Open

hardikkaurani wants to merge 1 commit into
TheAlgorithms:masterfrom
hardikkaurani:fix/lda-features-any-check

Conversation

@hardikkaurani

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Problem & Context

In machine_learning/dimensionality_reduction.py, line 146 in linear_discriminant_analysis wrote:

    if features.any:

instead of calling the method if features.any(): (compare with line 107 in principal_component_analysis, which correctly uses if features.any():).

Because an ndarray.any bound method object is always truthy, ty check emitted:

warning[redundant-condition]: Method `ndarray.any` is always truthy
   --> machine_learning/dimensionality_reduction.py:146:8
    |
146 |       if features.any:
    |          ^^^^^^^^^^^^ Did you mean to call this method?

As a result, the else branch handling empty datasets was unreachable dead code, and passing an empty dataset would bypass the check and fail with an unexpected error inside covariance_between_classes.

Fix

  • Call features.any() in linear_discriminant_analysis, matching principal_component_analysis.
  • Add a test case in test_linear_discriminant_analysis verifying that an empty dataset raises AssertionError.

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 machine_learning/dimensionality_reduction.py -> All checks passed.
  • uvx ruff format --check machine_learning/dimensionality_reduction.py -> 1 file already formatted.

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.

…lysis

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
Copilot AI balanced review requested due to automatic review settings October 19, 2026 11:19
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Oct 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@algorithms-keeper algorithms-keeper Bot added the enhancement This PR modified some existing files label Oct 19, 2026
@hardikkaurani

Copy link
Copy Markdown
Author

Hi @cclauss,

I've opened this PR to resolve an uncalled method in linear_discriminant_analysis within machine_learning/dimensionality_reduction.py.

Overview:

  • Line 146 previously checked if features.any: instead of calling the method if features.any(): (matching line 107 in principal_component_analysis).
  • Because a bound method object is always truthy, ty check flagged warning[redundant-condition]: Method ndarray.any is always truthy, making the else block dead code.
  • Calling features.any() ensures empty arrays correctly hit the else branch, and a test case was added to test_linear_discriminant_analysis to verify this behavior.

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.

Whenever you have a moment, could you please take a look? Thank you!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants