Compute the Pearson correlation coefficient of two NumPy arrays as a scalar

Quick Overview

Implement a NumPy function that returns the Pearson correlation coefficient of two equal-length arrays as a single float. It tests reading a correlation matrix correctly, the centered formula for r, and how to handle inputs with zero variance or mismatched lengths.

Compute the Pearson correlation coefficient of two NumPy arrays as a scalar

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def pearson_correlation(x: np.ndarray, y: np.ndarray) -> float` that returns the Pearson correlation coefficient r between two equal-length 1-D arrays as a single scalar `float`. The return value must be one number. `np.corrcoef(x, y)` returns a 2x2 matrix, so if you use it, return `np.corrcoef(x, y)[0, 1]`. The result is checked with `numpy.testing.assert_allclose`. ```python x = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) y = np.array([2.0, 4.0, 5.0, 4.0, 6.0]) pearson_correlation(x, y) # -> 0.8528 (a single float) ``` ```hint Watch the return type An entry of a NumPy matrix is a NumPy scalar, and a slice such as `[0:1, 1:2]` is still an array. Convert to a Python `float` at the end, and think about what the formula does when one input is constant. ``` ### Constraints & Assumptions - `x` and `y` are 1-D arrays of the same length n, with n of at least 2. - Values are numeric (ints or floats) and finite. - The output is compared with `numpy.testing.assert_allclose`, so tiny floating-point differences are accepted. ### What a Strong Answer Covers - Returns a plain scalar `float`, taken from the off-diagonal entry of the correlation matrix or computed directly - States r as the covariance divided by the product of the standard deviations, computed from centered arrays - Explains what happens when either input has zero variance and picks a clear behavior for it - Converts inputs to float arrays and deals with mismatched lengths - Gives the cost: linear time in n ### Follow-up Questions - How would you compute r without `np.corrcoef`? - What should happen if `x` or `y` has zero variance? - How does r relate to the slope and the $R^2$ of a simple linear regression of y on x? - How would you compute r over data that arrives in chunks too large to hold in memory?

Overview: Implement a NumPy function that returns the Pearson correlation coefficient of two equal-length arrays as a single float. It tests reading a correlation matrix correctly, the centered formula for r, and how to handle inputs with zero variance or mismatched lengths.

General logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement def pearson_correlation(x: np.ndarray, y: np.ndarray) -> float that returns the Pearson correlation coefficient r between two equal-length 1-D arrays as a single scalar float.

The return value must be one number. np.corrcoef(x, y) returns a 2x2 matrix, so if you use it, return np.corrcoef(x, y)[0, 1]. The result is checked with numpy.testing.assert_allclose.

x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([2.0, 4.0, 5.0, 4.0, 6.0])
pearson_correlation(x, y)  # -> 0.8528 (a single float)

Constraints & Assumptions

  • x and y are 1-D arrays of the same length n, with n of at least 2.
  • Values are numeric (ints or floats) and finite.
  • The output is compared with numpy.testing.assert_allclose , so tiny floating-point differences are accepted.

What a Strong Answer Covers Guidance

  • Returns a plain scalar float , taken from the off-diagonal entry of the correlation matrix or computed directly
  • States r as the covariance divided by the product of the standard deviations, computed from centered arrays
  • Explains what happens when either input has zero variance and picks a clear behavior for it
  • Converts inputs to float arrays and deals with mismatched lengths
  • Gives the cost: linear time in n

Follow-up Questions Guidance

  • How would you compute r without np.corrcoef ?
  • What should happen if x or y has zero variance?
  • How does r relate to the slope and the R2R^2 of a simple linear regression of y on x?
  • How would you compute r over data that arrives in chunks too large to hold in memory?