.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_nonparametric_clustering.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_plot_nonparametric_clustering.py: ========================== Non parametric clustering ========================== This example illustrates how we can run nonparametric clustering using GEMINI. .. GENERATED FROM PYTHON SOURCE LINES 8-16 .. code-block:: default import numpy as np from matplotlib import pyplot as plt from sklearn import metrics from gemclus import data from gemclus.nonparametric import CategoricalMMD .. GENERATED FROM PYTHON SOURCE LINES 17-19 Draw samples from a GMM -------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-29 .. code-block:: default # Generate samples on that are simple to separate N = 100 # Number of nodes in the graph # GMM parameters means = np.array([[1, -1], [1, 1], [-1, -1], [-1, 1]])*2 covariances = [np.eye(2)*0.5]*4 X, y = data.draw_gmm(N, means, covariances, np.ones(4) / 4, random_state=1789) .. GENERATED FROM PYTHON SOURCE LINES 30-34 Train the model -------------------------------------------------------------- Create the Non parametric GEMINI clustering model and call the .fit method to optimise the cluster assignment of the nodes .. GENERATED FROM PYTHON SOURCE LINES 36-40 .. code-block:: default model = CategoricalMMD(n_clusters=4, ovo=True, random_state=0, learning_rate=1e-2) y_pred = model.fit_predict(X) .. GENERATED FROM PYTHON SOURCE LINES 41-43 Final Clustering ----------------- .. GENERATED FROM PYTHON SOURCE LINES 45-52 .. code-block:: default plt.scatter(X[:, 0], X[:, 1], c=y_pred) plt.show() ari_score = metrics.adjusted_rand_score(y, y_pred) gemini_score = model.score(X) print(f"Final ARI score: {ari_score:.3f}") print(f"GEMINI score is {gemini_score:.3f}") .. image-sg:: /auto_examples/images/sphx_glr_plot_nonparametric_clustering_001.png :alt: plot nonparametric clustering :srcset: /auto_examples/images/sphx_glr_plot_nonparametric_clustering_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Final ARI score: 0.975 GEMINI score is 3.328 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.393 seconds) .. _sphx_glr_download_auto_examples_plot_nonparametric_clustering.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_nonparametric_clustering.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_nonparametric_clustering.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_