Search code examples
pythonscikit-learnimporterror

Sklearn, ImportError : top_k_accuracy_score


I had no issues while importing any of the other metrics (such as accuracy_score, classification_report) but top_k_accuracy_score triggers a import error. I ran my code on Google Colab.

Code snippet :

from sklearn.metrics import accuracy_score, classification_report, top_k_accuracy_score

print(top_k_accuracy_score(cat_y_test, preds, k = 3))

Error Message :

ImportError: cannot import name 'top_k_accuracy_score' from 'sklearn.metrics' (/usr/local/lib/python3.7/dist-packages/sklearn/metrics/__init__.py)

I get the same error while running the example they'd provided in their docs as well.

Thanks in Advance.


Solution

  • I got same error when using scikit-learn 0.21.2.

    Try to update your scikit-learn to latest version (0.24.2 for August 2021) by invoking

    pip install --upgrade scikit-learn
    

    Or by using this command on google collab :

    !pip install --upgrade scikit-learn
    

    It resolve the error in my case.