Why write custom functions for ML metrics

Kasinath Reddy
2 min readMar 22, 2021


Writing custom functions will be useful if the use case would be of same kind and you would like to use it in your workflow let’s consider any machine learning model related to binary -classification problem

In order to check the performance of the classification model we will extract confusion matrix and check its performance by seeing specific metrics like sensitivity, specificity and accuracy accordingly to business perspective

In order to extract sensitivity and specificity and accuracy standard approach would be generate confusion matrix and import sklearn.metrics and call respective functions

Let’s just say I want to check accuracy, sensitivity and specificity and I have like 4 models then in order to extract the metrics its the same repetitive code to call respective functions so in cases like this having custom functions comes handy

Below code would be a simple version of such function

So what above class needs to initialize it is a confusion matrix let’s see a basic demo of it

All we need to do is create object from CM_METRICS class and you can call its respective function if it exists in class like cm.get_accuracy() or if you just like to see the output printed for all metrics listed in the class you can go for cm.print_all_metrics() and this reduces lot of time and you really don’t need to copy-paste commands a lot in it

This code is just demo of how we can use custom stuff so its upto the individual to write in a way which can be simple as well as time saving

Happy learning :)