# Help me convert a weird data type into booleans!

**URL:** https://discourse.julialang.org/t/help-me-convert-a-weird-data-type-into-booleans/93704
**Category:** New to Julia
**Tags:** question, dataframes, mlj
**Created:** [January 28, 2023, 6:05pm UTC](https://discourse.julialang.org/t/help-me-convert-a-weird-data-type-into-booleans/93704 "2023-01-28T18:05:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![clouedoc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/clouedoc/32/46324_2.png) [@clouedoc](https://discourse.julialang.org/u/clouedoc)
#### Post date: [January 28, 2023, 6:05pm UTC](https://discourse.julialang.org/t/help-me-convert-a-weird-data-type-into-booleans/93704/1 "2023-01-28T18:05:45Z")

</div>

Hello there,

I’ve been struggling to learn Julia for the last few days.  
I’m especially interested in machine learning.

So, I’ve managed to train a decision tree model, and I’ve been able to make some predictions.

However, they came into the form of categorical distributions!  
I’d like to convert them to booleans to submit my results to Kaggle (data science competition website)

Here’s how they look like:

```julia
julia> predictions
3281-element CategoricalDistributions.UnivariateFiniteVector{Multiclass{2}, Bool, UInt8, Float32}:
 UnivariateFinite{Multiclass{2}}(false=>0.473, true=>0.527)
 UnivariateFinite{Multiclass{2}}(false=>0.805, true=>0.195)
 UnivariateFinite{Multiclass{2}}(false=>0.121, true=>0.879)
 UnivariateFinite{Multiclass{2}}(false=>0.359, true=>0.641)
 ...

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/4/f4ebbc33442fae1d15163e9014d3e648595a8287.jpeg)

Instead I’d like to have an array of booleans to have this as my final result:

```julia
PassengerId,Transported
0013_01,False
0018_01,False
0019_01,False
0021_01,False
0023_01,False
0027_01,False

```

Thanks 🙏

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [January 28, 2023, 6:59pm UTC](https://discourse.julialang.org/t/help-me-convert-a-weird-data-type-into-booleans/93704/2 "2023-01-28T18:59:07Z")

</div>

The following should work:

1. run `pdf.(x, true)` to get probabilities of `true`
2. then choose some cut-off threshold for classification to make a prediction based on the produced probability, e.g. `pdf.(x, true) .> 0.5` (if you want to make classification at 0.5)

---

<div class="post-metadata">

### Author: ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)
#### Post date: [January 28, 2023, 9:05pm UTC](https://discourse.julialang.org/t/help-me-convert-a-weird-data-type-into-booleans/93704/3 "2023-01-28T21:05:24Z")

</div>

If you’re happy with 0.5 thresholding you can also just call `mode.(x)`, or call `predict_mode` instead of `predict` in MLJ (which I assume you are using). The CategoricalDistributions.jl [readme](https://github.com/JuliaAI/CategoricalDistributions.jl#readme) has more. And you may want to look at [Working with Dategorical Data](https://alan-turing-institute.github.io/MLJ.jl/dev/working_with_categorical_data/) section of the MLJ manual.

---

<div class="post-metadata">

### Author: ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)
#### Post date: [January 29, 2023, 7:20am UTC](https://discourse.julialang.org/t/help-me-convert-a-weird-data-type-into-booleans/93704/4 "2023-01-29T07:20:52Z")

</div>

Or you can even wrap your probabilistic model using MLJ’s `BinaryThresholdPredictor` to get a point-predictor and optimise the threshold to minimise your loss by wrapping again using `TunedModel`. There is an example in the [More on Probabilistic Predictors](https://alan-turing-institute.github.io/MLJ.jl/dev/more_on_probabilistic_predictors/) section of the MLJ manual.

---

<div class="post-metadata">

### Author: ![clouedoc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/clouedoc/32/46324_2.png) [@clouedoc](https://discourse.julialang.org/u/clouedoc)
#### Post date: [January 30, 2023, 10:37am UTC](https://discourse.julialang.org/t/help-me-convert-a-weird-data-type-into-booleans/93704/5 "2023-01-30T10:37:35Z")

</div>

@bkamins @ablaom Thank you both for your help.

I ended up wrapping my `EvoTree` with a `BinaryThresholdPredictor`.

Here’s how the final product looks, if anyone passing here is curious:

```julia
EvoTreeClassifier = @load EvoTreeClassifier pkg = EvoTrees verbosity = 0

prob_predictor = EvoTreeClassifier()
point_predictor = BinaryThresholdPredictor(prob_predictor, threshold=0.5)

balanced = BalancedAccuracy(adjusted=true)

r = range(point_predictor, :threshold, lower=0.1, upper=0.9)
tuned_point_predictor = TunedModel(
  point_predictor,
  tuning=RandomSearch(rng=123),
  resampling=CV(nfolds=6),
  range=r,
  measure=balanced,
  n=30,
)
mach2 = machine(tuned_point_predictor, X, y) |> fit!
optimized_point_predictor = report(mach2).best_model
optimized_point_predictor.threshold # 0.260
predict(mach2, X)[1:3] # [1, 1, 0]

e = evaluate!(mach2, resampling=CV(nfolds=6), measure=[balanced, accuracy])
e.measurement[1] # 0.576 ± 0.0263

final_machine = machine(optimized_point_predictor, X, y) |> fit!
evaluate!(final_machine, measure=[balanced, accuracy])

```

I also uploaded my work on [GitHub](https://github.com/clouedoc/kaggle-spaceship-titanic)

---

<div class="post-metadata">

### Author: ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)
#### Post date: [January 30, 2023, 7:37pm UTC](https://discourse.julialang.org/t/help-me-convert-a-weird-data-type-into-booleans/93704/6 "2023-01-30T19:37:15Z")

</div>

Hey that’s cool to see MLJ applied to this Kaggle competition.

I see you’ve chosen to optimise `balanced` rather than `accuracy` but note that the competition is based on the latter. I’m curious if you score any better if you use `accuracy` instead.

---

<div class="post-metadata">

### Author: ![clouedoc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/clouedoc/32/46324_2.png) [@clouedoc](https://discourse.julialang.org/u/clouedoc)
#### Post date: [January 31, 2023, 1:34pm UTC](https://discourse.julialang.org/t/help-me-convert-a-weird-data-type-into-booleans/93704/7 "2023-01-31T13:34:58Z")

</div>

If you know other competitions that are fit for MLJ, I will gladly take a bite at them! Or if you have Julia ML framework recommendations too.

* * *

I’ve tried optimizing for `accuracy` instead just now, and I’ve got a lower score (`0.787` \< `0.788`) which seems pretty logical for me, since we don’t actually know whether the testing dataset is balanced or not. They might be having a slightly unbalanced testing set to reward users who think about balancing, even though it’s not super necessary.

I’ve compared my two `out.csv`. About 2% of my answers changed because of using a balanced model. On the other hand, my score increased by about 0.1%, so I’m uncertain if there’s really an improvement or just pure chance 🙂
