Skip to content

Conversation

narendrasinghdangi
Copy link
Contributor

Reference Issues/PRs #26965 is Fixed

What does this implement/fix? Explain your changes.

Case when y_true contains a single class and y_true == y_pred.

labels = unique_labels(y_true, y_pred)
It calculate number of unique value in the given y_test and y_pred
n_label=label.size

so i have create a condition that:-
if n_labels==1:
return coo_matrix((sample_weight, (y_true, y_pred)),shape=(2, 2),dtype=dtype,).toarray()

example 👍
y_true = [1,1,1,1]
y_pred = [1,1,1,1]
before it shows [[4]]
but now it shows [[4,0],[0,0]]

SO this issue now fixed

y_true = np.array([0, 0])
y_pred = np.array([0, 0])
print(f1_score(y_true, y_pred, zero_division=1)) # Here division by zero should be triggered resulting in 1.0
but now confusion Matrix =[[2,0],[0,0]] so now the precision , recall and f1_score will come without and trigged ed
So this issue is also solved.

And other Issue in class_likelihood_ratios :---

LR+ ranges from 1 to infinity. A LR+ of 1 indicates that the probability of predicting the positive class is the same for samples belonging to either class; therefore, the test is useless. The greater LR+ is, the more a positive prediction is likely to be a true positive when compared with the pre-test probability. A value of LR+ lower than 1 is invalid as it would indicate that the odds of a sample being a true positive decrease with respect to the pre-test odds.

LR- ranges from 0 to 1. The closer it is to 0, the lower the probability of a given sample to be a false negative. A LR- of 1 means the test is useless because the odds of having the condition did not change after the test. A value of LR- greater than 1 invalidates the classifier as it indicates an increase in the odds of a sample belonging to the positive class after being classified as negative. This is the case when the classifier systematically predicts the opposite of the true label.

This issue is also Fixed :--

Firstly calculate the number of unique_label if it come 1 then create a condition to solve it.

if labels is None:
labels = unique_labels(y_true, y_pred)
else:
labels = np.asarray(labels)
n_labels = labels.size
if n_labels == 0:
raise ValueError("'labels' should contains at least one label.")
elif y_true.size == 0:
return np.zeros((n_labels, n_labels), dtype=int)
elif len(np.intersect1d(y_true, labels)) == 0:
raise ValueError("At least one label specified must be in y_true")
n_labels = labels.size

if (n_labels,n_labels)==(1,1):
positive_likelihood_ratio=float("inf")
negative_likelihood_ratio=0

One test case is removed because it check the previous error but now it is solved

Any other comments?

So all The issue related to precision, recall , F1_score, confusion_matrix , class_likelihood_ratios is Solved
Case when y_test contains a single class and y_test == y_pred.

Case when `y_test` contains a single class and `y_test == y_pred`. is Solved
 Case when `y_test` contains a single class and `y_test == y_pred`.
Test Case Solved
@github-actions
Copy link

github-actions bot commented Aug 12, 2023

❌ Linting issues

This PR is introducing linting issues. Here's a summary of the issues. Note that you can avoid having linting issues by enabling pre-commit hooks. Instructions to enable them can be found here.

You can see the details of the linting issues under the lint job here


black

black detected issues. Please run black . locally and push the changes. Here you can see the detected issues. Note that running black might also fix some of the issues which might be detected by ruff. Note that the installed black version is black=23.3.0.


--- /home/runner/work/scikit-learn/scikit-learn/sklearn/metrics/_classification.py	2023-08-12 13:38:06.028768 +0000
+++ /home/runner/work/scikit-learn/scikit-learn/sklearn/metrics/_classification.py	2023-08-12 13:38:32.610812 +0000
@@ -365,15 +365,16 @@
     if sample_weight.dtype.kind in {"i", "u", "b"}:
         dtype = np.int64
     else:
         dtype = np.float64
 
-    if n_labels==1:
-        return coo_matrix((sample_weight, (y_true, y_pred)),
-                shape=(2, 2),
-                dtype=dtype,
-            ).toarray()
+    if n_labels == 1:
+        return coo_matrix(
+            (sample_weight, (y_true, y_pred)),
+            shape=(2, 2),
+            dtype=dtype,
+        ).toarray()
 
     cm = coo_matrix(
         (sample_weight, (y_true, y_pred)),
         shape=(n_labels, n_labels),
         dtype=dtype,
@@ -1934,13 +1935,13 @@
     n_labels = labels.size
 
     # Case when `y_test` contains a single class and `y_test == y_pred`. (is solved Now)
     # This may happen when cross-validating imbalanced data and should
     # not be interpreted as a perfect score.
-    if (n_labels,n_labels)==(1,1):
-        positive_likelihood_ratio=float("inf")
-        negative_likelihood_ratio=0
+    if (n_labels, n_labels) == (1, 1):
+        positive_likelihood_ratio = float("inf")
+        negative_likelihood_ratio = 0
     else:
         tn, fp, fn, tp = cm.ravel()
         support_pos = tp + fn
         support_neg = tn + fp
         pos_num = tp * support_neg
would reformat /home/runner/work/scikit-learn/scikit-learn/sklearn/metrics/_classification.py
--- /home/runner/work/scikit-learn/scikit-learn/sklearn/metrics/tests/test_classification.py	2023-08-12 13:38:06.032768 +0000
+++ /home/runner/work/scikit-learn/scikit-learn/sklearn/metrics/tests/test_classification.py	2023-08-12 13:38:35.236551 +0000
@@ -630,12 +630,11 @@
 
 @pytest.mark.parametrize(
     "params, warn_msg",
     [
         # When y_test contains one class only and y_test==y_pred, LR+ is undefined
-        #Solved Now and removed This test Case.
-        
+        # Solved Now and removed This test Case.
         # When `fp == 0` and `tp != 0`, LR+ is undefined
         (
             {
                 "y_true": np.array([1, 1, 1, 0, 0, 0]),
                 "y_pred": np.array([1, 1, 1, 0, 0, 0]),
would reformat /home/runner/work/scikit-learn/scikit-learn/sklearn/metrics/tests/test_classification.py

Oh no! 💥 💔 💥
2 files would be reformatted, 907 files would be left unchanged.

ruff

ruff detected issues. Please run ruff --fix --show-source . locally, fix the remaining issues, and push the changes. Here you can see the detected issues. Note that the installed ruff version is ruff=0.0.284.


sklearn/metrics/tests/test_classification.py:636:1: W293 [*] Blank line contains whitespace
    |
634 |         # When y_test contains one class only and y_test==y_pred, LR+ is undefined
635 |         #Solved Now and removed This test Case.
636 |         
    | ^^^^^^^^ W293
637 |         # When `fp == 0` and `tp != 0`, LR+ is undefined
638 |         (
    |
    = help: Remove whitespace from blank line

Found 1 error.
[*] 1 potentially fixable with the --fix option.

Generated for commit: 6cc5c5e. Link to the linter CI: here

@narendrasinghdangi
Copy link
Contributor Author

all the issue related to precision, recall , F1_score, confusion_matrix , class_likelihood_ratios is Solved
Case when y_test contains a single class and y_test == y_pred.

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

Successfully merging this pull request may close these issues.

1 participant