Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Given two arrays of numbers representing paired observations, compute the Pearson correlation coefficient between them using NumPy.
Return the correlation coefficient rounded to 2 decimal places.
| Argument | Type |
|---|---|
| nums1 | list |
| nums2 | list |
| Return Name | Type |
|---|---|
| value | float |
len(nums1) == len(nums2); otherwise must raise ValueError.
n = len(nums1) >= 2; otherwise must raise ValueError.
Inputs must be Python list objects; otherwise must raise TypeError.
If either list has zero variance (denominator is 0), must raise ValueError (correlation undefined).
Compute the means and first, then iterate again to accumulate numerator and denominator terms.
The denominator is ; if it becomes 0, correlation is undefined.
Use round(r, 2) at the end to enforce 2-decimal output.