Code Monkey home page Code Monkey logo

Comments (5)

 avatar commented on June 4, 2024

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.

SteveLerQB avatar SteveLerQB commented on June 4, 2024

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.

sriharsha0806 avatar sriharsha0806 commented on June 4, 2024

@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.

SteveLerQB avatar SteveLerQB commented on June 4, 2024

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:

image1

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)

image2

By specifying tabu_edges, the algorithm will learn the structure without these edges.

I hope this helps! 🙂

from causalnex.

sriharsha0806 avatar sriharsha0806 commented on June 4, 2024

@SteveLerQB Thanks for the explanation

from causalnex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.