Skip to content

Sort eigenvalues before truncating in linear_discriminant - #192

Open
chenyangsu wants to merge 1 commit into
spectralpython:developfrom
chenyangsu:fix/sort-lda-eigenvalues
Open

chenyangsu wants to merge 1 commit into
spectralpython:developfrom
chenyangsu:fix/sort-lda-eigenvalues

Conversation

@chenyangsu

Copy link
Copy Markdown

In linear_discriminant, slicing vals[:rank] and vecs[:, :rank] directly after np.linalg.eig(inv_cov_w.dot(cov_b)) relies on the returned eigenvalue ordering. For two classes separated only in band 1 with eigenvalues [0.0, 21.875], linear_discriminant returns [0.0] and drops the separating eigenvector, and a 3-class 6-band case similarly returns [3.597, 9.14e-17] in place of [3.597, 0.951].

Could we sort by vals.real descending before slicing the top rank components, matching principal_components and mnf? Happy to switch to a symmetric reformulation with eigh instead if you prefer.

Testing: SPECTRAL_DATA=.venv/sample-data pytest -m "not graphics" spectral/tests -> 1856 passed, 3 skipped (2 new tests in spectral/tests/test_spymath.py fail on develop).

@tboggs

tboggs commented Dec 1, 2026

Copy link
Copy Markdown
Member

Hello,

Sorting on vals.real (vice just vals) shouldn't be necessary, since numpy uses .real as the primary sorting key but it is probably fine to keep anyway.

Can you clarify your comment about failing tests? Are you stating that the 2 new tests will fail on develop without your changes to algorithms.py but will pass when the MR is merged?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants