.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_clustering/plot_09_fuzziness_exponent.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_01_clustering_plot_09_fuzziness_exponent.py: The fuzziness exponent m: from crisp cells to uniform memberships ================================================================== The fuzzy c-means functional :math:`J=\sum_i\sum_k\mu_{ik}^m D^2_{ik}` has a single parameter besides the number of clusters: the weighting exponent :math:`m>1`. For fixed prototypes the memberships are .. math:: \mu_{ik} = \frac{1}{\sum_{j=1}^{c}\left(D_{ik}/D_{jk}\right)^{2/(m-1)}}, so :math:`m` controls how fast the membership of a sample decays with its relative distance to the prototypes. As :math:`m\to1` the exponent :math:`2/(m-1)` grows without bound and the partition becomes crisp (k-means); as :math:`m\to\infty` every membership tends to :math:`1/c` and the prototypes collapse onto the grand mean of the data. This example shows the membership functions along a line for several values of :math:`m`, the corresponding partitions of overlapping clusters, and how the partition coefficient, the spread of the prototypes, and the number of iterations depend on :math:`m`. .. GENERATED FROM PYTHON SOURCE LINES 22-28 Membership functions between two fixed prototypes ------------------------------------------------- Two prototypes sit at :math:`v_1=0` and :math:`v_2=1` on a line. The membership of a point :math:`x` in the first cluster depends only on the ratio of its distances to the two prototypes; the plot is the textbook illustration of the role of :math:`m`. .. GENERATED FROM PYTHON SOURCE LINES 28-56 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from fuzzyclust import FCM from fuzzyclust.datasets import make_fuzzy_blobs from fuzzyclust.plotting import PALETTE, plot_membership_contours # [snippet:membership1d] ms = [1.1, 1.5, 2.0, 3.0, 5.0] x = np.linspace(-1.0, 2.0, 601) v = np.array([0.0, 1.0]) fig, ax = plt.subplots(figsize=(6.5, 4.2)) for j, m in enumerate(ms): d2 = (x[:, None] - v[None, :]) ** 2 + 1e-10 # squared distances to v_1, v_2 mu = d2 ** (-1.0 / (m - 1.0)) mu /= mu.sum(axis=1, keepdims=True) ax.plot(x, mu[:, 0], lw=2.0, color=PALETTE[j], label=f"m = {m}") # [/snippet:membership1d] ax.axvline(0.0, color="0.5", lw=0.8, ls="--") ax.axvline(1.0, color="0.5", lw=0.8, ls="--") ax.axhline(0.5, color="0.8", lw=0.8) ax.set_xlabel("$x$") ax.set_ylabel(r"membership $\mu_{1}(x)$ in the cluster of $v_1=0$") ax.set_title(r"membership functions for two prototypes $v_1=0$, $v_2=1$") ax.legend(frameon=False) ax.grid(True, color="0.9") plt.tight_layout() .. image-sg:: /auto_examples/01_clustering/images/sphx_glr_plot_09_fuzziness_exponent_001.png :alt: membership functions for two prototypes $v_1=0$, $v_2=1$ :srcset: /auto_examples/01_clustering/images/sphx_glr_plot_09_fuzziness_exponent_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 57-64 For :math:`m` close to one the function is a step at the midpoint between the prototypes (the Voronoi boundary of k-means); for :math:`m=2` it is the classical bell-like curve :math:`\mu_1(x)=(x-1)^2/((x-1)^2+x^2)`; for large :math:`m` it is flat at :math:`1/2` almost everywhere. The membership is one *only* at the prototype itself and tends to :math:`1/c` far away from both prototypes in either direction, a consequence of the probabilistic constraint :math:`\sum_i\mu_{ik}=1`. .. GENERATED FROM PYTHON SOURCE LINES 66-70 Partitions of overlapping clusters for several m ------------------------------------------------ Three Gaussian clusters whose spread is large relative to the distance between them. .. GENERATED FROM PYTHON SOURCE LINES 70-79 .. code-block:: Python X, y_true = make_fuzzy_blobs(n_clusters=3, n_samples_per_cluster=150, spread=1.3, box=8.0, random_state=3) fig, axes = plt.subplots(1, 3, figsize=(15, 4.4), sharex=True, sharey=True) for ax, m in zip(axes, (1.2, 2.0, 4.0), strict=True): model = FCM(n_clusters=3, m=m, random_state=0).fit(X) plot_membership_contours(model, X, ax=ax, labels=False) ax.set_title(f"FCM with m = {m}") plt.tight_layout() .. image-sg:: /auto_examples/01_clustering/images/sphx_glr_plot_09_fuzziness_exponent_002.png :alt: FCM with m = 1.2, FCM with m = 2.0, FCM with m = 4.0 :srcset: /auto_examples/01_clustering/images/sphx_glr_plot_09_fuzziness_exponent_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 80-86 Quantities that depend on m --------------------------- We fit FCM on a fine grid of exponents and record the partition coefficient :math:`PC=\frac1N\sum_{ik}\mu_{ik}^2`, the mean distance of the prototypes from the grand mean of the data (a measure of how far the prototypes have collapsed), and the number of iterations. .. GENERATED FROM PYTHON SOURCE LINES 86-110 .. code-block:: Python grand_mean = X.mean(axis=0) grid = np.array([1.1, 1.2, 1.3, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0, 5.0, 7.0, 10.0]) pc, spread, iters = [], [], [] for m in grid: model = FCM(n_clusters=3, m=m, random_state=0, tol=1e-6).fit(X) pc.append(np.sum(model.membership_**2) / X.shape[0]) spread.append(np.linalg.norm(model.cluster_centers_ - grand_mean, axis=1).mean()) iters.append(model.n_iter_) fig, axes = plt.subplots(1, 3, figsize=(15, 4.0)) for ax, values, label in zip(axes, (pc, spread, iters), ("partition coefficient PC", "mean distance of prototypes\nfrom the grand mean", "iterations to converge"), strict=True): ax.plot(grid, values, marker="o", color=PALETTE[0], lw=1.8) ax.set_xscale("log") ax.set_xlabel("fuzziness exponent $m$") ax.set_title(label) ax.grid(True, color="0.9") axes[0].axhline(1 / 3, color="0.6", ls="--", lw=0.8) axes[0].text(1.12, 1 / 3 + 0.02, "$1/c$", color="0.4") plt.tight_layout() for m, p, s, it in zip(grid, pc, spread, iters, strict=True): print(f"m = {m:5.2f} PC = {p:.3f} prototype spread = {s:.2f} iterations = {it}") .. image-sg:: /auto_examples/01_clustering/images/sphx_glr_plot_09_fuzziness_exponent_003.png :alt: partition coefficient PC, mean distance of prototypes from the grand mean, iterations to converge :srcset: /auto_examples/01_clustering/images/sphx_glr_plot_09_fuzziness_exponent_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none m = 1.10 PC = 0.981 prototype spread = 3.18 iterations = 19 m = 1.20 PC = 0.969 prototype spread = 3.19 iterations = 16 m = 1.30 PC = 0.954 prototype spread = 3.20 iterations = 15 m = 1.50 PC = 0.909 prototype spread = 3.23 iterations = 14 m = 1.75 PC = 0.829 prototype spread = 3.24 iterations = 14 m = 2.00 PC = 0.743 prototype spread = 3.24 iterations = 15 m = 2.50 PC = 0.602 prototype spread = 3.20 iterations = 18 m = 3.00 PC = 0.511 prototype spread = 3.14 iterations = 22 m = 4.00 PC = 0.420 prototype spread = 3.04 iterations = 29 m = 5.00 PC = 0.382 prototype spread = 2.96 iterations = 37 m = 7.00 PC = 0.354 prototype spread = 2.84 iterations = 41 m = 10.00 PC = 0.342 prototype spread = 2.73 iterations = 47 .. GENERATED FROM PYTHON SOURCE LINES 111-122 **Take-away.** The exponent trades the sharpness of the partition against its robustness: small :math:`m` gives crisp, k-means-like partitions that react to every sample; large :math:`m` gives fuzzy partitions whose prototypes drift toward the grand mean and eventually coincide (the partition coefficient then approaches :math:`1/c`). Above :math:`m\approx2` the iteration count grows with :math:`m`, because the memberships change more slowly; near :math:`m=1` it is also higher, because the nearly crisp partition takes several iterations to settle. :math:`m=2` is the common default, values in :math:`[1.5, 2.5]` are worth trying, and the validity scan can be run over :math:`m` as well as over :math:`c`. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.420 seconds) .. _sphx_glr_download_auto_examples_01_clustering_plot_09_fuzziness_exponent.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/01_clustering/plot_09_fuzziness_exponent.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_09_fuzziness_exponent.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_09_fuzziness_exponent.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_09_fuzziness_exponent.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_