# BayesNets Structure Learning Exception

**URL:** https://discourse.julialang.org/t/bayesnets-structure-learning-exception/34849
**Category:** Machine Learning
**Created:** [February 19, 2020, 3:06pm UTC](https://discourse.julialang.org/t/bayesnets-structure-learning-exception/34849 "2020-02-19T15:06:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![rsteckel](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@rsteckel](https://discourse.julialang.org/u/rsteckel)
#### Post date: [February 19, 2020, 3:06pm UTC](https://discourse.julialang.org/t/bayesnets-structure-learning-exception/34849/1 "2020-02-19T15:06:43Z")

</div>

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

```julia
data = CSV.read("/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)

```julia
fit(BayesNets.DiscreteBayesNet, data, parameters)

```

But I get this exception:

```julia
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
Stacktrace:
 [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?

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [February 19, 2020, 5:04pm UTC](https://discourse.julialang.org/t/bayesnets-structure-learning-exception/34849/2 "2020-02-19T17:04:33Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![rsteckel](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@rsteckel](https://discourse.julialang.org/u/rsteckel)
#### Post date: [February 19, 2020, 6:01pm UTC](https://discourse.julialang.org/t/bayesnets-structure-learning-exception/34849/3 "2020-02-19T18:01:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![rsteckel](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@rsteckel](https://discourse.julialang.org/u/rsteckel)
#### Post date: [February 20, 2020, 5:43pm UTC](https://discourse.julialang.org/t/bayesnets-structure-learning-exception/34849/4 "2020-02-20T17:43:27Z")

</div>

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,

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

```

The “fit” function worked.
