6

# Absolute Error

Unsolved###### Prob. and Stats

##### Problem reported in interviews at

If x is the actual value of a quantity and x0 is the predicted value, then the absolute error can be calculated using the formula:

\(\delta x = x_0 - x\)

For multiple predictions, the arithmetic mean of absolute errors of individual measurements should be the final absolute error.

\(\delta x = {\sum |x_0 - x| \over n}\)

x0 is the predicted value

x is the actual value

and n is the length of array

For a given set of arrays (two numeric lists of same length with actual value as the first list and predicted value as the second list), calculate and return the absolute error.

##### Sample Input:

`arr1: [1, 2, 3]`

<class 'list'>

`arr2: [3, 4, 5]`

##### Expected Output:

`2.0`

This is a premium problem, to view more details of this problem please sign up for MLPro Premium. MLPro premium offers access to actual machine learning and data science interview questions and coding challenges commonly asked at tech companies all over the world

MLPro Premium also allows you to access all our high quality MCQs which are not available on the free tier.

Not able to solve a problem? MLPro premium brings you access to solutions for all problems available on MLPro

Get access to Premium only exclusive educational content available to only Premium users.

Have an issue, the MLPro support team is available 24X7 to Premium users.

##### This is a premium feature.

To access this and other such features, click on upgrade below.

Log in to post a comment

Comments- Using list operators:

`return (sum(arr2) - sum(arr1)) / len(arr2)`

- Using numpy:

**Note:** You must manually import *np* although it says that *np *has been imported, as *np *is only automatically imported in tests.

`return np.average(np.subtract(arr2, arr1))`

##
admin • 8 months ago
**0**

**0**

Thank you for that! We have added an automatic import statement to this problem.

##
tien • 8 months ago
**1**

**1**

def abs_error(arr1,arr2):

return np.average(np.abs(np.subtract(arr1, arr2)))

It said the code passed 3/4 tests. What is the fourth one ?

##
trungle98hn@gmail.com • 8 months ago
**0**

**0**

I got same error when using numpy and traditional for loop :(

##
admin • 7 months, 4 weeks ago
**0**

**0**

Thank you for letting us know. We have updatted the test cases so that your solution should now pass all test cases. Please feel free to reach out if there is anything else that shows up!

##
admin • 7 months, 4 weeks ago
**0**

**0**

Thank you for letting us know. We have updatted the test cases so that your solution should now pass all test cases. Please feel free to reach out if there is anything else that shows up!

##
shandytp • 7 months ago
**0**

**0**

I still got the same error message, only 3/4 cases passed

##
abhishek_kumar • 3 months, 1 week ago
**0**

**0**

```
import numpy as np
def abs_error(arr1,arr2):
abs_diff = np.absolute(np.asarray(arr1) - np.asarray(arr2))
result = sum(abs_diff)/len(arr1)
return result
```

Steps:

1. Get the Absolute difference

2. Compute the average

3. return the average

REFERENCE:

##
harish9 • 1 month, 2 weeks ago
**0**

**0**

I am getting this message "Test case failure: 3 out of 4 cases passed.". What is the fourth case? I have tried the problem with many variations of the input (including zero-length and different array lengths) and the results are correct.

##
gideon_fadele • 1 month, 2 weeks ago
**0**

**0**

##
harish9 • 1 month, 2 weeks ago
**0**

**0**

Input Test Case

Please enter only one test case at a timenumpy has been already imported as np (import numpy as np)

1