py-why / causal-learn

Causal Discovery in Python. It also includes (conditional) independence tests and score functions.
https://causal-learn.readthedocs.io/en/latest/
MIT License
1.12k stars 183 forks source link

QUESTION: To get DAG/Graph from FCM-based methods #102

Closed ukamath closed 1 year ago

ukamath commented 1 year ago

With FCM-based methods, for instance DirectiINGAM, how do we get DAG so that we can compare and contrast the structure with other methods such as PC,FCI, GES etc? Are there any utilities?

from causallearn.search.FCMBased import lingam model = lingam.DirectLiNGAM() model.fit(data)

print(model.causalorder) print(model.adjacencymatrix)

Thanks

ukamath commented 1 year ago

Got it working using DiGraph

import networkx as nx import matplotlib.pyplot as plt

create DAG using NetworkX

dag = nx.DiGraph(model.adjacencymatrix)

relabel nodes with variables X1, X2, ..., XN

node_labels = {i: f"X{i+1}" for i in range(len(dag.nodes))} dag = nx.relabel_nodes(dag, node_labels)

draw DAG using Matplotlib

nx.draw(dag, with_labels=True) plt.show()