Pure Python SPACE: sparse partial correlation estimation
Project description
space-graph
Discover which variables in your dataset are directly related to each other, even after accounting for all other variables. Given a data matrix, SPACE estimates a sparse network of partial correlations -- connections that remain after removing indirect effects. Designed for settings where the number of variables can far exceed the number of samples (e.g. genomics).
Pure Python implementation of SPACE (Sparse Partial Correlation Estimation) from Peng et al. (2009), with no R or C dependencies.
Paper: Sparse Partial Correlation Estimation for High-Dimensional Data
Install
pip install space-graph
Optional Numba (faster inner jsrm loop):
pip install 'space-graph[numba]'
From GitHub:
pip install git+https://github.com/shahrozeabbas/space-graph.git
Usage
import numpy as np
from space_graph import SPACE
X = np.random.randn(20, 5)
model = SPACE(
alpha=0.7,
max_outer_iter=2,
max_inner_iter=500,
tol=1e-6,
weight='uniform',
)
model.fit(X)
print(model.partial_correlation_)
Choosing alpha
Use SPACE.select_alpha(X, alphas) to pick the regularization strength by BIC (Peng et al. 2009, Sec. 2.4). It fits across the grid and returns the alpha minimizing sum_i [n * log(RSS_i) + log(n) * k_i], where k_i is the number of neighbors of variable i.
import numpy as np
from space_graph import SPACE
template = SPACE(max_outer_iter=3)
alphas = np.geomspace(0.005, 1.0, 15)
best_alpha = template.select_alpha(X, alphas)
model = SPACE(alpha=best_alpha, max_outer_iter=3).fit(X)
Pass return_curve=True to also get the per-alpha BIC vector for plotting. The template is not mutated — reuse or discard it.
Penalty
alpha: overall regularization strength (like sklearn).gamma(γ) in[0, 1]: mix between L1 and L2 terms.lam1 = alpha * gamma(L1 / sparsity)lam2 = alpha * (1 - gamma)(L2 / ridge)- Default
gamma=1matches Rspace::space.jointdefaultlam2 = 0.
Options
tol(default1e-6): inner solver convergence + active-set threshold, same scale as the reference C code.weight: defaultuniform(equivalentlyequal, unit weights). Other modes:sig— reweight each variable by its residual variance (updates each outer iteration).degree— reweight by node connectivity (updates each outer iteration).- custom ndarray of length
p— internally normalized to mean 1, soalpha's scale stays comparable to the other modes.
backend(defaultauto): inner joint sparse regression (JSRM) solver.numpyalways uses pure NumPy.autouses Numba when installed (lazy on firstfit), otherwise NumPy.numbarequires Numba and raisesImportErrorif it is missing. The firstfitwithautoornumbapays a one-time compile cost.
Tests
pytest
Optional: build libjsrm_test.so from ../space/src/JSRM.c to run the ctypes cross-check in tests/test_space.py.
License
GPL-3.0-or-later (same family as the original space R package).
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file space_graph-1.0.0.tar.gz.
File metadata
- Download URL: space_graph-1.0.0.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
108282085b6787703b2509c4d20f265608e5fd99a29fdbd5e84c65bcb3a17734
|
|
| MD5 |
469fb6c57e89a40748dd49d928e89376
|
|
| BLAKE2b-256 |
6b160569ca48676fd98cd47bea2a79f5eda0ec6e0512f16b3bca055ed1052bd2
|
File details
Details for the file space_graph-1.0.0-py3-none-any.whl.
File metadata
- Download URL: space_graph-1.0.0-py3-none-any.whl
- Upload date:
- Size: 14.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3a18a558f924705b116488feb0ddd84940b6adf4f559e728bfa638492b2ef92
|
|
| MD5 |
b533d0d709468a14d5407537d98a6a27
|
|
| BLAKE2b-256 |
74fed5a1da1e56fbff8f3f6e0d2a1c31f85a749ee661b7247fbdfe0d8fdbca42
|