Accuracy_Score in Sklearn
A crucial stage in the data science workflow is to measure our model's accuracy using the appropriate metric. In this tutorial, we'll learn two methods for calculating the source sample's predicted class accuracy: manually and using Python's scikit-learn library.
Here is a rundown of the topics we have discussed in this tutorial.
What is Accuracy?
One of the widely used metrics that computes the performance of classification models is accuracy. The percentage of labels that our model successfully predicted is represented by accuracy. For instance, if our model accurately classified 80 of 100 labels, its accuracy would be 0.80.
Creating Function to Compute Accuracy Score
Let's create a Python function to compute the predicted values accuracy score, given that we already have the sample's true labels and the labels predicted the model.
The above function accepts values for the classification model's predicted labels and true labels of the sample as its arguments and computes the accuracy score. Here, we iterate through each pair of true and predicted labels in parallel to record the number of correct predictions. We then divide that number by the total number of labels to compute the accuracy score.
We will apply the function on a sample now.
We get 0.978 as the accuracy score for the Support Vector Classification model's predictions.
Note that using numpy arrays to vectorize the equality computation can make the code mentioned above more efficient.
Accuracy using Sklearn's accuracy_score()
The accuracy_score() method of sklearn.metrics, accept the true labels of the sample and the labels predicted by the model as its parameters and computes the accuracy score as a float value, which can likewise be used to obtain the accuracy score in Python. There are several helpful functions to compute typical evaluation metrics in the sklearn.metrics class. Let's use sklearn's accuracy_score() function to compute the Support Vector Classification model's accuracy score using the same sample dataset as earlier.
sklearn.metrics.accuracy_score(y_true, y_pred, *, normalize=True, sample_weight=None)
We use this for computing the accuracy score of classification. This method calculates subgroup accuracy in multi-label classification; a dataset's predicted subset of labels must precisely match the actual dataset of labels in y_true.
Example of Accuracy_score
When using binary label indicators with multiple labels:
How scikit learn accuracy_score works
The accuracy_score method of the sklearn.metrics package assigns subset accuracy in multi-label classification.
It is required that the labels the model has predicted for the given sample and the true labels of the sample match exactly.
Accuracy describes the model's behaviour across all classes. If all of the classes are comparably significant, it is helpful.
The ratio of the count of accurate predictions to the total number of samples or the total number of predictions is used to determine the model's accuracy.
Confusion Matrix: [[2 2] [3 0]] 0.2857142857142857
So, in this tutorial, we learnt scikit-learn accuracy_score in Python and examined some implementation examples.
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week