PIXELBANKv9.1.0
Menu

Compute the explained variance ratio from eigenvalues.

Given a list of eigenvalues (sorted descending), compute: ratioi=λi∑jλj\text{ratio}_i = \frac{\lambda_i}{\sum_{j} \lambda_j}

Also compute the cumulative explained variance ratio.

Return a tuple (ratios, cumulative) where both are lists rounded to 4 decimal places.

Example:

Input:
eigenvalues = [4.0, 2.0, 1.0, 0.5]
Output:
([0.5333, 0.2667, 0.1333, 0.0667], [0.5333, 0.8, 0.9333, 1.0])
Reasoning:
  • First, we calculate the sum of all eigenvalues: ∑jλj=4.0+2.0+1.0+0.5=7.5\sum_{j} \lambda_j = 4.0 + 2.0 + 1.0 + 0.5 = 7.5
  • Then, we compute the explained variance ratio for each eigenvalue: ratioi=λi∑jλj\text{ratio}_i = \frac{\lambda_i}{\sum_{j} \lambda_j}, resulting in 4.07.5=0.5333\frac{4.0}{7.5} = 0.5333, 2.07.5=0.2667\frac{2.0}{7.5} = 0.2667, 1.07.5=0.1333\frac{1.0}{7.5} = 0.1333, 0.57.5=0.0667\frac{0.5}{7.5} = 0.0667
  • Next, we calculate the cumulative explained variance ratio by summing the individual ratios: 0.53330.5333, 0.5333+0.2667=0.80.5333 + 0.2667 = 0.8, 0.8+0.1333=0.93330.8 + 0.1333 = 0.9333, 0.9333+0.0667=1.00.9333 + 0.0667 = 1.0
  • The final output is a tuple containing the lists of ratios and cumulative ratios, both rounded to 4 decimal places: ([0.5333,0.2667,0.1333,0.0667],[0.5333,0.8,0.9333,1.0])([0.5333, 0.2667, 0.1333, 0.0667], [0.5333, 0.8, 0.9333, 1.0])

Constraints:

  • eigenvalues: list of non-negative floats (sorted descending)
  • Return (ratios, cumulative) rounded to 4 decimal places
  • cumulative[i] = sum(ratios[0:i+1])
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Variance Explained Ratio - Easy | PixelBank