.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/ande/06_plot_aode_xor.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_06_plot_aode_xor.py: ===================================================== XOR problem: AnDE and Naive Bayes ===================================================== The XOR dataset has two continuous features. Class 0 lies in the first and third quadrants (both features with the same sign) and class 1 in the second and fourth. Each feature taken alone has the same distribution in both classes, centered at 0, so Naive Bayes has nothing to learn from and stays near chance level. :class:`skbn.AnDE` with ``n_dependence=1`` conditions every feature on one other feature, the super-parent, which it discretizes. Given the sign of the super-parent, the other feature does separate the classes, and the ensemble recovers the four quadrants. The figure shows the probability of class 1 for :class:`skbn.MixedNB` and for AnDE, with the training accuracy in the titles. .. GENERATED FROM PYTHON SOURCE LINES 18-102 .. image-sg:: /auto_examples/ande/images/sphx_glr_06_plot_aode_xor_001.png :alt: XOR problem, MixedNB Training accuracy: 0.56, AnDE, $n = 1$ Training accuracy: 0.99 :srcset: /auto_examples/ande/images/sphx_glr_06_plot_aode_xor_001.png :class: sphx-glr-single-img .. code-block:: Python # Author: The scikit-bayes Developers # SPDX-License-Identifier: BSD-3-Clause import matplotlib.pyplot as plt import numpy as np from sklearn.inspection import DecisionBoundaryDisplay from sklearn.metrics import accuracy_score from skbn import AnDE, MixedNB np.random.seed(42) n_samples = 400 X = np.random.randn(n_samples, 2) y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0).astype(int) mnb = MixedNB() mnb.fit(X, y) # The super-parent is discretized into 4 bins and the child is modeled as a # Gaussian conditioned on the bin. ande = AnDE(n_dependence=1, n_bins=4) ande.fit(X, y) models = [mnb, ande] titles = ["MixedNB", "AnDE, $n = 1$"] fig, axes = plt.subplots(1, 2, figsize=(10, 4.8), sharey=True) for ax, model, title in zip(axes, models, titles): acc = accuracy_score(y, model.predict(X)) display = DecisionBoundaryDisplay.from_estimator( model, X, response_method="predict_proba", plot_method="pcolormesh", shading="auto", alpha=0.8, ax=ax, vmin=0, vmax=1, cmap="viridis", ) ax.scatter( X[y == 0, 0], X[y == 0, 1], c="indigo", marker="o", s=25, alpha=0.8, edgecolors="w", linewidth=0.6, label="Class 0", ) ax.scatter( X[y == 1, 0], X[y == 1, 1], c="gold", marker="^", s=25, alpha=0.9, edgecolors="k", linewidth=0.4, label="Class 1", ) ax.set_title(f"{title}\nTraining accuracy: {acc:.2f}") ax.set_xlabel("Feature 1") ax.set_ylabel("") ax.axvline(0, color="white", linestyle="--", alpha=0.3) ax.axhline(0, color="white", linestyle="--", alpha=0.3) axes[0].set_ylabel("Feature 2") fig.colorbar(display.surface_, ax=axes, label="Probability of class 1") fig.legend(*axes[0].get_legend_handles_labels(), loc="outside lower center", ncol=2) fig.suptitle("XOR problem") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.243 seconds) .. _sphx_glr_download_auto_examples_ande_06_plot_aode_xor.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 06_plot_aode_xor.ipynb <06_plot_aode_xor.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 06_plot_aode_xor.py <06_plot_aode_xor.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 06_plot_aode_xor.zip <06_plot_aode_xor.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_