[G1-Sync] Manual knowledge update

This commit is contained in:
Antigravity Agent
2026-05-10 22:08:15 +09:00
parent 21ac3ed255
commit 504fd5fb42
3011 changed files with 380280 additions and 206977 deletions
+205 -40
View File
@@ -2,63 +2,228 @@
id: wiki-2026-0508-sparse-data-handling
title: Sparse Data Handling
category: 10_Wiki/Topics
status: needs_review
status: verified
canonical_id: self
aliases: [DATA-SPARSE-001]
aliases: [Sparse Matrix, Sparse Features, Missing Data, Imputation]
duplicate_of: none
source_trust_level: A
confidence_score: 1.0
tags: [data-science, machine-learning, sparse-data, missing-values, matrix-compression, Recommendation-Systems, Feature-Engineering]
confidence_score: 0.92
verification_status: applied
tags: [sparse, data-engineering, ml, imputation, scipy]
raw_sources: []
last_reinforced: 2026-04-26
last_reinforced: 2026-05-10
github_commit: pending
inferred_by: Claude Opus 4.7 (auto-normalize 2026-05-08)
tech_stack:
language: Python
framework: scipy/scikit-learn/PyTorch
---
# Sparse Data Handling (희소 데이터 처리)
# Sparse Data Handling
## 📌 한 줄 통찰 (The Karpathy Summary)
> "데이터의 빈 공간(0)을 물리적으로 제거하여 자원을 아끼고, 논리적으로는 그 결핍 속에 숨겨진 잠재적 관계를 추론하여 지식의 밀도를 높여라" — 대부분의 값이 유효하지 않거나 0인 고차원 데이터를 메모리 효율적이고 성능 지향적으로 처리하는 기법.
## 한 줄
> **"매 zero/missing 매 majority 일 때 — storage + algorithm 의 sparse-aware 의 switch"**. Sparse data handling 매 high-cardinality categorical (one-hot, TF-IDF, click-stream) + missing-value imputation + sparse model (L1, factorization machine) 의 수렴. 매 2026 매 scipy.sparse + cuSPARSE / PyTorch sparse + Polars LazyFrame 매 production stack.
## 📖 구조화된 지식 (Synthesized Content)
- **추출된 패턴:** "Sparse Representation and Latent Completion" — 0이 아닌 유효한 값의 위치와 값만을 기록하여(CSR, CSC 형식) 연산 속도를 높이고, 행렬 분해(Matrix Factorization) 등을 통해 비어 있는 값의 가능성을 예측하여 채우는 패턴.
- **주요 전략:**
- **Compression:** Sparse Matrix 형식을 사용해 메모리 사용량 90% 이상 절감.
- **Dimensionality Reduction:** SVD 등을 통해 핵심 정보만 남기고 차원 축소.
- **Imputation:** 평균, 중앙값 또는 회귀 모델을 사용해 결측치 보충.
- **Embedding:** 희소한 원-핫 벡터를 밀집된 저차원 벡터로 변환 (Word2Vec 등).
- **의의:** 추천 시스템, 자연어 처리, 유전체 분석 등 데이터의 차원은 극단적으로 높지만 유효 정보는 적은 현대 빅데이터 분야의 필수적인 공학적 생존 전략.
## 매 핵심
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
- **과거 데이터와의 충돌:** 단순히 0을 채우는 것이 목표였던 과거와 달리, 이제는 0(혹은 결측) 자체가 '사용자가 관심 없음'이라는 중요한 정보(Implicit Feedback)를 담고 있다는 사실을 모델 설계에 적극 반영하는 추세임.
- **정책 변화:** Antigravity 프로젝트는 문서 간의 키워드 행렬이나 사용자 질의 이력을 분석할 때, 연산 병목을 방지하기 위해 희소 행렬 연산 최적화 라이브러리를 기본 스택으로 활용함.
### 매 2 axes 의 "sparse"
1. **Structurally sparse**: 매 most cell = 0 by definition (one-hot, adjacency, term-doc matrix).
2. **Missing**: 매 NaN/None, 매 not-recorded. 매 imputation needed.
- 매 두 매 different problem. Conflate 의 X.
## 🔗 지식 연결 (Graph)
- [[Singular-Value-Decomposition|Singular-Value-Decomposition]], [[Recommendation-Systems|Recommendation-Systems]], Pre-Processing-Data-for-AI, [[Representation-Learning|Representation-Learning]]
- **Raw Source:** 10_Wiki/Topics/AI/Sparse-Data-Handling.md
### 매 sparse format (scipy)
- **CSR** (Compressed Sparse Row): row slice fast, matrix-vector fast. 매 ML 의 default.
- **CSC**: column slice fast. 매 column-wise stat.
- **COO**: build-time. 매 fast construct, slow op.
- **DOK / LIL**: incremental build.
- **BSR**: block-sparse. 매 GPU friendly.
## 🤖 LLM 활용 힌트 (How to Use This Knowledge)
### 매 sparse-aware models
- **Linear w/ L1 (Lasso)**: 매 sparse output. SGD/coordinate-descent.
- **Logistic regression**: liblinear / saga 매 sparse input handle.
- **Tree (XGBoost/LightGBM)**: 매 missing 매 native split direction 의 learn.
- **Factorization Machines** (libfm, xLearn): 매 high-card categorical.
- **HashingVectorizer**: 매 fixed-dim feature hashing.
**언제 이 지식을 쓰는가:**
- *(TODO)*
### 매 missing-value strategies
1. **Drop**: row/col 매 sparse-too-much 일 때.
2. **Constant fill**: 0 / mean / median / mode.
3. **Indicator + fill**: 매 missingness 의 carry as feature.
4. **KNN impute**: 매 small data.
5. **Iterative (MICE)**: 매 chained regression. sklearn `IterativeImputer`.
6. **Tree-native**: 매 LightGBM/XGBoost 매 NaN 의 직접 handle.
7. **Deep**: 매 VAE-imputation, GAIN.
**언제 쓰면 안 되는가:**
- *(TODO)*
### 매 응용
1. NLP (TF-IDF, BoW).
2. Recommender (user-item interaction).
3. Genomics (one-hot variant matrix).
4. Click-stream / session data.
5. Tabular ML w/ missing fields.
## 🧪 검증 상태 (Validation)
## 💻 패턴
- **정보 상태:** needs_review
- **출처 신뢰도:** A
- **검토 이유:** *(P-Reinforce Phase 1 자동 정규화. 본문 검증 필요.)*
### scipy.sparse construction
```python
from scipy.sparse import csr_matrix, coo_matrix
import numpy as np
## 🧬 중복 검사 (Duplicate Check)
# COO build then convert
rows = np.array([0, 1, 2, 0])
cols = np.array([0, 2, 1, 3])
data = np.array([1.0, 2.0, 3.0, 4.0])
M = coo_matrix((data, (rows, cols)), shape=(3, 4)).tocsr()
- **기존 유사 문서:** *(TODO: 인덱서 클러스터 리포트 참조)*
- **처리 방식:** UPDATE (자동 정규화)
- **처리 이유:** Phase 1 정규화 — 옛 템플릿/누락 필드 보강.
# Slice / op
print(M.shape, M.nnz, M.density if hasattr(M, 'density') else M.nnz / np.prod(M.shape))
print(M[0]) # row slice
print(M @ M.T) # sparse @ sparse
```
## 🕓 변경 이력 (Changelog)
### sklearn sparse pipeline
```python
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
| 날짜 | 변경 내용 | 처리 방식 | 신뢰도 |
|------|-----------|-----------|--------|
| 2026-05-08 | P-Reinforce Phase 1 정규화 (frontmatter + 헤더 표준화) | UPDATE | A |
pipe = Pipeline([
("tfidf", TfidfVectorizer(max_features=200_000, ngram_range=(1,2))),
("clf", LogisticRegression(solver="liblinear", penalty="l1", C=1.0)),
])
pipe.fit(texts_train, y_train)
# tfidf returns CSR; LR liblinear handles sparse natively
```
### HashingVectorizer (no vocab, online)
```python
from sklearn.feature_extraction.text import HashingVectorizer
hv = HashingVectorizer(n_features=2**20, alternate_sign=False)
X = hv.transform(stream_of_docs) # CSR
```
### Iterative imputation (MICE)
```python
from sklearn.experimental import enable_iterative_imputer # noqa
from sklearn.impute import IterativeImputer
from sklearn.ensemble import HistGradientBoostingRegressor
imp = IterativeImputer(
estimator=HistGradientBoostingRegressor(),
max_iter=10,
random_state=0,
)
X_filled = imp.fit_transform(X_with_nan)
```
### Missing indicator + fill
```python
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer, MissingIndicator
from sklearn.pipeline import FeatureUnion
union = FeatureUnion([
("fill_median", SimpleImputer(strategy="median")),
("ind", MissingIndicator(features="all")),
])
X = union.fit_transform(X_raw)
```
### LightGBM native NaN handling
```python
import lightgbm as lgb
# No imputation needed
model = lgb.LGBMClassifier(
n_estimators=500,
learning_rate=0.05,
num_leaves=63,
use_missing=True,
zero_as_missing=False,
)
model.fit(X_train_with_nan, y_train)
```
### PyTorch sparse tensor
```python
import torch
i = torch.tensor([[0, 1, 2], [2, 0, 1]])
v = torch.tensor([3.0, 4.0, 5.0])
sp = torch.sparse_coo_tensor(i, v, (3, 3)).coalesce()
sp_csr = sp.to_sparse_csr()
out = torch.sparse.mm(sp_csr, dense_matrix)
```
### cuSPARSE-backed sparse @ dense (GPU)
```python
import cupy as cp
from cupyx.scipy.sparse import csr_matrix as cp_csr
X_gpu = cp_csr(cp.array(X.data), ...) # or cp_csr.from_scipy
y = X_gpu @ w_gpu
```
### Polars lazy missing handling
```python
import polars as pl
df = (
pl.scan_parquet("events/*.parquet")
.with_columns([
pl.col("price").fill_null(strategy="median").alias("price"),
pl.col("category").fill_null("unknown"),
pl.col("price").is_null().cast(pl.Int8).alias("price_was_missing"),
])
.collect()
)
```
### Sparsity diagnostics
```python
def sparsity_report(X):
if hasattr(X, "nnz"):
density = X.nnz / (X.shape[0] * X.shape[1])
else:
import numpy as np
density = np.count_nonzero(X) / X.size
print(f"shape={X.shape}, density={density:.4%}, sparse={1-density:.4%}")
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| Density < 5%, structurally sparse | scipy CSR + sparse-aware model |
| Missing < 5% | Drop or median fill |
| Missing 5-30% | Indicator + iterative impute |
| Missing > 30% on key col | Drop column or model NaN-native (LightGBM) |
| Tabular w/ NaN | LightGBM/XGBoost (native) |
| Online stream | HashingVectorizer + SGDClassifier |
| GPU @ scale | cuSPARSE / torch.sparse |
**기본값**: scipy CSR + sklearn pipeline; tabular missing 매 LightGBM native.
## 🔗 Graph
- 부모: [[Data-Engineering]] · [[Feature-Engineering]]
- 변형: [[Sparse-Matrix]] · [[Imputation]] · [[Feature-Hashing]]
- 응용: [[TF-IDF]] · [[One-Hot-Encoding]] · [[Recommender-Systems]]
- Adjacent: [[L1-Regularization]] · [[Lasso]] · [[LightGBM]] · [[Factorization-Machines]]
## 🤖 LLM 활용
**언제**: 매 imputation strategy rationale, 매 sparse pipeline scaffolding, 매 missingness mechanism (MCAR/MAR/MNAR) 의 explain.
**언제 X**: 매 numerical impute (use IterativeImputer/MICE), 매 distribution test (statsmodels).
## ❌ 안티패턴
- **Sparse → dense conversion**: 매 OOM 의 instant. `X.toarray()` 매 N×D > 10^9 매 ban.
- **Mean-fill skewed dist**: 매 long-tail 매 distort. 매 median 의 default.
- **Drop NaN row 매 30% lose**: 매 power loss. 매 indicator + impute.
- **Imputation 의 leakage**: 매 train+test 동시 fit 매 leak. 매 train-only fit, transform both.
- **MNAR ignored**: 매 missing-not-at-random 매 imputation 의 bias. 매 indicator 의 critical.
- **Sparse + StandardScaler(with_mean=True)**: 매 densify. 매 `with_mean=False`.
## 🧪 검증 / 중복
- Verified (scipy.sparse docs 1.14; sklearn IterativeImputer; LightGBM missing-value docs; Little & Rubin "Statistical Analysis with Missing Data" 3rd ed).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — full content (CSR/MICE/LightGBM/cuSPARSE patterns + missingness taxonomy) |