Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Scikit learn Scikit learn Accuracy Score

From Leeroopedia


Field Value
source scikit-learn|https://github.com/scikit-learn/scikit-learn
domains Data_Science, Machine_Learning
last_updated 2026-02-08 15:00 GMT

Overview

Concrete tool for computing classification accuracy provided by scikit-learn.

Description

The accuracy_score function computes the fraction (or count) of predictions that exactly match the ground truth labels. For standard single-label classification, it returns the proportion of correct predictions. For multilabel classification, it computes subset accuracy: a sample is counted as correct only if all of its predicted labels exactly match the true label set.

Usage

  • Computing the overall correctness rate of a classifier on test data.
  • Comparing multiple models on the same evaluation set using a simple, interpretable metric.
  • Serving as a quick sanity check during model development.

Code Reference

Source Location

sklearn/metrics/_classification.py, function accuracy_score

Signature

def accuracy_score(y_true, y_pred, *, normalize=True, sample_weight=None):

Import

from sklearn.metrics import accuracy_score

I/O Contract

Inputs

Parameter Type Default Description
y_true 1d array-like, or label indicator array / sparse matrix (required) Ground truth (correct) labels.
y_pred 1d array-like, or label indicator array / sparse matrix (required) Predicted labels, as returned by a classifier.
normalize bool True If True, return the fraction of correctly classified samples. If False, return the number of correctly classified samples.
sample_weight array-like of shape (n_samples,) or None None Sample weights. If provided, the accuracy is computed as a weighted average.

Outputs

Return Type Description
score float When normalize=True, the fraction of correctly classified samples (best value is 1.0). When normalize=False, the number of correctly classified samples.

Companion APIs

In addition to accuracy_score, scikit-learn provides two closely related metric functions that offer richer diagnostic information:

classification_report

from sklearn.metrics import classification_report

def classification_report(
    y_true,
    y_pred,
    *,
    labels=None,
    target_names=None,
    sample_weight=None,
    digits=2,
    output_dict=False,
    zero_division="warn",
):

Builds a text report showing per-class precision, recall, F1-score, and support (number of true instances per class), as well as macro, weighted, and micro averages. Pass output_dict=True to receive the report as a dictionary instead of a string. This function is located in sklearn/metrics/_classification.py.

confusion_matrix

from sklearn.metrics import confusion_matrix

def confusion_matrix(
    y_true, y_pred, *, labels=None, sample_weight=None, normalize=None
):

Computes a confusion matrix C where entry Ci,j is the number of observations known to be in class i but predicted as class j. Supports optional normalization over true labels ("true"), predicted labels ("pred"), or the entire population ("all"). This function is located in sklearn/metrics/_classification.py.

Usage Examples

Basic accuracy computation:

from sklearn.metrics import accuracy_score

y_true = [0, 1, 2, 2, 1]
y_pred = [0, 1, 2, 1, 1]
print(accuracy_score(y_true, y_pred))  # 0.8

Returning the count of correct predictions:

from sklearn.metrics import accuracy_score

y_true = [0, 1, 2, 2, 1]
y_pred = [0, 1, 2, 1, 1]
print(accuracy_score(y_true, y_pred, normalize=False))  # 4

Full evaluation workflow with companion APIs:

from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42
)

clf = LogisticRegression(max_iter=200, random_state=42)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)

# Overall accuracy
print("Accuracy:", accuracy_score(y_test, y_pred))

# Per-class precision, recall, F1
print(classification_report(y_test, y_pred, target_names=["setosa", "versicolor", "virginica"]))

# Confusion matrix
print(confusion_matrix(y_test, y_pred))

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment