In machine learning, accuracy is one of the most important performance evaluation metrics for a classification model. The mathematical formula for calculating the accuracy of a machine learning model is 1 – [Number of misclassified samples / Total number of samples]. If you want to learn how to evaluate the performance of a machine learning model by calculating its accuracy, this article is for you. In this article, I’ll give you an introduction to accuracy in machine learning and its calculation using Python.
Introduction to Accuracy in Machine Learning
Accuracy means the state of being correct or precise. For example, think of a group of friends who guessed the release of the next part of Avengers, and whoever guessed the date which is either the exact release date or closest to the release date is the most accurate one. So, the degree of being closer to a specific value is nothing but accuracy. In machine learning, it is one of the most important and widely used performance evaluation metrics for classification. If you’ve never used it before, below is a comprehensive tutorial on the calculation of accuracy in machine learning using Python.
For the calculation of the accuracy of a classification model, we must first train a model for any classification-based problem. So here’s how we can easily train a classification-based machine learning model:
Now here is how we can calculate the accuracy of our trained model:
print[accuracy_score[ytest, model.predict[xtest]]]
0.99
Many people often confuse accuracy and precision[another classification metric] with each other, accuracy is how close the predicted values are to the expected value, while precision is how close the predicted values are with each other.
Also, Read – Solving Data Science Case Studies with Python [eBook]
Summary
So this is how you can easily calculate the accuracy of a machine learning model based on the classification problem. This is one of the most important performance evaluation metrics for classification in machine learning. The mathematical formula for calculating the accuracy of a machine learning model is 1 – [Number of misclassified samples / Total number of samples]. Hope you liked this article on an introduction to accuracy in machine learning and its calculation using Python. Please feel free to ask your valuable questions in the comments section below.
Recipe Objective
After training a model we need a measure to check its performance, their are many scoring metric on which we can score the model"s performance. Out of many metric we will be using accuracy to measure our models performance. We will also be using cross validation to test the model on multiple sets of data.
So this is the recipe on How we can check model"s accuracy using cross validation in Python
Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects
Table of Contents
- Recipe Objective
- Step 1 - Import the library
- Step 2 - Setting up the Data
- Step 3 - Model and its accuracy
Step 1 - Import the library
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
We have imported various modules from differnt libraries such as cross_val_score, DecisionTreeClassifier and datasets.
Step 2 - Setting up the Data
We have used an inbuilt Wine dataset. We have stored data in X and target in y.
Wine = datasets.load_wine[]
X = Wine.data
y = Wine.target
Step 3 - Model and its accuracy
We are using DecisionTreeClassifier as a model to train the data. We are training the model with cross_validation which will train
the data on different training set and it will calculate accuracy for all the test train split. We are printing the accuracy for all the splits in cross validation. We have passed model, data, target and cv an parameters. cv signifies the number of splits we want while performing cross validation. We are also printing mean and standard deviation of average precision.
dtree = DecisionTreeClassifier[]
print[]; print[cross_val_score[dtree, X, y, scoring="accuracy", cv = 7]]
mean_score = cross_val_score[dtree, X, y, scoring="accuracy", cv = 7].mean[]
std_score = cross_val_score[dtree, X, y, scoring="accuracy", cv = 7].std[]
print[mean_score]
print[std_score]
So the output comes as
[0.92592593 0.84615385 0.80769231 0.88 0.88 0.88
0.79166667]
0.8647110297110298
0.05775178587574378