CausalLib

raw JSON →
0.10.0 verified Sat May 09 auth: no python

A Python package for flexible and modular causal inference modeling. Current version 0.10.0. Provides a scikit-learn compatible API for causal effect estimation, including propensity score matching, weighting, doubly robust estimation, and survival analysis. Released irregularly.

pip install causallib
error AttributeError: 'DataFrame' object has no attribute 'as_matrix'
cause Older scikit-learn or causallib code uses deprecated `.as_matrix()` instead of `.values`.
fix
Upgrade causallib to 0.9.7+ or use pandas to_numpy().
error ImportError: cannot import name 'load_nhefs' from 'causallib.datasets'
cause Deprecated dataset function removed; replaced by `load_nhefs_survival`.
fix
Use from causallib.datasets import load_nhefs_survival.
error KeyError: 'Columns not found' when calling .fit()
cause Passing X as numpy array without column names; causallib expects DataFrame with column names.
fix
Convert X to DataFrame: import pandas as pd; X = pd.DataFrame(X, columns=['col0','col1',...]).
gotcha Data must be provided as DataFrames (not array-like). Many methods internally expect pandas DataFrames for indexing and column alignment; passing numpy arrays may cause silent errors or attribute failures.
fix Convert inputs to pandas DataFrame/Series before fitting.
breaking In version 0.9.0 the evaluation module was refactored; plot APIs changed. Old imports from causallib.evaluation no longer work.
fix Use the new causal_metrics module and updated plot functions. See migration guide in the release notes.
deprecated The `causallib.simulation` module is being phased out. Networkx >=3 may break old simulation code.
fix Use the newer simulator API or external simulation libraries.

Fits Inverse Probability Weighting (IPW) to estimate potential outcomes on the IHDP dataset.

from causallib.datasets import load_ihdp
from causallib.estimation import IPW
from sklearn.linear_model import LogisticRegression

data = load_ihdp()
X = data['X']
a = data['a']
y = data['y']

learner = LogisticRegression(solver='lbfgs')
ipw = IPW(learner)
ipw.fit(X, a)
potential_outcomes = ipw.estimate_population_outcome(X, a, y)
print(potential_outcomes)