Comments (5)
Cleaning up the code block, it looks like
sm_manual.add_edges_from([("b", "a", origin="expert")])
but it should be (i.e: the origin
kwarg should be a kwarg, not a member of the tuple):
sm_manual.add_edges_from([("b", "a")], origin="expert")
Does that work?
from causalnex.
Yes, there's an error in the code snippet in the user guide https://causalnex.readthedocs.io/en/latest/04_user_guide/04_user_guide.html
Just created a pull request to fix this 🙂
#10
from causalnex.
@ZainPatelQB @SteveLerQB continuation of the tutorial, I have defined structure Model as
from causalnex.structure import StructureModel
# Encoding the causal graph suggested by an expert
# d
# ↙ ↓ ↘
# a ← b → c
# ↑ ↗
# e
sm_manual = StructureModel()
sm_manual.add_edges_from(
[
("b","a"),
("b","c"),
("d","a"),
("d","c"),
("d","b"),
("e","c"),
("e","b"),
],
origin="expert",
)
I am using struct_data from previous causalnex tutorial.
from causalnex.structure.notears import from_pandas
from causalnex.network import BayesianNetwork
# Unconstrained learning of the structure from data
sm = from_pandas(struct_data)
# Imposing edges that are not allowed in the causal model
sm_with_tabu_edges = from_pandas(struct_data, tabu_edges=[("e", "a")])
# Imposing parent nodes that are not allowed in the causal model
sm_with_tabu_parents = from_pandas(struct_data, tabu_parent_nodes=["a", "c"])
# Imposing child nodes that are not allowed in the causal model
sm_with_tabu_parents = from_pandas(struct_data, tabu_child_nodes=["d", "e"])
When I ran the code I got the following KeyError,
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-28-cc0c03133d3a> in <module>
4 sm = from_pandas(struct_data)
5 # Imposing edges that are not allowed in the causal model
----> 6 sm_with_tabu_edges = from_pandas(struct_data, tabu_edges=[("e", "a")])
7 # Imposing parent nodes that are not allowed in the causal model
8 sm_with_tabu_parents = from_pandas(struct_data, tabu_parent_nodes=["a", "c"])
~/miniconda3/lib/python3.7/site-packages/causalnex/structure/notears.py in from_pandas(X, max_iter, h_tol, w_threshold, tabu_edges, tabu_parent_nodes, tabu_child_nodes)
241
242 if tabu_edges:
--> 243 tabu_edges = [(col_idx[u], col_idx[v]) for u, v in tabu_edges]
244 if tabu_parent_nodes:
245 tabu_parent_nodes = [col_idx[n] for n in tabu_parent_nodes]
~/miniconda3/lib/python3.7/site-packages/causalnex/structure/notears.py in <listcomp>(.0)
241
242 if tabu_edges:
--> 243 tabu_edges = [(col_idx[u], col_idx[v]) for u, v in tabu_edges]
244 if tabu_parent_nodes:
245 tabu_parent_nodes = [col_idx[n] for n in tabu_parent_nodes]
KeyError: 'e'
In this tutorial, data variable mentioned in the cell isn't provided. What should the data variable be?
from causalnex.
Dear @sriharsha0806,
Thanks for your question. Each of your data variable should have numerical rows. Let me give you a simple example for this.
from causalnex.structure import StructureModel
import numpy as np
import pandas as pd
from causalnex.structure.notears import from_pandas
from causalnex.plots import plot_structure
a = np.random.normal(0, 1, 200)
b = 5 + 3 * a
c = -6 * a
d = 10 * b
struct_data = pd.DataFrame({"a": a, "b": b, "c": c, "d": d})
sm = from_pandas(struct_data, w_threshold=0.5)
plot_structure(sm)
This will give you the following plot:
But you may think that there should not be any relationship between the data variable "a" and "c". You can do the following:
sm = from_pandas(struct_data, w_threshold=0.5, tabu_edges=[("a", "c")]) plot_structure(sm)
By specifying tabu_edges, the algorithm will learn the structure without these edges.
I hope this helps! 🙂
from causalnex.
@SteveLerQB Thanks for the explanation
from causalnex.
Related Issues (20)
- EMSingleLatentVariable is producing random error at random times HOT 1
- Add GitHub Actions installation jobs across environments HOT 1
- Unsuitability of Notears for causal inference HOT 3
- How do I save the fitted Bayesian model locally HOT 2
- get_target_subgraph function is not working HOT 1
- vis.show() UnicodeEncodeError HOT 9
- Find out the number of cycles HOT 1
- 01_first_tutorial.ipynb hangs on `from_pandas(...)`
- [Feature Request]: Support pandas >= 2.0
- [Bug]: Pycharm cannot use causalnex HOT 1
- [help]: I am unable to display images while using viz. show() HOT 1
- [Feature Request]: batch_size for notears with GPU
- [Bug]: Inconsistent Use of CUDA Devices When Using GPU with notears
- [Bug]: Classification Model always predicting 0 HOT 1
- [Bug]: fix typo in 04_user_guide.md
- [Feature Request]: Support Python 3.11
- [Bug]: causalnex.discretiser.MDLPSupervisedDiscretiserMethod does not import MDLP
- An issue with plotting[Bug]: HOT 1
- Discontinuous time series data HOT 1
- Numpy dependency update
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from causalnex.