Contact
Back to Home

Could you create a function that receives two arrays and calculates the RMSE?

Featured Answer

Question Analysis

The question requires you to create a function that calculates the Root Mean Square Error (RMSE) between two arrays. RMSE is a metric used to measure the differences between values predicted by a model and the actual observed values. It is a frequently used measure in regression analysis and is especially useful for comparing prediction errors of different models.

To solve this problem, you need to:

  • Ensure the two arrays have the same length, as RMSE calculation requires pairwise comparison.
  • Calculate the difference between each pair of elements in the arrays.
  • Square these differences.
  • Compute the mean of these squared differences.
  • Take the square root of this mean to get the RMSE.

Answer

import numpy as np

def calculate_rmse(array1, array2):
    """
    Calculate the Root Mean Square Error (RMSE) between two arrays.

    :param array1: First array of numerical values
    :param array2: Second array of numerical values, same length as array1
    :return: RMSE value
    """

    # Check if both arrays have the same length
    if len(array1) != len(array2):
        raise ValueError("Both arrays must have the same length.")
    
    # Convert to numpy arrays for easier computation
    array1 = np.array(array1)
    array2 = np.array(array2)

    # Calculate the squared differences
    squared_differences = (array1 - array2) ** 2

    # Compute the mean of the squared differences
    mean_squared_difference = np.mean(squared_differences)

    # Calculate the square root of the mean to get the RMSE
    rmse = np.sqrt(mean_squared_difference)

    return rmse

# Example usage:
# array1 = [1, 2, 3]
# array2 = [1.1, 2.2, 3.3]
# print(calculate_rmse(array1, array2))

Explanation:

  • The function calculate_rmse takes two arrays as input.
  • It first checks if the arrays have the same length and raises a ValueError if they do not.
  • It then converts the arrays to numpy arrays for efficient computation of numerical operations.
  • The squared differences between corresponding elements of the arrays are calculated.
  • The mean of these squared differences is computed.
  • Finally, the RMSE is obtained by taking the square root of the mean squared difference.