Registry / ai-ml / causal-learn

causal-learn

JSON →
library0.1.4.7pypypi✓ verified 87d ago

Causal-learn is a Python library for causal inference, providing various algorithms for causal discovery (identifying causal relationships from observational data) and causal effect estimation. It is part of the Py-Why project and aims to be a comprehensive toolkit for causality. The current version is 0.1.4.5, with frequent patch releases addressing bug fixes and minor enhancements.

pip install causal-learn
INSTALL
IMPORT
SIG · CAUSAL-LEARN
C
causal-learn
ai-mlpythonv0.1.4.7
Install
21.4s avg
Import
6375ms
Disk
516MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.4.7 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
build_error
glibc
py 3.103.910 runs
installs and imports cleanly · install 21.4s · import 6.375s · 493MB
516MB installed
● package 516MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pc
from causallearn.search.ConstraintBased.PC import pc
from causal_learn.search.ConstraintBased.PC import pc
The PyPI package name `causal-learn` uses a hyphen, but the Python import name is `causallearn` (no hyphen).
ges
from causallearn.search.ScoreBased.GES import ges
from causal_learn.search.ScoreBased.GES import ges
The PyPI package name `causal-learn` uses a hyphen, but the Python import name is `causallearn` (no hyphen).
chisq
from causallearn.utils.cit import chisq
Common conditional independence test for continuous data.

This quickstart demonstrates how to generate synthetic data, apply the PC (Peter and Clark) algorithm for causal discovery using the chi-squared conditional independence test, and print the resulting adjacency matrix representing the causal graph. The PC algorithm is a constraint-based method widely used for learning causal structures from observational data.

import numpy as np from causallearn.search.ConstraintBased.PC import pc from causallearn.utils.cit import chisq # 1. Generate synthetic data: X0 -> X1 <- X2, X0 -> X2 # X0 influences X2, and both X0 and X2 influence X1 np.random.seed(42) N = 1000 X0 = np.random.normal(0, 1, N) X2 = X0 * 0.5 + np.random.normal(0, 1, N) X1 = X0 * 0.3 + X2 * 0.7 + np.random.normal(0, 1, N) data = np.array([X0, X1, X2]).T # P-dimensional data (N samples, P variables) # 2. Run PC algorithm for causal discovery # data: input data matrix (N_samples, N_variables) # alpha: significance level for conditional independence test (e.g., 0.05) # ci_test: conditional independence test function (e.g., chisq for continuous data) # verbose: set to True for detailed progress output # num_cores: number of cores to use for parallel computation (-1 for all available) causal_graph = pc(data, alpha=0.05, ci_test=chisq, verbose=False, num_cores=-1) # 3. Print the adjacency matrix of the discovered causal graph # An entry (i, j) == 1 indicates an edge from i to j, 0 otherwise. print("\nDiscovered Causal Graph Adjacency Matrix:") print(causal_graph.G.graph)
Debug
Known issues
gotchaThe PyPI package name `causal-learn` uses a hyphen, but the Python import name is `causallearn` (no hyphen). This often leads to `ModuleNotFoundError` if `from causal_learn import ...` is used.
fix
Always use `from causallearn import ...` for imports.
affects: All versions
gotchaVisualizing causal graphs with `GraphUtils.plot_networkx_graph` requires a system-wide installation of Graphviz in addition to the Python `graphviz` package. Without it, visualization functions will raise `ExecutableNotFound` errors.
fix
Install Graphviz on your operating system (e.g., `sudo apt-get install graphviz` on Debian/Ubuntu, `brew install graphviz` on macOS) and then `pip install graphviz pydot`.
affects: All versions
gotchaInput data for causal discovery algorithms (e.g., `pc`, `ges`) must typically be a 2D `numpy.ndarray` with shape `(n_samples, n_features)`. Providing other formats like pandas DataFrames directly or incorrectly shaped arrays will lead to errors.
fix
Ensure your data is converted to a `numpy.ndarray` before passing it to algorithms. For a pandas DataFrame `df`, use `df.values`.
affects: All versions
gotchaCausal-learn includes a wide array of algorithms, each with specific assumptions and parameter requirements. Misconfiguration of algorithms or using an inappropriate conditional independence test (e.g., `chisq` for discrete data or `gsq` for non-linear continuous data) can lead to incorrect results or runtime errors.
fix
Consult the official documentation for the specific algorithm and CI test being used to understand its assumptions and parameter settings. Common CI tests include `chisq` (for discrete), `gsq` (for continuous linear), and `kci` (for continuous non-linear).
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'causal_learn'
Incorrect package name used in the import statement. The PyPI package name `causal-learn` is different from its Python import name `causallearn`.
fix
Change your import statements from `from causal_learn import ...` to `from causallearn import ...`.
graphviz.backend.ExecutableNotFound: failed to execute dot; make sure the Graphviz executables are on your systems' path
The Graphviz command-line tools are not installed or not accessible in your system's PATH. The Python `graphviz` package is a wrapper; it requires the underlying Graphviz system tools.
fix
Install Graphviz on your operating system (e.g., `sudo apt-get install graphviz` on Debian/Ubuntu, `brew install graphviz` on macOS, or download from graphviz.org for Windows) and ensure it's in your system's PATH.
ValueError: Input data must be a numpy array.
A causal-learn algorithm received input data that was not a `numpy.ndarray` or was not in the expected 2D `(n_samples, n_features)` format.
fix
Convert your data to a `numpy.ndarray`. If starting from a pandas DataFrame `df`, use `data = df.values`. Ensure it has the correct shape.
Upgrade
Version history
0.1.4.7latest on PyPI · released May 18, 2026
Audit
Dependencies
numpyrequiredCore numerical operations and data structures.
scipyrequiredScientific computing, statistics, and optimization.
networkxrequiredGraph data structures and algorithms.
scikit-learnrequiredUtility functions and machine learning algorithms.
pandasrequiredData manipulation and analysis, often used for input data.
matplotlibrequiredPlotting and visualization capabilities.
tqdmrequiredProgress bars for iterative computations.
joblibrequiredLightweight pipelining for parallel computation.
Agent activity
21 hits · last 30 days
node
20
OpenAI (training)
1
Resources
causal-learn — pip install causal-learn · libregistry