Svd target reconstruction

From wenmin-wu/ds-skills · View interactive version

Svd target reconstruction is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Compresses high-dimensional targets with TruncatedSVD, trains on the reduced space, then reconstructs full predictions via the components matrix.

How to use this skill

Skill file

SVD Target Reconstruction

Overview

When predicting thousands of outputs (e.g., 23,418 gene expressions), training directly is slow and noisy. TruncatedSVD compresses targets to 64-512 components, capturing the dominant variance. The model trains on these compact representations, then at inference, predictions are projected back to full dimensionality by multiplying with the SVD components matrix. This reduces training time by 10-100x while preserving 95%+ of target variance, and acts as implicit regularization by discarding noise in low-variance components.

Quick Start

import numpy as np
from sklearn.decomposition import TruncatedSVD
import scipy.sparse

# Y_train shape: (n_samples, 23418) — can be sparse
svd = TruncatedSVD(n_components=128, random_state=42)
Y_reduced = svd.fit_transform(Y_train)  # (n_samples, 128)

print(f"Variance retained: {svd.explained_variance_ratio_.sum():.3f}")

# Train model on reduced targets
model.fit(X_train, Y_reduced)

# Reconstruct full predictions at inference
preds_reduced = model.predict(X_test)  # (n_test, 128)
preds_full = preds_reduced @ svd.components_  # (n_test, 23418)

Workflow

  1. Fit TruncatedSVD on training targets (works with sparse matrices)
  2. Transform targets to reduced space (64-512 dims)
  3. Train model on reduced targets
  4. At inference, multiply predictions by svd.components_ to reconstruct

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.