.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_decision_boundary.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_04_decision_boundary.py: Plotting the decision boundary ============================== Plot the decision boundary a Soft Decision Tree learns on make_moons. The boundary is smooth rather than axis-aligned and piecewise constant, which is the visible difference between soft and hard splits. Run with: python examples/04_decision_boundary.py Writes examples/output/04_decision_boundary.png .. GENERATED FROM PYTHON SOURCE LINES 15-55 .. image-sg:: /auto_examples/images/sphx_glr_04_decision_boundary_001.png :alt: SoftDecisionTree(depth=4) on make_moons, accuracy 0.840 :srcset: /auto_examples/images/sphx_glr_04_decision_boundary_001.png :class: sphx-glr-single-img .. code-block:: Python from pathlib import Path import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import make_moons from neural_trees import SoftDecisionTree X, y = make_moons(n_samples=400, noise=0.2, random_state=42) model = SoftDecisionTree(depth=4, max_epochs=40, random_state=42) model.fit(X, y) x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5 y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5 xx, yy = np.meshgrid( np.linspace(x_min, x_max, 300), np.linspace(y_min, y_max, 300) ) grid = np.c_[xx.ravel(), yy.ravel()] zz = model.predict_proba(grid)[:, 1].reshape(xx.shape) fig, ax = plt.subplots(figsize=(7, 5)) contour = ax.contourf(xx, yy, zz, levels=20, cmap="RdBu_r", alpha=0.8) ax.contour(xx, yy, zz, levels=[0.5], colors="black", linewidths=1.2) ax.scatter(X[:, 0], X[:, 1], c=y, cmap="RdBu_r", edgecolors="k", s=25) ax.set_title(f"SoftDecisionTree(depth=4) on make_moons, accuracy {model.score(X, y):.3f}") fig.colorbar(contour, ax=ax, label="P(class 1)") # Writing a file only makes sense when this runs as a script. Inside the # documentation gallery there is no __file__ and the figure is captured directly. if "__file__" in globals(): out_path = Path(__file__).parent / "output" / "04_decision_boundary.png" out_path.parent.mkdir(parents=True, exist_ok=True) fig.savefig(out_path, dpi=150, bbox_inches="tight") print(f"Saved {out_path}") else: plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.713 seconds) .. _sphx_glr_download_auto_examples_04_decision_boundary.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 04_decision_boundary.ipynb <04_decision_boundary.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 04_decision_boundary.py <04_decision_boundary.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 04_decision_boundary.zip <04_decision_boundary.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_