Skip to content

Commit 04a7961

Browse files
committed
update nb
1 parent ac1174e commit 04a7961

File tree

2 files changed

+51
-36
lines changed

2 files changed

+51
-36
lines changed

lib/exgboost/plotting/styles.ex

+7
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,11 @@ defmodule EXGBoost.Plotting.Styles do
1515
""" end) |> Enum.join("\n\n")}
1616
</div>
1717
"""
18+
19+
@functions [{:a, "a"}, {:b, "b"}, {:c, "c"}]
20+
for {name, ret} <- @functions do
21+
def unquote(name)() do
22+
unquote(ret)
23+
end
24+
end
1825
end

notebooks/plotting.livemd

+44-36
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
# Plotting in EXGBoost
22

33
```elixir
4-
Mix.install(
5-
[
6-
{:exgboost, "~> 0.5", env: :dev}
7-
]
8-
)
4+
# Mix.install(
5+
# [
6+
# {:exgboost, "~> 0.5", env: :dev}
7+
# ]
8+
# )
9+
10+
Mix.install([
11+
{:exgboost, path: "/Users/andres/Documents/exgboost"},
12+
{:nx, "~> 0.5"},
13+
{:scidata, "~> 0.1"},
14+
{:scholar, "~> 0.1"},
15+
{:kino_vega_lite, "~> 0.1"}
16+
])
917

1018
# This assumed you launch this livebook from its location in the exgboost/notebooks folder
1119
```
@@ -24,24 +32,24 @@ This notebook will go over some of the details of the `EXGBoost.Plotting` module
2432

2533
There are 2 main APIs exposed to control plotting in `EXGBoost`:
2634

27-
- Top-level API (`EXGBoost.plot_tree/2`)
35+
* Top-level API (`EXGBoost.plot_tree/2`)
2836

29-
- Using predefined styles
30-
- Defining custom styles
31-
- Mix of the first 2
37+
* Using predefined styles
38+
* Defining custom styles
39+
* Mix of the first 2
3240

33-
- `EXBoost.Plotting` module API
41+
* `EXBoost.Plotting` module API
3442

35-
- Use the Vega `data` spec defined in `EXGBoost.Plotting.get_data_spec/2`
36-
- Define your own Vega spec using the data from either `EXGBoost.Plotting.to_tabular/1` or some other means
43+
* Use the Vega `data` spec defined in `EXGBoost.get_data_spec/2`
44+
* Define your own Vega spec using the data from either `EXGBoost.Plotting.to_tabular/1` or some other means
3745

3846
We will walk through each of these in detail.
3947

4048
Regardless of which API you choose to use, it is helpful to understand how the plotting module works (althought the higher-level API you choose to work with the less important it becomes).
4149

4250
## Implementation Details
4351

44-
The plotting functionality provided in `EXGBoost` is powered by the [Vega](https://vega.github.io/vega/) JavaScript library and the Elixir [`VegaLite`](https://hexdocs.pm/vega_lite/VegaLite.html) library which provides the piping to interop with the JavaScript libraries. **We do not actually much use the Elixir API provided by the Elixir VegaLite library. It is mainly used for the purposes of rendering.**
52+
The plotting functionality provided in `EXGBoost` is powered by the [Vega](https://vega.github.io/vega/) JavaScript library and the Elixir [`VegaLite`](https://hexdocs.pm/vega_lite/VegaLite.html) library which provides the piping to interop with the JavaScript libraries. **We do not actually much use the Elixir API provided by the Elixir VegaLite library. It is mainly used for the purposes of rendering.**
4553

4654
Vega is a plotting library built on top of the very powerful [D3](https://d3js.org/) JavaScript library. Vega visualizations are defined according to the respective JSON Schema specification. Vega-Lite offers a [reduced schema](https://vega.github.io/schema/vega-lite/v5.json) compared to the [full Vega spec](https://vega.github.io/schema/vega/v5.json). `EXGBoost.Plotting` leverages several transforms which are not available in the reduced Vega-Lite schema, which is the reason for targeting the lower-level API.
4755

@@ -69,7 +77,7 @@ y_test = Nx.tensor(y_test)
6977

7078
## Train Your Booster
7179

72-
Now go ahead and train your booster. We will use `early_stopping_rounds: 1` because we're not interested in the accuracy of the booster for this demonstration (_Note that we need to set `evals` to use early stopping_).
80+
Now go ahead and train your booster. We will use `early_stopping_rounds: 1` because we're not interested in the accuracy of the booster for this demonstration (*Note that we need to set `evals` to use early stopping*).
7381

7482
You will notice that `EXGBoost` also provides an implementation for `Kino.Render` so that `EXGBoost.Booster`s are rendered as a plot by default.
7583

@@ -112,20 +120,20 @@ This API uses [Vega `Mark`s](https://vega.github.io/vega/docs/marks/) to describ
112120

113121
The plot is composed of the following parts:
114122

115-
- Top-level keys: Options controlling parts of the plot outside of direct control of a `Mark`, such as `:padding`, `:autosize`, etc. Accepts any Vega top-level [top-level key](https://vega.github.io/vega/docs/specification/) in addition to several specific to this API (scuh as `:style` and `:depth`).
116-
- `:leaves`: `Mark` specifying the leaf nodes of the tree
117-
- `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
118-
- `:rect`: [Rect Mark](https://vega.github.io/vega/docs/marks/rect/)
119-
- `:splits` `Mark` specifying the split (or inner / decision) nodes of the tree
120-
- `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
121-
- `:rect`: [Rect Mark](https://vega.github.io/vega/docs/marks/rect/)
122-
- `:children`: [Text Mark](https://vega.github.io/vega/docs/marks/text/) for the child count
123-
- `:yes`
124-
- `:path`: [Path Mark](https://vega.github.io/vega/docs/marks/path/)
125-
- `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
126-
- `:no`
127-
- `:path`: [Path Mark](https://vega.github.io/vega/docs/marks/path/)
128-
- `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
123+
* Top-level keys: Options controlling parts of the plot outside of direct control of a `Mark`, such as `:padding`, `:autosize`, etc. Accepts any Vega top-level [top-level key](https://vega.github.io/vega/docs/specification/) in addition to several specific to this API (scuh as `:style` and `:depth`).
124+
* `:leaves`: `Mark` specifying the leaf nodes of the tree
125+
* `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
126+
* `:rect`: [Rect Mark](https://vega.github.io/vega/docs/marks/rect/)
127+
* `:splits` `Mark` specifying the split (or inner / decision) nodes of the tree
128+
* `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
129+
* `:rect`: [Rect Mark](https://vega.github.io/vega/docs/marks/rect/)
130+
* `:children`: [Text Mark](https://vega.github.io/vega/docs/marks/text/) for the child count
131+
* `:yes`
132+
* `:path`: [Path Mark](https://vega.github.io/vega/docs/marks/path/)
133+
* `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
134+
* `:no`
135+
* `:path`: [Path Mark](https://vega.github.io/vega/docs/marks/path/)
136+
* `:text`: [Text Mark](https://vega.github.io/vega/docs/marks/text/)
129137

130138
`EXGBoost.plot_tree/2` defaults to outputting a `VegaLite` struct. If you pass the `:path` option it will save to a file instead.
131139

@@ -146,7 +154,7 @@ EXGBoost.plot_tree(booster, rankdir: :bt)
146154
By default, plotting only shows one (the first) tree, but seeing as a `Booster` is really an ensemble of trees you can choose which tree to plot through the `:index` option, or set to `nil` to have a dropdown box to select the tree.
147155

148156
```elixir
149-
EXGBoost.plot_tree(booster, rankdir: :lr, index: 4)
157+
EXGBoost.plot_tree(booster, rankdir: :lr, index: nil)
150158
```
151159

152160
You'll also notice that the plot is interactive, with support for scrolling, zooming, and collapsing sections of the tree. If you click on a split node you will toggle the visibility of its descendents, and the rest of the tree will fill the canvas.
@@ -340,7 +348,7 @@ For example, if you just want to change the default pre-configured style you can
340348
Mix.install([
341349
{:exgboost, path: Path.join(__DIR__, ".."), env: :dev},
342350
],
343-
config:
351+
config:
344352
[
345353
exgboost: [
346354
plotting: [
@@ -355,7 +363,7 @@ You can also make one-off changes to any of the settings with this method. In ef
355363
<!-- livebook:{"force_markdown":true} -->
356364

357365
```elixir
358-
default_style =
366+
default_style =
359367
[
360368
style: nil,
361369
background: "#3f3f3f",
@@ -390,7 +398,7 @@ You can also make one-off changes to any of the settings with this method. In ef
390398
Mix.install([
391399
{:exgboost, path: Path.join(__DIR__, ".."), env: :dev},
392400
],
393-
config:
401+
config:
394402
[
395403
exgboost: [
396404
plotting: default_style,
@@ -399,7 +407,7 @@ config:
399407
)
400408
```
401409

402-
**NOTE: When you specify a parameter in the configuration, it is merged with the defaults which is different from runtime behavior.**
410+
**NOTE: When you specify a parameter in the configuration, it is merged with the defaults which is different from runtime behavior.**
403411

404412
At any point, you can check what your default settings are by using `EXGBoost.Plotting.get_defaults/0`
405413

@@ -437,8 +445,8 @@ EXGBoost.Plotting.get_data_spec(booster, rankdir: :bt)
437445

438446
The Vega fields which are not included with `get_data_spec/2` and are included in `plot/2` are:
439447

440-
- [Marks](https://vega.github.io/vega/docs/marks/)
441-
- [Scales](https://vega.github.io/vega/docs/scales/)
442-
- [Signals](https://vega.github.io/vega/docs/signals/)
448+
* [Marks](https://vega.github.io/vega/docs/marks/)
449+
* [Scales](https://vega.github.io/vega/docs/scales/)
450+
* [Signals](https://vega.github.io/vega/docs/signals/)
443451

444452
You can make a completely valid plot using only the Data from `get_data_specs/2` and adding the marks you need.

0 commit comments

Comments
 (0)