# MAPE – Mean Absolute Percentage Error in Python Hello, readers! In our series of Error Metrics, we have understood and implemented Root Mean Square Error.

Today, we will be focusing on another important error metric in model building — Mean Absolute Percentage Error (MAPE) in Python.

## What is MAPE?

Mean Absolute Percentage Error (MAPE) is a statistical measure to define the accuracy of a machine learning algorithm on a particular dataset.

MAPE can be considered as a loss function to define the error termed by the model evaluation. Using MAPE, we can estimate the accuracy in terms of the differences in the actual v/s estimated values.

Let us have a look at the below interpretation of Mean Absolute Percentage Error–

As seen above, in MAPE, we initially calculate the absolute difference between the Actual Value (A) and the Estimated/Forecast value (F). Further, we apply the mean function on the result to get the MAPE value.

MAPE can also be expressed in terms of percentage. Lower the MAPE, better fit is the model.

## Mean Absolute Percentage Error with NumPy module

Let us now implement MAPE using Python NumPy module.

At first, we have imported the dataset into the environment. You can find the dataset here.

Further, we have split the dataset into training and testing datasets using the Python train_test_split() function.

Then, we have defined a function to implement MAPE as follows–

• Calculate the difference between the actual and the predicted values.
• Then, use `numpy.abs() function` to find the absolute value of the above differences.
• Finally, apply `numpy.mean() function `to get the MAPE.

Example:

```import numpy as np
from sklearn.model_selection import train_test_split
import pandas as pd

#Separating the dependent and independent data variables into two data frames.
X = bike.drop(['cnt'],axis=1)
Y = bike['cnt']

# Splitting the dataset into 80% training data and 20% testing data.
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=.20, random_state=0)

#Defining MAPE function
def MAPE(Y_actual,Y_Predicted):
mape = np.mean(np.abs((Y_actual - Y_Predicted)/Y_actual))*100
return mape
```

Now, we have implemented a Linear Regression to check the error rate of the model using MAPE.

Here, we have made use of `LinearRegression() function` to apply linear regression on the dataset. Further, we have used the predict() function to predict the values for the testing dataset.

At last, we have called the MAPE() function created above to estimate the error value in the predictions as shown below:

```#Building the Linear Regression Model
from sklearn.linear_model import LinearRegression
linear_model = LinearRegression().fit(X_train , Y_train)

#Predictions on Testing data
LR_Test_predict = linear_model.predict(X_test)

# Using MAPE error metrics to check for the error rate and accuracy level
LR_MAPE= MAPE(Y_test,LR_Test_predict)
print("MAPE: ",LR_MAPE)
```

Output:

```MAPE:  16.628873360270358
```

## Mean Absolute Percentage Error with Python scikit learn library

In this example, we have implemented the concept of MAPE using Python sklearn library.

Python sklearn library offers us with `mean_absolute_error() function` to calculate the MAPE value as shown below–

Example:

```from sklearn.metrics import mean_absolute_error
Y_actual = [1,2,3,4,5]
Y_Predicted = [1,2.5,3,4.1,4.9]
mape = mean_absolute_error(Y_actual, Y_Predicted)*100
print(mape)
```

Output:

```13.999999999999984
```

## Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Python, Stay tuned here and till then, Happy Learning!! 🙂