Wie benutze ich Granatapfelfunktionen?Python

Python-Programme
Anonymous
 Wie benutze ich Granatapfelfunktionen?

Post by Anonymous »

Ich versuche, ein Beispiel aus dem CS50 -Kurskurs für künstliche Intelligenz zu führen, an dem das Granatapfelpaket verwendet wird (ein Wahrscheinlichkeitsmodell). Dies ist der Code: < /p>

Code: Select all

from pomegranate import *

class Node():
def __init__(self, distribution, name):
self.distribution = distribution
self.name = name

# 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()

What I tried:
  • pip install pomegranate
  • Updated pip to the latest version
What I expect:

[*]Able to run the code smoothly
< /ol>
Fehler in Traceback: < /p>
rain = Node(DiscreteDistribution({
^^^^^^^^^^^^^^^^^^^^
NameError: name 'DiscreteDistribution' is not defined
< /code>
Obwohl die Diskretedivision als eine der Funktionen in der offiziellen Website von Granatapfels angegeben wurde
gilt für konditionalprobabilitätstable und Bayesiandwork < /p>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post