My Coding >
Programming language >
Python >
Python FAQ >
Python: How to calculate MAPE – Mean Absolute Percentage Error
Python: How to calculate MAPE – Mean Absolute Percentage ErrorMean absolute percentage error (MAPE) is a metric to calculate the distance between two datasets. This metric often used, when it is necessary to estimate the distance (or cost) between predicted and target values of some functions. MAPE = (100%/n)*Σ^{n}_{i=1}((y_{i}y’_{i})/y_{i}) where y is our target value and y'  our predicted value. This function have few disadvantages. First of all, if or target value is equal to 0, then we will have division to 0, which is not very easy to do. Second disadvantage – is percent value. In some calculations, it is very easy to forget that this is per cent values and make a mistake. Also different function can calculate this value with or without per cents. So it is necessary to be very accurate. Test data setLet’s make a simple dataset for calculation MAE value
and we can transfer these data to python numpy array
Calculating MAPE with pure numpyWe will implement the above equation with Numpy mathematical operations
And the final code at one go
Sklearn MAPE functionSclean library (Scikitlearn is a free software machine learning library for the Python) have MAPE procedure within its tools. But it is necessary to pay attention – it calculate Mean absolute percentage error without applying per cents, which is necessary to remember.

Last 10 artitles
9 popular artitles


© 2020 MyCoding.uk My blog about coding and further learning. This blog was writen with pure Perl and frontend output was performed with TemplateToolkit. 