Can someone correct the code ? It says NameError: name 'Node' is not defined

1.2k Views Asked by At
# Also the terms Node, DiscreteDistribution, ConditionalProbabilityTable, distribution, BayesianNetwork(), add_edge, bake are not being recognised.

from pomegranate import \*

# Rain node has no parents

rain = Node(DiscreteDistribution({"none": 0.7,"light": 0.2,"heavy": 0.1}), name="rain")

# Track maintenance node is conditional on rain

maintenance = Node(ConditionalProbabilityTable(\[\["none", "yes", 0.4\],\["none", "no", 0.6\],\["light", "yes", 0.2\],\["light", "no", 0.8\],\["heavy", "yes", 0.1\],\["heavy", "no", 0.9\]\], \[rain.distribution\]), name="maintenance")

# Train node is conditional on rain and maintenance

train = Node(ConditionalProbabilityTable(\[\["none", "yes", "on time", 0.8\],\["none", "yes", "delayed", 0.2\],\["none", "no", "on time", 0.9\],\["none", "no", "delayed", 0.1\],\["light", "yes", "on time", 0.6\],
\["light", "yes", "delayed", 0.4\],\["light", "no", "on time", 0.7\],\["light", "no", "delayed", 0.3\],
\["heavy", "yes", "on time", 0.4\],\["heavy", "yes", "delayed", 0.6\],\["heavy", "no", "on time", 0.5\],\["heavy", "no", "delayed", 0.5\],\], \[rain.distribution, maintenance.distribution\]), name="train")

# Appointment node is conditional on train

appointment = Node(ConditionalProbabilityTable(\[\["on time", "attend", 0.9\],\["on time", "miss", 0.1\],\["delayed", "attend", 0.6\],\["delayed", "miss", 0.4\]\], \[train.distribution\]), name="appointment")

# Create a Bayesian Network and add states

model = BayesianNetwork()
model.add_states(rain, maintenance, train, appointment)

# Add edges connecting nodes

model.add_edge(rain, maintenance)
model.add_edge(rain, train)
model.add_edge(maintenance, train)
model.add_edge(train, appointment)

# Finalize model

model.bake()

code screenshot

This is a program from CS50 course. Please someone explain why it isn't running on my system.

https://learning.edx.org/course/course-v1:HarvardX+CS50AI+1T2020/block-v1:HarvardX+CS50AI+1T2020+type@sequential+block@c62f675bf7f94f0e91b408cacda56451/block-v1:HarvardX+CS50AI+1T2020+type@vertical+block@1bf36eea064644718f0c5d4f44fc3e29?_gl=1%2A1jbevuj%2A_ga%2AOTI5MTM1MzM4LjE2NjY0MzEzMDI.%2A_ga_D3KS4KMDT0%2AMTY4Nzg4Njc2Ny4xMC4wLjE2ODc4ODY3NjcuNjAuMC4w

2

There are 2 best solutions below

0
On

Try

from pomegranate import Node, DiscreteDistribution, ConditionalProbabilityTable, BayesianNetwork

# Rain node has no parents
rain = Node(DiscreteDistribution({"none": 0.7, "light": 0.2, "heavy": 0.1}), name="rain")

# Track maintenance node is conditional on rain
maintenance = Node(ConditionalProbabilityTable([["none", "yes", 0.4], ["none", "no", 0.6], ["light", "yes", 0.2], ["light", "no", 0.8], ["heavy", "yes", 0.1], ["heavy", "no", 0.9]], [rain.distribution]), name="maintenance")

# Train node is conditional on rain and maintenance
train = Node(ConditionalProbabilityTable([["none", "yes", "on time", 0.8], ["none", "yes", "delayed", 0.2], ["none", "no", "on time", 0.9], ["none", "no", "delayed", 0.1], ["light", "yes", "on time", 0.6],
["light", "yes", "delayed", 0.4], ["light", "no", "on time", 0.7], ["light", "no", "delayed", 0.3],
["heavy", "yes", "on time", 0.4], ["heavy", "yes", "delayed", 0.6], ["heavy", "no", "on time", 0.5], ["heavy", "no", "delayed", 0.5]], [rain.distribution, maintenance.distribution]), name="train")

# Appointment node is conditional on train
appointment = Node(ConditionalProbabilityTable([["on time", "attend", 0.9], ["on time", "miss", 0.1], ["delayed", "attend", 0.6], ["delayed", "miss", 0.4]], [train.distribution]), name="appointment")

# Create a Bayesian Network and add states
model = BayesianNetwork()
model.add_states(rain, maintenance, train, appointment)

# Add edges connecting nodes
model.add_edge(rain, maintenance)
model.add_edge(rain, train)
model.add_edge(maintenance, train)
model.add_edge(train, appointment)

# Finalize model
model.bake()

The classes Node, DiscreteDistribution, ConditionalProbabilityTable, and BayesianNetwork are imported correctly from the pomegranate module. Additionally, the add_states() and add_edge() methods are used to define the network structure.

Please make sure you have the pomegranate library installed in your environment before running the code.

0
On

That course is missing a requirements.txt to lock the dependency versions. Also, it was written before 2023, and before "pomegranate" v1.X.X was released which no longer includes the Node class. See the version history: https://pypi.org/project/pomegranate/#history.

Use pomegranate "v10.14.9" for that and you should be fine:

pip install pomegranate==v0.14.9