How To Create/Customize Your Own Scorer Function In Scikit-Learn?
Last Updated :
28 Apr, 2025
A well-known Python machine learning toolkit called Scikit-learn provides a variety of machine learning tools and methods to assist programmers in creating sophisticated machine learning models. A strong framework for assessing the effectiveness of these models using a variety of metrics and scoring functions is also offered by Scikit-learn. To assess the effectiveness of their models, users might want to design their scoring function in specific circumstances. Scikit-learn makes this possible, and in this article, we'll go over how to design and tweak your very own scoring function.
A scikit-learn function called a scorer accepts two arguments: the ground truth (actual values) and the model's predicted values. A single score that evaluates the accuracy of the anticipated values is returned by the function. Accuracy, precision, recall, F1-score, and other predefined scoring functions are available in Scikit-learn. To assess the effectiveness of their models, users might want to develop their unique scoring system.
Custom scorer for a multi-class Regression problem
To create a custom scorer function in sci-kit-learn, we need to follow some steps:
Step 1: Create a custom function that evaluates the accuracy
create a Python function that accepts two arguments: the model's predicted values and the ground truth (actual values). A single score that evaluates the accuracy of the anticipated values should be returned by the function.
Here I am defining the coefficient of determination (R2)
The coefficient of determination (R²) is a statistical measure that represents how well a statistical model predicts an outcome. It measures the proportion of variance in the predicted output that is explained by the independent input variable(s) in a regression model.
R^2 = 1- \frac{RSS}{TSS}
Here,
- RSS = Sum of Squared error also known as Residual sum of squares (RSS) measures the variation that is not explained by the regression model. It is the sum of squared differences between the predicted values and the actual target values.
RSS = \sum(pred-actual)^2
- TSS = total sum of squares (TSS) represents the total variation in the dependent variable. It is the sum of squared differences between the actual values and the mean of the dependent variable
TSS = \sum (actual-mean)^2
The value of R² ranges from 0 to 1, with higher values indicating a better fit. A value of 0 indicates that the regression line does not fit the data at all, while a value of 1 indicates a perfect fit.
Python3
import numpy as np
def r_squared(y_true, y_pred):
# Calculate the mean of the true values
mean_y_true = np.mean(y_true)
# Calculate the sum of squares of residuals and total sum of squares
ss_res = np.sum((y_true - y_pred) ** 2)
ss_tot = np.sum((y_true - mean_y_true) ** 2)
# Calculate R²
r2 = 1 - (ss_res / ss_tot)
return r2
Step 2:Create a scorer object:
Once the scoring function has been constructed, a scorer object must be created using the sci-kit-learn make_scorer() function. The scoring function is passed as an argument to the make_scorer() function, which returns a scorer object.
Python3
from sklearn.metrics import make_scorer
# Create a scorer object using the r_squared function
r2_score = make_scorer(r2_squared)
r2_score
Output:
make_scorer(r2_squared)
Step 3: Implementations of the above-defined scorer object
After creating the scorer object, we can use it to access a machine learning model's performance using the cross-validation functions for different subsets of datasets provided by scikit-learn or other model assessment tools.
Python3
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import cross_val_score
# Load the California Housing Price dataset
X, y = fetch_california_housing(return_X_y=True)
# Create a Random Forest regression model
model = RandomForestRegressor()
# Evaluate the performance of the model u
# sing cross-validation with the r2_squared function
scores = cross_val_score(model,
X, y,
cv=5,
scoring=r2_score)
# Print the mean and standard deviation of the scores
print(f"R2 Squared: {scores.mean():.2f} +/- {scores.std():.2f}")
Output:
R2 Squared: 0.65 +/- 0.08
Custom scorer for a multi-class classification problem
Steps:
- Import the necessary libraries
- Load the iris dataset
- Define multiple metrics like accuracy_score, precision_score, recall_score, f1_score with make_scorer.
- Create a XGBClassifier model
- Evaluate the model using cross-validation and the custom scorer
- Print the mean scores for each metric
Python
from sklearn.metrics import make_scorer, accuracy_score
from sklearn.metrics import precision_score, recall_score, f1_score
from sklearn.model_selection import cross_validate
from sklearn.datasets import load_iris
from xgboost import XGBClassifier
# Load the iris dataset
iris = load_iris()
# Define multiple metrics
scoring = {'accuracy': make_scorer(accuracy_score),
'precision': make_scorer(precision_score, average='macro'),
'recall': make_scorer(recall_score, average='macro'),
'f1-score': make_scorer(f1_score, average='macro')
}
# Create a XGBClassifier
clf = XGBClassifier(n_estimators=2,
max_depth=3,
learning_rate=0.1)
# Evaluate the model using cross-validation and the custom scorer
scores = cross_validate(clf, iris.data, iris.target, cv=5, scoring=scoring)
# Print the mean scores for each metric
print("Accuracy mean score:", scores['test_accuracy'].mean())
print("Precision mean score:", scores['test_precision'].mean())
print("Recall mean score:", scores['test_recall'].mean())
print("f1-score:", scores['test_f1-score'].mean())
Output:
Accuracy mean score: 0.9666666666666668
Precision mean score: 0.9707070707070707
Recall mean score: 0.9666666666666668
f1-score: 0.9664818612187034
Similar Reads
How to Create a Custom Loss Function in Keras Creating a custom loss function in Keras is crucial for optimizing deep learning models. The article aims to learn how to create a custom loss function. Need to create Custom Loss Functions Loss function is considered as a fundamental component of deep learning as it is helpful in error minimization
3 min read
How to create a custom Loss Function in PyTorch? Choosing the appropriate loss function is crucial in deep learning. It serves as a guide for directing the optimization process of neural networks while they are being trained. Although PyTorch offers many pre-defined loss functions, there are cases where regular loss functions are not enough. In th
3 min read
How to create custom distance function with multiple arguments for sklearn When working with machine learning algorithms, particularly those involving clustering or nearest neighbours, the choice of distance metric can greatly influence the performance and outcomes. While scikit-learn provides several built-in distance metrics, there might be situations where you need a cu
3 min read
Creating Custom Cross-Validation Generators in Scikit-learn Cross-validation is a fundamental technique in machine learning used to assess the performance and generalizability of models. Scikit-learn, a popular Python library, provides several built-in cross-validation methods, such as K-Fold, Stratified K-Fold, and Time Series Split. However, there are scen
6 min read
Map Data to a Normal Distribution in Scikit Learn A Normal Distribution, also known as a Gaussian distribution, is a continuous probability distribution that is symmetrical around its mean. It is defined by its norm, which is the center of the distribution, and its standard deviation, which is a measure of the spread of the distribution. The normal
5 min read
How to create a function in MATLAB ? A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: fun
2 min read
Custom Loss Function in R Keras In deep learning, loss functions guides the training process by quantifying how far the predicted values are from the actual target values. While Keras provides several standard loss functions like mean_squared_error or categorical_crossentropy, sometimes the problem you're working on requires a cus
3 min read
How to calculate the F1 score and other custom metrics in PyTorch? Evaluating deep learning models goes beyond just training them; it means rigorously checking their performance to ensure they're accurate, reliable, and efficient for real-world use. This evaluation is critical because it tells us how well a model has learned and how effective it might be in real-li
7 min read
How to Generate Feature Importance Plots from Scikit-Learn? Understanding which factors affect predictions in machine learning models is vital for making them more accurate and reliable. Feature importance plots are tools that help us see and rank these factors visually, which makes it simpler to understand and improve our models. Here we create these plots
3 min read
Difference between score() and accuracy_score() methods in scikit-learn 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
4 min read