Coding

Could you code a function to calculate the root mean squared error between two arrays?

Data ScientistMachine Learning Engineer

TikTok

Deloitte

Calm

Mixpanel

Zoom

Instagram

Did you come across this question in an interview?

Answers

Expert Answer

Anonymous

4.2Exceptional
let's consider two vectors y and y_bis, the root mean square error between these two vectors is equal to the L2-norm between y and y_bis. in another words, let's consider z = y - y_bis. the root mean squared error corresponds to the square root of the sum square element of z.
I will code it in Python : 

first thing to have in mind is to code the sqrt function to compute square root of a number. for this purpose we use newton method.

def sqrt(x: float, epsilon: float = 1e-10) -> float:
    if x < 0:
        raise ValueError("Cannot compute square root of negative number.")
    if x == 0 or x == 1:
        return x

    guess = x / 2.0
    while abs(guess * guess - x) > epsilon:
        guess = (guess + x / guess) / 2.0
    return guess

now that we have the sqrt function we can define the root mean squared error function as :
def rmse(y: List[float], y_bis:List[float]) -> float:
    return sqrt(sum((y-y_bis)**2))

sum() is a native function in python.

Anonymous

4.2Exceptional
def rms(array1,array2):
"""
Calculate the root mean square error between two arrays.
Args:
array1: A numpy array.
array2: A numpy array.
Returns:
The root mean square error between the two arrays.
"""
return np.sqrt(np.mean((array1 - array2,axis=0) ** 2)'''
  • Could you code a function to calculate the root mean squared error between two arrays?
  • Please demonstrate how you would develop a function to compute the RMSE for two input arrays.
  • Can you compose a function that takes in two arrays and outputs the RMSE?
  • Would you be able to write a function that inputs two arrays and computes the RMSE?
  • How would you go about writing a function that determines the RMSE for two provided arrays?
  • Can you craft a function to evaluate the root mean squared error of two arrays?
  • Show how you would implement a function to return the root mean squared error for two arrays.
  • Could you create a function that receives two arrays and calculates the RMSE?
  • How would you construct a function to find the RMSE for a pair of input arrays?
  • Write a function that inputs 2 arrays and returns the root mean squared error for them.
Try Our AI Interviewer

Prepare for success with realistic, role-specific interview simulations.

Try AI Interview Now

Interview question asked to Machine Learning Engineers and Data Scientists interviewing at Viber, Yandex, Pluralsight and others: Could you code a function to calculate the root mean squared error between two arrays?.