You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
LinearDiscriminantAnalysis predict probability bug #6848
There is no reason why constant 1 should be added to the computed probability after exponentiation and before inversion.
To verify this, I have run a one-to-one comparison between the outcome of the method and MATLAB's builtin LDA classifier on the Iris dataset. Only after removal of line 518, results match (up to a tolerance).
If everyone agrees on that, I am happy to submit a PR.
Reacted by Amazasp Shaumyan, Christian Stade-Schuldt and Olivier Grisel
Data point 24 (please note MATLAB uses 1-based indexing, so this is 23 in Python)
[1.0000 0.0000 0.0000]
Data point 102 (101 in Python)
[0.0000 0.0011 0.9989]
From a theoretical point of view, I still believe that the line prob += 1 is the one causing the trouble. decision_function(x) returns the log-probability up to an additive constant. Then, we only need to exponentiate to get the unnormalized probability distribution (i.e probability distribution up to a multiplicative constant due to the exponentiation).
prob *= -1 followed by np.reciprocal(prob, prob) is used to avoid overflow issues I believe.
In mathematical terms.
log (p(y=k|x)) = δ_k(x)
-log (p(y=k|x)) = - δ_k(x)
e ^ (-log (p(y=k|x)) ) = e ^ (- δ_k(x))
e ^ log(1/(p(y=k|x))) = e ^ (- δ_k(x))
1/(p(y=k|x)) = e ^ (- δ_k(x))
p(y=k|x) = 1 / ( e ^ (- δ_k(x))) (unnormalized)
As you can hopefully see in the above formulae adding a constant to the posterior probability is not required and indeed it affects the results.
δ (it should be δ_k) is the discriminant function for class k, i.e. the element of the vector returned by the decision_function() method which corresponds to class k. Please refer to 4.10 (p 109) of the textbook.
By π_k I meant the posterior probability for class k (it is not a fraction). However in the textbook π_k is used to denote the prior probability so apologies about the confusion. From now on, I denote the prior with π_k and the posterior p(y=k|x). (I' ve also edited my comment above to reflect this).
In the multiclass case the posterior probabilities are given by
p(y=k|x) = e ^ {δ_k(x)} / sum(e ^ {δ_k(x)}).
In the binary classification case, you can replace p(y=1|x) with 1 - p(y=0|x) and then substitute in the fraction (which only makes sense in the binary case) (eq 4.9 of textbook) and I suspect that this is where the +1 comes from. But in the multiclass case we can't do that and I think since it generalises the binary case it is more elegant.
The += 1 (alone) is not the problem. If you remove that line the tests fail with negative probability values...
The problem likely stems from the implementation of the decision_function from LinearClassifierMixin (which depends on the standard coef_ and intercept_ attributes) which is probably not directly equivalent to the traditional LDA parameterization of the log likelihood ratio of the positive class w.r.t. another fixed class. More work is needed to workout the correct normalization while keeping the LinearClassifierMixin parameterization which is useful to get LDA models to behave consistently like other sklearn linear models.
Hi guys, is this bug still open? I confirm that I get bizarre results using LDA with 3 classes on the IRIS dataset (X1 (horizontal axis) = sepal_length, X2 (vertical axis) = sepal_width)
In particular, the covariance matrix seems to be off: for example the covariance matrix given by lda.covariance_ = array([[0.259708 , 0.09086667], [0.09086667, 0.11308 ]])
and the covariance given by covariance.empirical_covariance(X) =
array([[ 0.68112222, -0.04215111], [-0.04215111, 0.18871289]])
Which is what I get manually and with pandas.cov.
Since the covariance is off, the gaussian estimate is also off and hence the predictions ? All the best
Activity
agamemnonc commentedon Jul 18, 2016
MATLAB snippet (version R2016a)
https://gist.github.com/agamemnonc/4c9ddb472cd7c3ac84718cee1d2f6ce6
Output:
0.0000 0.9993 0.0007
Python/sklearn snippet (sklearn version 0.17.1)
https://gist.github.com/agamemnonc/0d668ae2552188b73958be7961a7e9e3
Output (currently)
[ 0.33333333 0.33333333 0.33333333]
Output (when line in question removed)
[ 4.69392059e-20 9.99390132e-01 6.09867606e-04]
agamemnonc commentedon Jul 19, 2016
Sure, no problem.
Data point 24 (please note MATLAB uses 1-based indexing, so this is 23 in Python)
[1.0000 0.0000 0.0000]
Data point 102 (101 in Python)
[0.0000 0.0011 0.9989]
From a theoretical point of view, I still believe that the line prob += 1 is the one causing the trouble. decision_function(x) returns the log-probability up to an additive constant. Then, we only need to exponentiate to get the unnormalized probability distribution (i.e probability distribution up to a multiplicative constant due to the exponentiation).
prob *= -1 followed by np.reciprocal(prob, prob) is used to avoid overflow issues I believe.
In mathematical terms.
log (p(y=k|x)) = δ_k(x)
-log (p(y=k|x)) = - δ_k(x)
e ^ (-log (p(y=k|x)) ) = e ^ (- δ_k(x))
e ^ log(1/(p(y=k|x))) = e ^ (- δ_k(x))
1/(p(y=k|x)) = e ^ (- δ_k(x))
p(y=k|x) = 1 / ( e ^ (- δ_k(x))) (unnormalized)
As you can hopefully see in the above formulae adding a constant to the posterior probability is not required and indeed it affects the results.
agamemnonc commentedon Jul 25, 2016
Sorry, I should have made it more explicit. I follow the notation of the textbook "The elements of statistical learning" by Hastie, Tibshirani and Friedman which is freely available for download (pdf) here
https://web.stanford.edu/~hastie/local.ftp/Springer/OLD/ESLII_print4.pdf
δ (it should be δ_k) is the discriminant function for class k, i.e. the element of the vector returned by the decision_function() method which corresponds to class k. Please refer to 4.10 (p 109) of the textbook.
By π_k I meant the posterior probability for class k (it is not a fraction). However in the textbook π_k is used to denote the prior probability so apologies about the confusion. From now on, I denote the prior with π_k and the posterior p(y=k|x). (I' ve also edited my comment above to reflect this).
In the multiclass case the posterior probabilities are given by
p(y=k|x) = e ^ {δ_k(x)} / sum(e ^ {δ_k(x)}).
In the binary classification case, you can replace p(y=1|x) with 1 - p(y=0|x) and then substitute in the fraction (which only makes sense in the binary case) (eq 4.9 of textbook) and I suspect that this is where the +1 comes from. But in the multiclass case we can't do that and I think since it generalises the binary case it is more elegant.
ogrisel commentedon Sep 6, 2016
The += 1 (alone) is not the problem. If you remove that line the tests fail with negative probability values...
The problem likely stems from the implementation of the
decision_functionfromLinearClassifierMixin(which depends on the standardcoef_andintercept_attributes) which is probably not directly equivalent to the traditional LDA parameterization of the log likelihood ratio of the positive class w.r.t. another fixed class. More work is needed to workout the correct normalization while keeping theLinearClassifierMixinparameterization which is useful to get LDA models to behave consistently like other sklearn linear models.[-]LDA predict probability bug[/-][+]LinearDiscriminantAnalysis predict probability bug[/+]xavierbourretsicotte commentedon Jun 18, 2018
Hi guys, is this bug still open? I confirm that I get bizarre results using LDA with 3 classes on the IRIS dataset (X1 (horizontal axis) = sepal_length, X2 (vertical axis) = sepal_width)
In particular, the covariance matrix seems to be off: for example the covariance matrix given by lda.covariance_ = array([[0.259708 , 0.09086667], [0.09086667, 0.11308 ]])
and the covariance given by covariance.empirical_covariance(X) =
array([[ 0.68112222, -0.04215111], [-0.04215111, 0.18871289]])
Which is what I get manually and with pandas.cov.
Since the covariance is off, the gaussian estimate is also off and hence the predictions ? All the best
agramfort commentedon Jun 18, 2018
agamemnonc commentedon Aug 11, 2018
I have now submitted a PR which I think solves the issue: #11796
Do we perhaps want to include additional tests checking the output of
predict_probafor LDA and QDA both for the binary and multi-class cases?