Explained Variance Ratios

Xem dạng PDF

Gửi bài giải

Điểm: 100,00
Giới hạn thời gian: 2.0s
Giới hạn bộ nhớ: 256M

Tác giả:
Dạng bài
Ngôn ngữ cho phép
Python

Problem Statement

Given the eigenvalues (or squared singular values) from a PCA decomposition, compute the explained variance ratio for each component and the cumulative explained variance.

Algorithm:

  1. Let the total variance = sum of all eigenvalues.
  2. The ratio for eigenvalue λ_i is λ_i / total.
  3. The cumulative ratios are the running sum of the individual ratios.

Function signature:

def explained_variance(evals: list) -> tuple:
    # returns (ratios, cumulative)
    # both are lists of floats in the same order as input

Input Format

Line 1: k — number of eigenvalues
Lines 2..k+1: one float per line — eigenvalues in descending order

Output Format

Line 1: k space-separated ratios (10 significant figures each)
Line 2: k space-separated cumulative ratios (10 significant figures each)

Example

Input:

4
10.0
4.0
3.0
3.0

Output:

0.5 0.2 0.15 0.15
0.5 0.7 0.85 1

Notes

  • The ratios always sum to 1.0.
  • The cumulative array's last value is always 1.0.
  • In PCA, eigenvalues are typically sorted in descending order, so the first component explains the most variance.

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.