BayesNets Structure Learning Exception

I’ve been trying out the BayesNets.jl package. I would like to learn the structure of a bayesian network using a small number of Bool variables in a DataFrame.

First, I set the parameters of the search

data ="/training.csv", truestrings=["TRUE"], falsestrings=["FALSE"])
vars = [:X1, :X2, :X3, :X4, :X5, :X6]
parameters = K2GraphSearch(vars, DiscreteCPD, max_n_parents=2)

Then, I try running the structure search (using a dataframe)

fit(BayesNets.DiscreteBayesNet, data, parameters)

But I get this exception:

ERROR: MethodError: no method matching infer_number_of_instantiations(::CSV.Column2{Bool,Bool})
Closest candidates are:
  infer_number_of_instantiations(::AbstractArray{I,1}) where I<:Int64 at /Users/---/.julia/packages/BayesNets/XwVs4/src/CPDs/utils.jl:62
 [1] fit(::Type{CategoricalCPD{DiscreteNonParametric{Int64,Float64,Base.OneTo{Int64},Array{Float64,1}}}}, ::DataFrame, ::Symbol, ::Array{Symbol,1}) at /Users/----/.julia/packages/BayesNets/XwVs4/src/CPDs/categorical_cpd.jl:128
 [2] fit(::Type{BayesNet{CategoricalCPD{DiscreteNonParametric{Int64,Float64,Base.OneTo{Int64},Array{Float64,1}}}}}, ::DataFrame, ::K2GraphSearch) at /Users/----/.julia/packages/BayesNets/XwVs4/src/learning.jl:254
 [3] top-level scope at /Users/---/Workspace/Julia_EDA/SEBayes.jl:12

I’m having trouble understand what the error is. Any ideas?

Based on the method error with signature infer_number_of_instantiations(::CSV.Column2{Bool,Bool}), it kind of looks like your data object is not actually a DataFrame. What version of CSV.jl are you using?

I was using v0.5.22 of CSV.jl. I upgraded to v0.5.23 and still got the same error.

In case it help someone else, it seems that my issue was due to the column type. For a DiscreteBayesNet, it seems the dataframe needs to have Int column types (rather than Bool). Also, the Ints need to be from 1-2 (instead of 0-1).

After I ran this,

data = DataFrames.mapcols(c -> convert.(Int8, c), data)
data = DataFrames.mapcols(c -> c .+ 1, data)

The “fit” function worked.

1 Like