[Python] How can I run Bayesian Network by pomegrante?

Discussão em 'Python' iniciado por Stack, Abril 20, 2025.

  1. Stack

    Stack Membro Participativo

    I want to calculate the probability by using Bayesian Network on pomegrante. Here is the example of the code in tutorial:

    initialize the state

    calculate the probability

    I think I do the same things in tutorial:

    import torch
    from pomegranate import bayesian_network, distributions
    import numpy as np

    # Create distribution node
    rain = distributions.Categorical(
    probs= torch.tensor([[
    0.7,
    0.2,
    0.1
    ]])
    )

    maintances = distributions.ConditionalCategorical(
    probs= torch.tensor([[
    [0.4, 0.6],
    [0.2, 0.8],
    [0.1, 0.9]
    ]])
    )

    train = distributions.ConditionalCategorical(
    probs= torch.tensor([[
    [0.8, 0.2],
    [0.9, 0.1],
    [0.6, 0.4],
    [0.7, 0.3],
    [0.4, 0.6],
    [0.5, 0.5]
    ]])
    )

    appointment = distributions.ConditionalCategorical(
    probs= torch.tensor([[
    [0.9, 0.1],
    [0.6, 0.4]
    ]])
    )



    # Create a Bayersian Network and add states
    model = bayesian_network.BayesianNetwork()
    model.add_distributions([rain, maintances, train, appointment])

    # Add edge
    model.add_edge(rain, maintances)
    model.add_edge(rain, train)
    model.add_edge(maintances, train)
    model.add_edge(train, appointment)

    X = np.column_stack([
    np.random.randint(0, 3, size=10), # rain: 3 categories
    np.random.randint(0, 2, size=10), # maintances: 2 categories
    np.random.randint(0, 2, size=10), # train: 2 categories
    np.random.randint(0, 2, size=10) # appointment: 2 categories
    ])

    print(model.probability(X))


    Here is the problem when I run the code The message

    Can someone give me the explanation and help me solve this problem?

    Continue reading...

Compartilhe esta Página