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
Incorrect implementation of confusion matrix in pred_table for Logit model #2968
The confusion matrix given by the pred_table() function gives the incorrect value. E.g.
y = np.array([[1],
[0],
[1],
[0],
[0],
[0],
[0],
[0],
[0],
[0]])
res = Logit(y,np.ones((10,1))).fit()
res.pred_table()
Outputs the following:
array([[ 0., 8.],
[ 0., 2.]])
This output is wrong on multiple accounts:
Given that label 1 is considered true and label 0 is considered false:
TP = 0, FP = 2, FN = 0, TN = 8
The correct predictions for each class should fall on the diagonal, however, in this case the correct predictions appear to be on the off diagonal.
As mentioned in Issue classification performance measures #1577tp, fp, fn, tn = map(float, pred_table.flatten()), if the true class is 0. However, when using Logit model the true class is almost always 1. And even if the true class is 0, then also TP is going to be cm[0,1] and not cm[0,0], where cm is the confusion matrix.
The model, the actual confusion matrix, considering label 1 as the positive class, should be:
[[0,2],
[0,8]]
#representing the traditional
[[TP, FN],
[FP, TN]]
Alternatively, it can also be simply:
[[8,0],
[2,0]]
#representing the sklearn implementation
[[Actual = 0, Predicted = 0, Actual = 0, Predicted = 1],
[Actual = 1, Predicted = 0, Actual = 1, Predicted = 1]]
# Ensuring that the diagonal is the correct class predictions counts.
Yes, that's a bug, which AFAICS causes problems when a row or column is zero.
The method just uses numpy histogram2d with bins=2, which produces the wrong bin limits if there are not both zeros and ones
I think using the threshold in the bin directly would make the 0-1 prediction unnecessary, but I don't remember how numpy histogram handles values exactly at the bounds (in case of ties and points on the threshold). b1 = np.array([ 0. , threshold, 1. ])
@josef-pkt
I can prepare a PR and send by next week. However, I think we can use sklearn's API for creating the confusion matrix which will make it more consistent for users coming form ML background. I think in Issue #1650 there was already discussion to some code from sklearn api for doing this.
Another option would be explicitly construct the Confusion matrix in the format such that the diagonal elements contain the true predictions for each class. In Logit case it would be just 2 classes such that 0 is the first class. The code would also be more readable as opposed to using np.histogram2d.
the problem is that any refactoring or enhancements will need decisions and review, while fixing the current implementation for the edge case with unit test can be merged right away.
I think histogram usage is quite fine, as long as it has explicit intervals. It's a common usage for counting a binned continuous variable.
Extensions along #1650 would be a good contribution, of course, but I'm not usually using this functionality and I'm not really familiar with what scikit-learn is doing in this area.
So, I don't have a strong opinion about details and would mainly review for overall consistency with the statsmodels pattern.
Overall predictive evaluation measures is not yet a strong, consistently supported area in statsmodels. I added the eval_measures for continuous data, but my usecase was more Monte Carlo studies than predictive cross-validation, plus some helper function for mainly internal usage.
This means, that it would be very useful if you or someone else is pushing into this area.
MNLogit has the same problem if a zero column is at the boundary. There is a unit test for it with zeros in the middle. Zero columns at the boundary mess up the bin edges for histogram2d.
Activity
josef-pkt commentedon May 25, 2016
@napsternxg Thanks for reporting
Yes, that's a bug, which AFAICS causes problems when a row or column is zero.
The method just uses numpy histogram2d with bins=2, which produces the wrong bin limits if there are not both zeros and ones
to fix this, I think it should be enough to use explicit bin boundaries
I think using the threshold in the bin directly would make the 0-1 prediction unnecessary, but I don't remember how numpy histogram handles values exactly at the bounds (in case of ties and points on the threshold).
b1 = np.array([ 0. , threshold, 1. ])josef-pkt commentedon May 25, 2016
Based on browsing the unit tests, test coverage for pred_table looks good. So, this problem might be only in this (untested) edge case.
@napsternxg Do you want to prepare a PR with the fix and your example as unit test?
napsternxg commentedon May 25, 2016
@josef-pkt
I can prepare a PR and send by next week. However, I think we can use sklearn's API for creating the confusion matrix which will make it more consistent for users coming form ML background. I think in Issue #1650 there was already discussion to some code from sklearn api for doing this.
Another option would be explicitly construct the Confusion matrix in the format such that the diagonal elements contain the true predictions for each class. In Logit case it would be just 2 classes such that 0 is the first class. The code would also be more readable as opposed to using np.histogram2d.
What do you think ?
josef-pkt commentedon May 25, 2016
the problem is that any refactoring or enhancements will need decisions and review, while fixing the current implementation for the edge case with unit test can be merged right away.
I think histogram usage is quite fine, as long as it has explicit intervals. It's a common usage for counting a binned continuous variable.
Extensions along #1650 would be a good contribution, of course, but I'm not usually using this functionality and I'm not really familiar with what scikit-learn is doing in this area.
So, I don't have a strong opinion about details and would mainly review for overall consistency with the statsmodels pattern.
Overall predictive evaluation measures is not yet a strong, consistently supported area in statsmodels. I added the
eval_measuresfor continuous data, but my usecase was more Monte Carlo studies than predictive cross-validation, plus some helper function for mainly internal usage.This means, that it would be very useful if you or someone else is pushing into this area.
BUG: fix binary pred_table for zeros see statsmodels#2968
josef-pkt commentedon Aug 17, 2016
MNLogit has the same problem if a zero column is at the boundary. There is a unit test for it with zeros in the middle. Zero columns at the boundary mess up the bin edges for histogram2d.
BUG: fix binary pred_table for zeros see statsmodels#2968
Merge pull request #3166 from josef-pkt/bug_discrete_pred_table
BUG: fix binary pred_table for zeros see statsmodels#2968
Merge pull request statsmodels#3166 from josef-pkt/bug_discrete_pred_…
add basic distributed_estimation notebook