.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_timeseries/plot_05_mixed_input_structure.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_04_timeseries_plot_05_mixed_input_structure.py: Model-free structure selection with mixed discrete and continuous inputs ======================================================================== Girimonte, Babuška, and Abonyi select the inputs of a nonlinear model *without fitting any model*. Every continuous candidate input is partitioned into a few fuzzy sets by fuzzy c-means, every discrete input into one crisp set per value, and for every subset of inputs the data are decomposed into the product-space segments of these sets. If the subset explains the output, the output varies little inside every segment, and the *total fuzzy variance* :math:`\sigma_t` (the squared error of the singleton fuzzy model that assigns each segment its membership-weighted mean) is small. All subsets up to a maximal size are ranked by :math:`\sigma_t`. Because an irrelevant input also lowers :math:`\sigma_t` a little (more segments, fewer samples each), the *variance–complexity trade-off* :math:`VCT=\sigma_t n_i` is used to compare structures of similar variance: the first dip of the VCT curve marks the structure with only the relevant inputs. The first problem is the mixed one of the paper. The output switches between two nonlinear functions according to a binary input, :math:`y=x_1^2` if :math:`x_2=1` and :math:`y=x_4^2+x_3` if :math:`x_2=0`, with two irrelevant inputs :math:`x_5, x_6`. The second problem is the Friedman function with five relevant and five irrelevant inputs. .. GENERATED FROM PYTHON SOURCE LINES 29-41 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import make_friedman1 from fuzzyclust.plotting import PALETTE from fuzzyclust.timeseries import ( MixedInputStructureSelection, make_mixed_input_system, plot_structure_ranking, select_structure_mixed, ) .. GENERATED FROM PYTHON SOURCE LINES 42-45 Mixed discrete and continuous inputs ------------------------------------ [snippet:fit] .. GENERATED FROM PYTHON SOURCE LINES 45-56 .. code-block:: Python X, y, discrete = make_mixed_input_system(n_samples=1000, noise=0.1, n_dummy=2, random_state=0) sel = MixedInputStructureSelection(n_clusters=3, max_inputs=5, random_state=0).fit(X, y, discrete=discrete) print(sel.ranking_.head(8).to_string()) print("recommended structure:", sel.best_structure_) # [/snippet:fit] fig, ax = plt.subplots(figsize=(7, 3.8)) plot_structure_ranking(sel.ranking_, n_show=30, ax=ax, best=sel.best_structure_) ax.set_title("Variance–complexity trade-off of the 30 best structures (mixed inputs)") plt.tight_layout() .. image-sg:: /auto_examples/04_timeseries/images/sphx_glr_plot_05_mixed_input_structure_001.png :alt: Variance–complexity trade-off of the 30 best structures (mixed inputs) :srcset: /auto_examples/04_timeseries/images/sphx_glr_plot_05_mixed_input_structure_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none inputs n_inputs sigma_t vct rank 1 (x1, x2, x3, x4, x5) 5 22.343321 111.716606 2 (x1, x2, x3, x4, x6) 5 22.418936 112.094679 3 (x1, x2, x3, x4) 4 23.417339 93.669357 4 (x2, x3, x4, x5, x6) 5 57.392114 286.960571 5 (x1, x2, x4, x5, x6) 5 58.015190 290.075950 6 (x2, x3, x4, x5) 4 59.784952 239.139808 7 (x2, x3, x4, x6) 4 60.425002 241.700008 8 (x1, x2, x3, x5, x6) 5 61.083260 305.416299 recommended structure: ('x1', 'x2', 'x3', 'x4') .. GENERATED FROM PYTHON SOURCE LINES 57-63 The three best structures by :math:`\sigma_t` all contain the four true inputs. The two with a dummy input added have a marginally smaller variance but a larger VCT, and the structure :math:`\{x_1,x_2,x_3,x_4\}` is the first dip of the curve, exactly as in the paper. The fuzzy sets of a continuous input and the crisp sets of the discrete one are stored in ``memberships_``: .. GENERATED FROM PYTHON SOURCE LINES 63-78 .. code-block:: Python fig, axes = plt.subplots(1, 2, figsize=(9, 3)) order = np.argsort(X[:, 0]) for i in range(3): axes[0].plot(X[order, 0], sel.memberships_[0][order, i], color=PALETTE[i], lw=1.6) axes[0].set_xlabel("$x_1$") axes[0].set_ylabel("membership") axes[0].set_title("Fuzzy sets of the continuous input $x_1$ (FCM, $c=3$)") for v in (0, 1): axes[1].bar([v], [np.mean(X[:, 1] == v)], color=PALETTE[v], width=0.5) axes[1].set_xticks([0, 1]) axes[1].set_xlabel("$x_2$") axes[1].set_ylabel("fraction of samples") axes[1].set_title("Crisp sets of the discrete input $x_2$") plt.tight_layout() .. image-sg:: /auto_examples/04_timeseries/images/sphx_glr_plot_05_mixed_input_structure_002.png :alt: Fuzzy sets of the continuous input $x_1$ (FCM, $c=3$), Crisp sets of the discrete input $x_2$ :srcset: /auto_examples/04_timeseries/images/sphx_glr_plot_05_mixed_input_structure_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 79-91 Parameter study: number of fuzzy sets, sample size and noise ------------------------------------------------------------ More fuzzy sets per input resolve the nonlinearities better and lower :math:`\sigma_t` for every structure, but they also multiply the number of segments (:math:`\prod_j c_j`), which costs samples and makes dummy inputs look useful. With two sets, the quadratic terms are resolved poorly, and under strong noise the weak input :math:`x_1` is even dropped. With five sets, the :math:`5\cdot2\cdot5\cdot5=250` segments of the true structure hold only a few samples each, and a dummy input creeps in. Three sets and a thousand samples work best for this problem; with 300 samples a dummy creeps in there too. The table lists, for every setting, the structure recommended by the VCT and the one with the lowest :math:`\sigma_t`. .. GENERATED FROM PYTHON SOURCE LINES 91-104 .. code-block:: Python # [snippet:scan] rows = [] for n_clusters in (2, 3, 5): for n_samples, noise in ((1000, 0.1), (300, 0.1), (1000, 0.5)): Xs, ys, disc = make_mixed_input_system(n_samples=n_samples, noise=noise, n_dummy=2, random_state=0) s = MixedInputStructureSelection(n_clusters=n_clusters, max_inputs=5, random_state=0).fit(Xs, ys, discrete=disc) rows.append((n_clusters, n_samples, noise, s.best_structure_, s.ranking_.iloc[0]["inputs"])) # [/snippet:scan] print(f"{'sets':>4} {'N':>5} {'noise':>5} {'recommended (VCT)':<28} {'lowest sigma_t'}") for n_clusters, n_samples, noise, best, lowest in rows: print(f"{n_clusters:>4} {n_samples:>5} {noise:>5} {', '.join(best):<28} {', '.join(lowest)}") .. rst-class:: sphx-glr-script-out .. code-block:: none sets N noise recommended (VCT) lowest sigma_t 2 1000 0.1 x1, x2, x3, x4 x1, x2, x3, x4, x5 2 300 0.1 x1, x2, x3, x4 x1, x2, x3, x4, x6 2 1000 0.5 x2, x3, x4 x1, x2, x3, x4, x5 3 1000 0.1 x1, x2, x3, x4 x1, x2, x3, x4, x5 3 300 0.1 x1, x2, x3, x4, x6 x1, x2, x3, x4, x6 3 1000 0.5 x1, x2, x3, x4 x1, x2, x3, x4, x5 5 1000 0.1 x1, x2, x3, x4, x6 x1, x2, x3, x4, x6 5 300 0.1 x1, x2, x3, x4, x6 x1, x2, x3, x4, x6 5 1000 0.5 x1, x2, x3, x4, x6 x1, x2, x3, x4, x6 .. GENERATED FROM PYTHON SOURCE LINES 105-115 The Friedman function: five relevant inputs among ten ----------------------------------------------------- The function is :math:`y=10\sin(\pi x_1x_2)+20(x_3-0.5)^2+10x_4+5x_5+\epsilon`, with :math:`x_6,\dots,x_{10}` irrelevant. This is the first example of the paper, with 847 candidate structures of up to six inputs. The six best structures by :math:`\sigma_t` are the true one and the true one plus each of the five dummies; the VCT singles out the true one. The weakest relevant input, :math:`x_3`, is the first to be dropped further down the list, as in the paper. .. GENERATED FROM PYTHON SOURCE LINES 115-123 .. code-block:: Python Xf, yf = make_friedman1(n_samples=1000, n_features=10, noise=1.0, random_state=0) ranking = select_structure_mixed(Xf, yf, n_clusters=3, max_inputs=6, random_state=0) print(ranking.head(8).to_string()) fig, ax = plt.subplots(figsize=(7, 3.8)) plot_structure_ranking(ranking, n_show=70, ax=ax, best=("x1", "x2", "x3", "x4", "x5"), label_top=3) ax.set_title("Variance–complexity trade-off of the 70 best structures (Friedman function)") plt.tight_layout() .. image-sg:: /auto_examples/04_timeseries/images/sphx_glr_plot_05_mixed_input_structure_003.png :alt: Variance–complexity trade-off of the 70 best structures (Friedman function) :srcset: /auto_examples/04_timeseries/images/sphx_glr_plot_05_mixed_input_structure_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none inputs n_inputs sigma_t vct rank 1 (x1, x2, x3, x4, x5, x10) 6 3920.427852 23522.567111 2 (x1, x2, x3, x4, x5, x7) 6 3925.533194 23553.199164 3 (x1, x2, x3, x4, x5, x8) 6 3931.964541 23591.787243 4 (x1, x2, x3, x4, x5, x6) 6 3946.218060 23677.308359 5 (x1, x2, x3, x4, x5, x9) 6 3992.949033 23957.694197 6 (x1, x2, x3, x4, x5) 5 4586.116029 22930.580144 7 (x1, x2, x4, x5, x6, x7) 6 4762.372390 28574.234339 8 (x1, x2, x4, x5, x7, x9) 6 4773.333134 28639.998806 .. GENERATED FROM PYTHON SOURCE LINES 124-133 **Take-away.** Fuzzy clustering turns continuous inputs into fuzzy "values", so discrete and continuous inputs can be treated alike and the variance decomposition of a discrete analysis of variance carries over. No model is fitted, the search is exhaustive, and the result is a *ranked list* of structures rather than a single answer. The VCT points to the parsimonious structure among those of similar variance, and the final choice, confirmed by fitting models for a handful of candidates, is left to the user. The method is limited to a moderate number of candidate inputs (about 15) because the number of structures grows combinatorially. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.757 seconds) .. _sphx_glr_download_auto_examples_04_timeseries_plot_05_mixed_input_structure.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/04_timeseries/plot_05_mixed_input_structure.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_mixed_input_structure.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_mixed_input_structure.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_05_mixed_input_structure.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_