Parameter learning in Python

This example makes use of the Python code in Data Frame Utils.

# __author__ = 'Bayes Server'
# __version__= '0.1'

import pandas as pd
from jpype import *  # pip install jpype1
import data_frame_utils as dfu

def train_network():
    Example code that learns the parameters of a Bayesian network from data.
    network = create_network_structure()  # we manually construct the network here, but it could be loaded from a file
    x = network.getVariables().get("X", True)
    y = network.getVariables().get("Y", True)

    # now learn the parameters from the data in Tutorial 2 - Mixture model

    df = pd.read_csv('mixture_model.csv')  # Note: Saved as csv from the Tutorial data installed with Bayes Server
    dt = dfu.to_data_table(df)

    # We will use the RelevanceTree algorithm here, as it is optimized for parameter learning
    learning = bayes_parameters.ParameterLearning(network, bayes_inference.RelevanceTreeInferenceFactory())
    learning_options = bayes_parameters.ParameterLearningOptions()

    data_reader_command = bayes_data.DataTableDataReaderCommand(dt)

    reader_options = bayes_data.ReaderOptions()  # we do not have a case column in this example

    # here we map variables to database columns
    # in this case the variables and database columns have the same name
    variable_references = [
        bayes_data.VariableReference(x, bayes_data.ColumnValueType.VALUE, x.getName()),
        bayes_data.VariableReference(y, bayes_data.ColumnValueType.VALUE, y.getName())

    # note that although this example only has non temporal data
    # we could have included additional temporal variables and data

    evidence_reader_command = bayes_data.DefaultEvidenceReaderCommand(

    result = learning.learn(evidence_reader_command, learning_options)

    print("Log likelihood = " + str(result.getLogLikelihood()))

def create_network_structure():
    network = bayes.Network()

    node_cluster = bayes.Node("Cluster", ["Cluster1", "Cluster2", "Cluster3"])

    x = bayes.Variable("X", bayes.VariableValueType.CONTINUOUS)
    y = bayes.Variable("Y", bayes.VariableValueType.CONTINUOUS)

    node_position = bayes.Node("Position", [x, y])

    network.getLinks().add(bayes.Link(node_cluster, node_position))

    # at this point the Bayesian network structure is fully specified

    return network

classpath = "C:\\Program Files\\Bayes Server\\Bayes Server 7.14\\API\\Java\\bayesserver-7.14.jar"

startJVM(getDefaultJVMPath(), "-Djava.class.path=%s" % classpath)

bayes = JPackage("com.bayesserver")
bayes_data =
bayes_inference = bayes.inference
bayes_parameters = bayes.learning.parameters