6

Absolute Error

Unsolved
Prob. and Stats

Difficulty: 2 | Problem written by mesakarghm
Problem reported in interviews at

Amazon
Apple
Facebook
Google
Netflix

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:
<class 'list'>
arr1: [1, 2, 3]
<class 'list'>
arr2: [3, 4, 5]

Expected Output:
<class 'float'>
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
Jump to comment-56
uahnbu • 8 months, 1 week ago

1

  • 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))

 

Jump to comment-65
admin • 8 months ago

0

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

Jump to comment-62
tien • 8 months ago

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 ? 

Jump to comment-63
trungle98hn@gmail.com • 8 months ago

0

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

Jump to comment-68
admin • 7 months, 4 weeks ago

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!

Jump to comment-67
admin • 7 months, 4 weeks ago

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!

Jump to comment-95
shandytp • 7 months ago

0

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

Jump to comment-140
abhishek_kumar • 3 months, 1 week ago

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:

1. Absolute difference of two NumPy arrays

2. What is Mean Absolute Error (MAE)?
 

Jump to comment-192
harish9 • 1 month, 2 weeks ago

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.

Jump to comment-195
gideon_fadele • 1 month, 2 weeks ago

0

Have you tried a case where the difference between the input is negative
Jump to comment-196
harish9 • 1 month, 2 weeks ago

0

I just ran it without making any changes and it passed.
Ready.

Input Test Case

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