.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_models/plot_07_hinging_tree.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 or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_03_models_plot_07_hinging_tree.py: Hinging-hyperplane regression tree identified by fuzzy c-regression =================================================================== A *hinging hyperplane* (Breiman) is the continuous piecewise-linear function :math:`h(\mathbf{x})=\max(\mathbf{x}_e^T\boldsymbol\theta_1,\ \mathbf{x}_e^T\boldsymbol\theta_2)` or the corresponding :math:`\min`: two planes glued together along the *hinge* :math:`\{\mathbf{x}:\ \mathbf{x}_e^T(\boldsymbol\theta_1-\boldsymbol\theta_2)=0\}`. Kenesei and Abonyi observed that fuzzy c-regression with :math:`c=2` finds two local linear models and their fuzzy operating regions at once, that is, one hinge, provided the two planes cross inside the data (a constraint added to the weighted least-squares step). The sign of the hinge function then splits the data into the two operating regions, each of which is again modeled by a hinge, and so on. The result is a binary regression tree with *oblique* splits whose leaves are hinge functions. No splitting variable has to be chosen, the tree is piecewise linear, and every node reports how well the crisp split agrees with the fuzzy partition. This example fits a surface made of two crossing hinges and studies the depth, the leaf size, and the growth strategy. .. GENERATED FROM PYTHON SOURCE LINES 23-36 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import pandas as pd from fuzzyclust.models import ( FuzzyCRegression, HingingHyperplaneTree, make_piecewise_linear, piecewise_linear_function, plot_hinge_tree_surface, ) from fuzzyclust.plotting import PALETTE .. GENERATED FROM PYTHON SOURCE LINES 37-42 One hinge from fuzzy c-regression (one input) --------------------------------------------- The data are noisy samples of :math:`y=\max(0.8x,\ -1.2x+1)`. FCRM with two models finds the two lines; the hinge is where they cross, and the membership functions of the two models form a soft version of the split. .. GENERATED FROM PYTHON SOURCE LINES 42-67 .. code-block:: Python rng = np.random.RandomState(0) x = rng.uniform(-2, 3, 200) y1d = np.maximum(0.8 * x, -1.2 * x + 1.0) + 0.1 * rng.randn(200) fcrm = FuzzyCRegression(n_clusters=2, n_init=5, random_state=0).fit(x[:, None], y1d) hinge1d = HingingHyperplaneTree(max_depth=0, random_state=0).fit(x[:, None], y1d) grid = np.linspace(-2, 3, 300)[:, None] fig, axes = plt.subplots(2, 1, figsize=(7, 5.6), sharex=True, gridspec_kw={"height_ratios": [2.2, 1]}) axes[0].scatter(x, y1d, s=10, color="0.5", alpha=0.7, label="data") for i in range(2): axes[0].plot(grid[:, 0], fcrm.predict(grid)[:, i], color=PALETTE[i], lw=1.3, ls="--", label=f"FCRM model {i + 1}") axes[0].plot(grid[:, 0], hinge1d.predict(grid), "k-", lw=2.2, label=f"hinge function ({hinge1d.tree_.kind})") x_hinge = -(hinge1d.tree_.theta[0, 1] - hinge1d.tree_.theta[1, 1]) / (hinge1d.tree_.theta[0, 0] - hinge1d.tree_.theta[1, 0]) axes[0].axvline(x_hinge, color="0.3", ls=":", lw=1) axes[0].set_ylabel("y") axes[0].set_title(f"Two FCRM lines form a hinge at x = {x_hinge:.2f} (true 0.5)") axes[0].legend(frameon=False, fontsize=8) U = fcrm.predict_proba(x[:, None], y1d) for i in range(2): axes[1].scatter(x, U[:, i], s=8, color=PALETTE[i]) axes[1].axvline(x_hinge, color="0.3", ls=":", lw=1) axes[1].set_xlabel("x") axes[1].set_ylabel(r"$\mu_{ik}$") axes[1].set_title("FCRM memberships: the fuzzy operating regions of the two lines") plt.tight_layout() .. image-sg:: /auto_examples/03_models/images/sphx_glr_plot_07_hinging_tree_001.png :alt: Two FCRM lines form a hinge at x = 0.49 (true 0.5), FCRM memberships: the fuzzy operating regions of the two lines :srcset: /auto_examples/03_models/images/sphx_glr_plot_07_hinging_tree_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-72 The node condition :math:`\varrho=1-|m_1-\sum_k\mu_{1k}|/\sum_{ik}\mu_{ik}` compares the number of samples the crisp hinge sends to the left with the membership mass of the first model; a value near one means the hinge agrees with the fuzzy partition. .. GENERATED FROM PYTHON SOURCE LINES 72-74 .. code-block:: Python print(f"condition of the root node: {hinge1d.tree_.condition:.3f}, RMSE {hinge1d.tree_.rmse:.3f}") .. rst-class:: sphx-glr-script-out .. code-block:: none condition of the root node: 0.999, RMSE 0.095 .. GENERATED FROM PYTHON SOURCE LINES 75-85 A surface with two crossing hinges ---------------------------------- :func:`~fuzzyclust.models.make_piecewise_linear` samples :math:`y=\max(x_1+x_2,\ 0.5-x_1+0.5x_2)+\max(0,\ x_2-0.5x_1-0.5)`, which consists of four affine pieces separated by two oblique creases. A tree of depth one (a root hinge and two leaf hinges, four planes) could represent the surface exactly if the root hinge coincided with the first crease. However, the root hinge is fitted to all the data, which are not planar on either side of the crease, so it is only approximately right, and deeper trees refine the pieces. .. GENERATED FROM PYTHON SOURCE LINES 85-109 .. code-block:: Python X, y = make_piecewise_linear(n_samples=400, noise=0.05, random_state=0) X_test, _ = make_piecewise_linear(n_samples=2000, noise=0.0, random_state=1) y_test = piecewise_linear_function(X_test) # [snippet:fit] tree = HingingHyperplaneTree(max_depth=2, min_samples_leaf=10, random_state=0).fit(X, y) print(tree.describe(feature_names=["x1", "x2"], digits=3)) print(tree.node_table().round(3).to_string(index=False)) print(f"test RMSE: {np.sqrt(np.mean((tree.predict(X_test) - y_test) ** 2)):.4f}") # [/snippet:fit] fig, axes = plt.subplots(1, 2, figsize=(12, 4.8)) gx, gy = np.meshgrid(np.linspace(-2, 2, 120), np.linspace(-2, 2, 120)) G = np.c_[gx.ravel(), gy.ravel()] cs = axes[0].contourf(gx, gy, piecewise_linear_function(G).reshape(gx.shape), levels=14, cmap="viridis", alpha=0.85) plt.colorbar(cs, ax=axes[0], label="$y$", fraction=0.046, pad=0.03) axes[0].scatter(X[:, 0], X[:, 1], c=y, cmap="viridis", s=8, edgecolor="white", linewidth=0.3) axes[0].set_xlabel("$x_1$") axes[0].set_ylabel("$x_2$") axes[0].set_title("True surface and the 400 noisy samples") plot_hinge_tree_surface(tree, X, ax=axes[1]) axes[1].set_title(f"Hinge tree of depth {tree.depth_} ({tree.n_leaves_} leaves): internal hinges black, leaf hinges white") plt.tight_layout() .. image-sg:: /auto_examples/03_models/images/sphx_glr_plot_07_hinging_tree_002.png :alt: True surface and the 400 noisy samples, Hinge tree of depth 2 (3 leaves): internal hinges black, leaf hinges white :srcset: /auto_examples/03_models/images/sphx_glr_plot_07_hinging_tree_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none node 0 (depth 0, N=400, RMSE=0.306, rho=1.00): y = max(h1, h2) h1 = -1.1*x1 +1.02*x2 +0.988 h2 = +0.633*x1 +1.14*x2 +0.64 if h1 >= h2 (plane 1 active): node 1 (depth 1, N=220, RMSE=0.0673, rho=1.00): y = max(h1, h2) h1 = -1.01*x1 +0.51*x2 +0.507 h2 = -1.45*x1 +1.5*x2 +0.0652 else: node 2 (depth 1, N=180, RMSE=0.259, rho=0.88): y = max(h1, h2) h1 = +0.491*x1 +1.22*x2 +0.708 h2 = +0.968*x1 +1.02*x2 +0.15 if h1 >= h2 (plane 1 active): node 3 (depth 2, N=98, RMSE=0.107, rho=1.00): y = max(h1, h2) h1 = +0.906*x1 +0.845*x2 +0.0962 h2 = +0.517*x1 +1.99*x2 -0.487 else: node 4 (depth 2, N=82, RMSE=0.155, rho=0.66): y = max(h1, h2) h1 = +0.395*x1 +1.15*x2 +1.01 h2 = +1.02*x1 +0.994*x2 -0.00731 node depth kind n_samples rmse condition leaf 0 0 max 400 0.306 0.999 False 1 1 max 220 0.067 0.998 True 2 1 max 180 0.259 0.878 False 3 2 max 98 0.107 0.996 True 4 2 max 82 0.155 0.665 True test RMSE: 0.0832 .. GENERATED FROM PYTHON SOURCE LINES 110-125 The root hinge (thick black line) splits the plane obliquely close to the main crease of the surface. The hinges of the children (thin dashed white lines) model the remaining kinks inside their regions, and one child is split once more. The root reports a condition of one (its crisp split and the fuzzy partition agree), whereas the deepest node has a lower :math:`\varrho`: its two planes fit a region whose boundary the hinge follows only roughly, and this is where the remaining error lies. Parameter study: depth, leaf size, and growth strategy ------------------------------------------------------ A deeper tree has more pieces. ``min_samples_leaf`` prevents splits that leave too few samples for a two-plane fit. ``growth="greedy"`` splits the worst leaf first until ``max_leaves`` leaves exist, which gives unbalanced trees that spend their pieces where the error is. ``leaf_model="linear"`` replaces every leaf hinge by a single plane. .. GENERATED FROM PYTHON SOURCE LINES 125-175 .. code-block:: Python # [snippet:scan] rows = [] for depth in (0, 1, 2, 3, 4): for leaf in (10, 40): t = HingingHyperplaneTree(max_depth=depth, min_samples_leaf=leaf, random_state=0).fit(X, y) rows.append( { "growth": "balanced", "max_depth": depth, "min_samples_leaf": leaf, "leaves": t.n_leaves_, "train RMSE": np.sqrt(np.mean((t.predict(X) - y) ** 2)), "test RMSE": np.sqrt(np.mean((t.predict(X_test) - y_test) ** 2)), } ) for n_leaves in (2, 3, 4, 6, 8): t = HingingHyperplaneTree(max_depth=6, growth="greedy", max_leaves=n_leaves, random_state=0).fit(X, y) rows.append( { "growth": "greedy", "max_depth": t.depth_, "min_samples_leaf": 10, "leaves": t.n_leaves_, "train RMSE": np.sqrt(np.mean((t.predict(X) - y) ** 2)), "test RMSE": np.sqrt(np.mean((t.predict(X_test) - y_test) ** 2)), } ) scan = pd.DataFrame(rows) # [/snippet:scan] print(scan.round(4).to_string(index=False)) fig, ax = plt.subplots(figsize=(6.5, 4)) for i, ((growth, leaf), sub) in enumerate(scan.groupby(["growth", "min_samples_leaf"])): label = f"{growth}, min_samples_leaf={leaf}" if growth == "balanced" else "greedy (max_leaves 2..8)" ax.plot(sub["leaves"], sub["test RMSE"], "o-", color=PALETTE[i], label=label) ax.axhline(0.0, color="0.6", lw=0.8) ax.set_xlabel("number of leaves") ax.set_ylabel("test RMSE (noise-free surface)") ax.set_title("Approximation error versus tree size") ax.legend(frameon=False, fontsize=8) ax.grid(True, color="0.9") plt.tight_layout() linear_leaves = HingingHyperplaneTree(max_depth=2, leaf_model="linear", random_state=0).fit(X, y) print( f"depth 2 with linear leaves: {linear_leaves.n_leaves_} planes, test RMSE " f"{np.sqrt(np.mean((linear_leaves.predict(X_test) - y_test) ** 2)):.4f}" ) .. image-sg:: /auto_examples/03_models/images/sphx_glr_plot_07_hinging_tree_003.png :alt: Approximation error versus tree size :srcset: /auto_examples/03_models/images/sphx_glr_plot_07_hinging_tree_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none growth max_depth min_samples_leaf leaves train RMSE test RMSE balanced 0 10 1 0.3064 0.3002 balanced 0 40 1 0.3064 0.3002 balanced 1 10 2 0.1811 0.1685 balanced 1 40 2 0.1811 0.1685 balanced 2 10 3 0.1011 0.0832 balanced 2 40 3 0.1011 0.0832 balanced 3 10 5 0.0797 0.0643 balanced 3 40 4 0.0911 0.0777 balanced 4 10 7 0.0765 0.0557 balanced 4 40 4 0.0911 0.0777 greedy 1 10 2 0.1811 0.1685 greedy 2 10 3 0.1011 0.0832 greedy 3 10 4 0.0910 0.0709 greedy 4 10 6 0.0772 0.0573 greedy 4 10 7 0.0765 0.0557 depth 2 with linear leaves: 3 planes, test RMSE 0.2647 .. GENERATED FROM PYTHON SOURCE LINES 176-188 **Take-away.** Fuzzy c-regression with two models is a hinge finder. Used recursively, it grows a regression tree whose splits are oblique hinges and whose leaves are piecewise-linear models. The result is a transparent nonlinear model that is directly usable in model-based control, because every leaf is a pair of local linear models. The depth and the greedy leaf budget control the number of pieces. The test error drops steeply with the first splits and then slowly, because every hinge is fitted to data that are only approximately piecewise planar; the greedy growth reaches a given error with fewer leaves than the balanced one. A large ``min_samples_leaf`` stops the growth early, and linear leaves need more splits than hinge leaves. The node condition :math:`\varrho` flags splits where the hinge overrides the fuzzy partition. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.216 seconds) .. _sphx_glr_download_auto_examples_03_models_plot_07_hinging_tree.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/fuzzyclust/main?urlpath=lab/tree/notebooks/auto_examples/03_models/plot_07_hinging_tree.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_07_hinging_tree.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_07_hinging_tree.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_07_hinging_tree.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_