python-3.x 有人能更正密码吗?它说NameError:未定义名称“Node”

e0bqpujr  于 2023-07-01  发布在  Python
关注(0)|答案(1)|浏览(141)
  1. # Also the terms Node, DiscreteDistribution, ConditionalProbabilityTable, distribution, BayesianNetwork(), add_edge, bake are not being recognised.
  2. from pomegranate import \*
  3. # Rain node has no parents
  4. rain = Node(DiscreteDistribution({"none": 0.7,"light": 0.2,"heavy": 0.1}), name="rain")
  5. # Track maintenance node is conditional on rain
  6. 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")
  7. # Train node is conditional on rain and maintenance
  8. 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\],
  9. \["light", "yes", "delayed", 0.4\],\["light", "no", "on time", 0.7\],\["light", "no", "delayed", 0.3\],
  10. \["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")
  11. # Appointment node is conditional on train
  12. 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")
  13. # Create a Bayesian Network and add states
  14. model = BayesianNetwork()
  15. model.add_states(rain, maintenance, train, appointment)
  16. # Add edges connecting nodes
  17. model.add_edge(rain, maintenance)
  18. model.add_edge(rain, train)
  19. model.add_edge(maintenance, train)
  20. model.add_edge(train, appointment)
  21. # Finalize model
  22. model.bake()

[ code screenshot ](https://i.stack.imgur.com/j7oTn.png
这是CS50课程的一个程序。请有人解释为什么它不能在我的系统上运行。
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

tyu7yeag

tyu7yeag1#

试试看

  1. from pomegranate import Node, DiscreteDistribution, ConditionalProbabilityTable, BayesianNetwork
  2. # Rain node has no parents
  3. rain = Node(DiscreteDistribution({"none": 0.7, "light": 0.2, "heavy": 0.1}), name="rain")
  4. # Track maintenance node is conditional on rain
  5. 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")
  6. # Train node is conditional on rain and maintenance
  7. 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],
  8. ["light", "yes", "delayed", 0.4], ["light", "no", "on time", 0.7], ["light", "no", "delayed", 0.3],
  9. ["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")
  10. # Appointment node is conditional on train
  11. 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")
  12. # Create a Bayesian Network and add states
  13. model = BayesianNetwork()
  14. model.add_states(rain, maintenance, train, appointment)
  15. # Add edges connecting nodes
  16. model.add_edge(rain, maintenance)
  17. model.add_edge(rain, train)
  18. model.add_edge(maintenance, train)
  19. model.add_edge(train, appointment)
  20. # Finalize model
  21. model.bake()

类Node、DiscreteDistribution、ConditionalProbabilityTable和BayesianNetwork从石榴模块正确导入。此外,add_states()add_edge()方法用于定义网络结构。
在运行代码之前,请确保您的环境中安装了石榴库。

展开查看全部

相关问题