{"id":9570,"library":"causal-learn","title":"Causal-Learn (Causal Inference Library)","description":"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.","status":"active","version":"0.1.4.5","language":"en","source_language":"en","source_url":"https://github.com/py-why/causal-learn","tags":["causal inference","causality","machine learning","statistics","graphical models","causal discovery"],"install":[{"cmd":"pip install causal-learn","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core numerical operations and data structures.","package":"numpy"},{"reason":"Scientific computing, statistics, and optimization.","package":"scipy"},{"reason":"Graph data structures and algorithms.","package":"networkx"},{"reason":"Utility functions and machine learning algorithms.","package":"scikit-learn"},{"reason":"Data manipulation and analysis, often used for input data.","package":"pandas"},{"reason":"Plotting and visualization capabilities.","package":"matplotlib"},{"reason":"Progress bars for iterative computations.","package":"tqdm"},{"reason":"Lightweight pipelining for parallel computation.","package":"joblib"}],"imports":[{"note":"The PyPI package name `causal-learn` uses a hyphen, but the Python import name is `causallearn` (no hyphen).","wrong":"from causal_learn.search.ConstraintBased.PC import pc","symbol":"pc","correct":"from causallearn.search.ConstraintBased.PC import pc"},{"note":"The PyPI package name `causal-learn` uses a hyphen, but the Python import name is `causallearn` (no hyphen).","wrong":"from causal_learn.search.ScoreBased.GES import ges","symbol":"ges","correct":"from causallearn.search.ScoreBased.GES import ges"},{"note":"Common conditional independence test for continuous data.","symbol":"chisq","correct":"from causallearn.utils.cit import chisq"}],"quickstart":{"code":"import numpy as np\nfrom causallearn.search.ConstraintBased.PC import pc\nfrom causallearn.utils.cit import chisq\n\n# 1. Generate synthetic data: X0 -> X1 <- X2, X0 -> X2\n# X0 influences X2, and both X0 and X2 influence X1\nnp.random.seed(42)\nN = 1000\nX0 = np.random.normal(0, 1, N)\nX2 = X0 * 0.5 + np.random.normal(0, 1, N)\nX1 = X0 * 0.3 + X2 * 0.7 + np.random.normal(0, 1, N)\ndata = np.array([X0, X1, X2]).T # P-dimensional data (N samples, P variables)\n\n# 2. Run PC algorithm for causal discovery\n# data: input data matrix (N_samples, N_variables)\n# alpha: significance level for conditional independence test (e.g., 0.05)\n# ci_test: conditional independence test function (e.g., chisq for continuous data)\n# verbose: set to True for detailed progress output\n# num_cores: number of cores to use for parallel computation (-1 for all available)\ncausal_graph = pc(data, alpha=0.05, ci_test=chisq, verbose=False, num_cores=-1)\n\n# 3. Print the adjacency matrix of the discovered causal graph\n# An entry (i, j) == 1 indicates an edge from i to j, 0 otherwise.\nprint(\"\\nDiscovered Causal Graph Adjacency Matrix:\")\nprint(causal_graph.G.graph)","lang":"python","description":"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."},"warnings":[{"fix":"Always use `from causallearn import ...` for imports.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"},{"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`.","message":"Visualizing 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your data is converted to a `numpy.ndarray` before passing it to algorithms. For a pandas DataFrame `df`, use `df.values`.","message":"Input 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.","severity":"gotcha","affected_versions":"All versions"},{"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).","message":"Causal-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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Change your import statements from `from causal_learn import ...` to `from causallearn import ...`.","cause":"Incorrect package name used in the import statement. The PyPI package name `causal-learn` is different from its Python import name `causallearn`.","error":"ModuleNotFoundError: No module named 'causal_learn'"},{"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.","cause":"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.","error":"graphviz.backend.ExecutableNotFound: failed to execute dot; make sure the Graphviz executables are on your systems' path"},{"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.","cause":"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.","error":"ValueError: Input data must be a numpy array."}]}