[MRG] Fix mean centering in ot.dr.fda and ot.dr.wda - #840
Open
deeb01 wants to merge 2 commits into
Open
Conversation
np.mean(X) returns the mean over all entries rather than the per-feature mean, so `X -= mx.reshape((1, -1))` subtracted a single scalar from every feature. The `proj` callables therefore did not center the data, contrary to their documented behaviour. In `fda` the same pattern appears in the class means, where `mxc[:, i] = np.mean(xc[i])` fills the column of class i with a scalar. Every class mean becomes a constant vector, so the between-class scatter matrix reduces to a multiple of the all-ones outer product and carries no information about which features separate the classes. The generalized eigenproblem eig(Cb, Cw + reg*I) then returns a direction driven only by the within-class scatter. On data separated along a single axis, `fda` returned a direction essentially orthogonal to it; it now agrees with sklearn's LinearDiscriminantAnalysis to within numerical precision. Both functions also mutated the caller's array through the in-place `-=`, which is now avoided. Adds non-regression tests for the recovered direction, for the centering of `proj`, and for the absence of input mutation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Types of changes
Bug fix (non-breaking change which fixes an issue).
Motivation and context / Related issue
np.mean(X)withoutaxisreturns a scalar;.reshape((1, -1))on it gives(1, 1)and broadcasts silently.X -= mx.reshape((1, -1))subtracts one scalar from every feature, soprojdoes not center as documented.wda's learnedPis unaffected — the cost sees only pairwise differences.fda,mxc[:, i] = np.mean(xc[i])makes every class mean a constant vector, soCb = [Σ_i (m_i − m̄)²]·11ᵀ: rank one, no information about which features separate the classes.Xin place.On 3 classes separated along feature 0 with 4 noise features,
fdagave[0.68, 0.99, 0.63, 1.00, 0.97]; now[1.00, 0.022, 0.043, 0.034, 0.124], matching sklearn's LDA to|cos| = 1.0.ot.dr.fdais the baseline inexamples/others/plot_WDA.py.How has this been tested (if it applies)
3 new tests, each verified failing on master.
test_dr.py9 passed, pre-commit clean.PR checklist