.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/ande/11_plot_order_cost.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_ande_11_plot_order_cost.py: ===================================================== Cost of the order of dependence ===================================================== An AnDE of order $n$ has one component for every set of $n$ attributes, so an ensemble over $d$ attributes has $\binom{d}{n}$ components: - $n = 0$: one component (Naive Bayes). - $n = 1$: $d$ components. - $n = 2$: $d(d-1)/2$ components. Each component also has more parameters as the order grows, because its tables are indexed by the values of $n$ super-parents. :class:`skbn.AnDE` is fitted here at orders 0, 1 and 2 on datasets with a growing number of attributes, and the figure shows the number of components and the training time of each. The time of orders 0 and 1 barely changes over this range, while that of order 2 grows with its number of components. .. GENERATED FROM PYTHON SOURCE LINES 21-89 .. image-sg:: /auto_examples/ande/images/sphx_glr_11_plot_order_cost_001.png :alt: Cost of AnDE by order of dependence, 2000 training instances :srcset: /auto_examples/ande/images/sphx_glr_11_plot_order_cost_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none d = 5 components: [1, 5, 10] fit time (s): [0.001, 0.001, 0.001] d = 10 components: [1, 10, 45] fit time (s): [0.001, 0.001, 0.002] d = 15 components: [1, 15, 105] fit time (s): [0.001, 0.002, 0.004] d = 20 components: [1, 20, 190] fit time (s): [0.001, 0.002, 0.008] d = 30 components: [1, 30, 435] fit time (s): [0.001, 0.002, 0.028] d = 40 components: [1, 40, 780] fit time (s): [0.002, 0.003, 0.071] | .. code-block:: Python # Author: The scikit-bayes Developers # SPDX-License-Identifier: BSD-3-Clause import time import matplotlib.pyplot as plt import numpy as np from matplotlib.ticker import NullLocator, StrMethodFormatter from skbn import AnDE rng = np.random.RandomState(42) n_samples = 2000 n_features_grid = [5, 10, 15, 20, 30, 40] orders = [0, 1, 2] n_repeats = 3 X_full = rng.randint(0, 4, size=(n_samples, max(n_features_grid))) y = rng.randint(0, 3, size=n_samples) n_components = {n: [] for n in orders} fit_times = {n: [] for n in orders} for d in n_features_grid: X = X_full[:, :d] for n in orders: model = AnDE(n_dependence=n, categorical_features=list(range(d))) times = [] for _ in range(n_repeats): t0 = time.perf_counter() model.fit(X, y) times.append(time.perf_counter() - t0) n_components[n].append(len(model.ensemble_)) fit_times[n].append(min(times)) print( f"d = {d:2d} components: {[n_components[n][-1] for n in orders]} " f"fit time (s): {[round(fit_times[n][-1], 3) for n in orders]}" ) # Label, colour and marker of each order styles = { 0: ("$n = 0$ (Naive Bayes)", "#6E6E6E", "s"), 1: ("$n = 1$ (A1DE)", "#0072B2", "o"), 2: ("$n = 2$ (A2DE)", "#56B4E9", "^"), } fig, (ax_comp, ax_time) = plt.subplots(1, 2, figsize=(10, 4.2)) for n in orders: label, color, marker = styles[n] ax_comp.plot( n_features_grid, n_components[n], color=color, marker=marker, label=label ) ax_time.plot(n_features_grid, fit_times[n], color=color, marker=marker) for ax in (ax_comp, ax_time): ax.set_xlabel("Number of attributes") ax.set_xticks(n_features_grid) ax.set_yscale("log") ax.yaxis.set_major_formatter(StrMethodFormatter("{x:g}")) ax.yaxis.set_minor_locator(NullLocator()) ax_comp.set_ylabel("Components in the ensemble") ax_time.set_ylabel("Training time (s)") fig.legend(*ax_comp.get_legend_handles_labels(), loc="outside lower center", ncol=3) fig.suptitle(f"Cost of AnDE by order of dependence, {n_samples} training instances") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.754 seconds) .. _sphx_glr_download_auto_examples_ande_11_plot_order_cost.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 11_plot_order_cost.ipynb <11_plot_order_cost.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 11_plot_order_cost.py <11_plot_order_cost.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 11_plot_order_cost.zip <11_plot_order_cost.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_