Open In App

Difference between score() and accuracy_score() methods in scikit-learn

Last Updated : 19 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The score( ) method and accuracy_score( ) function are both essential tools in evaluating machine learning models, especially in supervised learning tasks. While they both assess model performance in terms of accuracy, they differ in terms of usage, flexibility, and application. Understanding these differences is crucial for effectively evaluating and comparing machine learning models.

Score Vs Accuracy Score

Aspect

‘score’ method

‘accuracy_score’ function

Usage

This method can be called on the model object, score method refers to model object. For example, in sci-kit-learn after creating a model like ‘ Logistic Regression( ) ‘ , you have an object ‘model’ that represents this specific trained logistic regression model.

The ‘accuracy_score( ) ‘ function is a standalone function from the metrics module. This function does not need to be tied to a specific object or class.

Functionality

This method evaluates the model on test data.

This method compares predicted values with the true values.

Arguments

Takes test data directly

Takes predicted labels and true labels as arguments.

Flexibility

This method is less flexible as compare to accuracy_score function

This method is more flexible .

Performance comparison

Useful for quick model evaluation

Useful for comparing multiple models.

Implementation: score() method vs accuracy_score() function in python

Below is an example of implementation demonstrating the difference between two evaluation methods using a simple logistic regression model in scikit-learn –

Step 1: Importing the necessary libraries

We will import necessary modules for logistic regression, accuracy evaluation, iris dataset loading, and train-test splitting, facilitating the development and assessment of a logistic regression model for classification tasks.

Python3
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split


Step 2 : Loading the iris dataset

Once ,we have loaded the necessary libraries, we will be loading Iris Dataset into variables “X” and “y” representing the feature matrix and target labels, respectively.

Python3
# Load dataset
iris = load_iris()
X, y = iris.data, iris.target


Step 3 : Splitting the dataset into training and testing sets

This code splits the dataset into training and testing sets, with 80% of the data reserved for training (X_train, y_train) and 20% for testing (X_test, y_test), ensuring consistency in random splitting with a specified seed value (random_state=42).

Python3
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)


Step 4 : Create and Train a logistic Regression model and train on training dataset

We create a logistic regression model instance and trains it on the training data (X_train, y_train) to learn the relationships between the features and target labels.

Python3
# Create and train a model
model = LogisticRegression()
model.fit(X_train, y_train)

Step 5 : Model Evaluation

score() method

Then, we evaluate the trained logistic regression model’s performance on the test data (X_test, y_test) using the score() method, which computes the accuracy of the model’s predictions compared to the actual target labels. It then prints out the accuracy score.

Python3
# Method 1: Using score method
score = model.score(X_test, y_test)
print(f"Score method - Accuracy: {score}")

Output :

Score method - Accuracy: 1.0

accuracy_score()

The provided code generates predictions (y_pred) from the trained logistic regression model on the test data (X_test). Then, it computes the accuracy of the model’s predictions compared to the actual target labels (y_test) using the accuracy_score function, which measures the proportion of correctly predicted labels. Finally, it prints out the accuracy score obtained from the accuracy_score function.

Python3
# Get predictions from the model
y_pred = model.predict(X_test)
# Method 2: Using accuracy_score function
accuracy = accuracy_score(y_test, y_pred)
print(f"accuracy_score function - Accuracy: {accuracy}")

Output :

accuracy_score function - Accuracy: 1.0




In this implementation, we train a logistic regression model on the Iris dataset and then evaluate its accuracy using both the score() method and accuracy_score() function. The output will demonstrate the same accuracy value obtained using both approaches, showcasing their equivalence in evaluating model accuracy.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads