Skip to content
DeepForgeby svx
All problems
HardML Fundamentalsml-045

Explained Variance Ratio

Problem

Compute the explained variance ratio of a unit principal component v: the population variance of the projected (mean-centered) data divided by the total variance, which is the sum of the per-feature population variances.

If the total variance is 0, return 0.0. Empty X returns 0.0.

Hint

Ratio = variance of projected coordinates / sum of per-feature variances.

Test cases

case 1
in: [[[1,2],[3,4],[5,6]],[0.7071067811865476,0.7071067811865476]]
expected: 1.0000000000000004
case 2
in: [[[1,2],[3,4],[5,6]],[0.7071067811865476,-0.7071067811865476]]
expected: 0
case 3
in: [[[2,0],[0,1],[-2,0],[0,-1]],[1,0]]
expected: 0.8
case 4
in: [[[5,5],[5,5]],[1,0]]
expected: 0
Starter codePython
def explained_variance_ratio(X, v):
    # Your code here
    pass

4 test cases · run your code against them in the editor