.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/mixture/plot_gmm.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end <sphx_glr_download_auto_examples_mixture_plot_gmm.py>` 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_mixture_plot_gmm.py: ================================= é«˜æ–¯æ··åˆæ¨¡åž‹æ¤åœ†ä½“ ================================= 绘制通过期望最大化( ``GaussianMixture`` 类)和å˜åˆ†æŽ¨æ–(具有狄利克雷过程先验的 ``BayesianGaussianMixture`` ç±»æ¨¡åž‹ï¼‰èŽ·å¾—çš„ä¸¤ä¸ªé«˜æ–¯æ··åˆæ¨¡åž‹çš„置信æ¤åœ†ä½“。 两个模型都å¯ä»¥ä½¿ç”¨äº”ä¸ªç»„ä»¶æ¥æ‹Ÿåˆæ•°æ®ã€‚请注æ„,期望最大化模型将必然使用所有五个组件,而å˜åˆ†æŽ¨æ–模型实际上åªä¼šä½¿ç”¨æ‰€éœ€çš„组件数é‡ä»¥èŽ·å¾—è‰¯å¥½çš„æ‹Ÿåˆã€‚在这里我们å¯ä»¥çœ‹åˆ°ï¼ŒæœŸæœ›æœ€å¤§åŒ–模型会任æ„åœ°åˆ†å‰²ä¸€äº›ç»„ä»¶ï¼Œå› ä¸ºå®ƒè¯•å›¾æ‹Ÿåˆè¿‡å¤šçš„ç»„ä»¶ï¼Œè€Œç‹„åˆ©å…‹é›·è¿‡ç¨‹æ¨¡åž‹ä¼šè‡ªåŠ¨è°ƒæ•´å…¶çŠ¶æ€æ•°é‡ã€‚ è¿™ä¸ªä¾‹åæ²¡æœ‰å±•示出æ¥ï¼Œå› 为我们处于低维空间ä¸ï¼Œä½†ç‹„利克雷过程模型的å¦ä¸€ä¸ªä¼˜ç‚¹æ˜¯ï¼Œå³ä½¿æ¯ä¸ªç°‡çš„示例数é‡å°‘于数æ®çš„维度,由于推æ–算法的æ£åˆ™åŒ–特性,它也å¯ä»¥æœ‰æ•ˆåœ°æ‹Ÿåˆå®Œæ•´çš„åæ–¹å·®çŸ©é˜µã€‚ .. GENERATED FROM PYTHON SOURCE LINES 13-81 .. image-sg:: /auto_examples/mixture/images/sphx_glr_plot_gmm_001.png :alt: Gaussian Mixture, Bayesian Gaussian Mixture with a Dirichlet process prior :srcset: /auto_examples/mixture/images/sphx_glr_plot_gmm_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /app/scikit-learn-main-origin/sklearn/mixture/_base.py:251: ConvergenceWarning: Best performing initialization did not converge. Try different init parameters, or increase max_iter, tol, or check for degenerate data. | .. code-block:: Python import itertools import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np from scipy import linalg from sklearn import mixture color_iter = itertools.cycle(["navy", "c", "cornflowerblue", "gold", "darkorange"]) def plot_results(X, Y_, means, covariances, index, title): splot = plt.subplot(2, 1, 1 + index) for i, (mean, covar, color) in enumerate(zip(means, covariances, color_iter)): v, w = linalg.eigh(covar) v = 2.0 * np.sqrt(2.0) * np.sqrt(v) u = w[0] / linalg.norm(w[0]) # 由于动æ€è§„划ä¸ä¼šä½¿ç”¨å®ƒèƒ½è®¿é—®çš„æ¯ä¸ªç»„ä»¶ï¼Œé™¤éžå®ƒéœ€è¦å®ƒä»¬ï¼Œæˆ‘们ä¸åº”该绘制冗余组件。 if not np.any(Y_ == i): continue plt.scatter(X[Y_ == i, 0], X[Y_ == i, 1], 0.8, color=color) # 绘制æ¤åœ†ä»¥æ˜¾ç¤ºé«˜æ–¯æˆåˆ† angle = np.arctan(u[1] / u[0]) angle = 180.0 * angle / np.pi # convert to degrees ell = mpl.patches.Ellipse(mean, v[0], v[1], angle=180.0 + angle, color=color) ell.set_clip_box(splot.bbox) ell.set_alpha(0.5) splot.add_artist(ell) plt.xlim(-9.0, 5.0) plt.ylim(-3.0, 6.0) plt.xticks(()) plt.yticks(()) plt.title(title) # Number of samples per component # # n_samples = 500 # 生æˆéšæœºæ ·æœ¬ï¼Œä¸¤ä¸ªç»„æˆéƒ¨åˆ† np.random.seed(0) C = np.array([[0.0, -0.1], [1.7, 0.4]]) X = np.r_[ np.dot(np.random.randn(n_samples, 2), C), 0.7 * np.random.randn(n_samples, 2) + np.array([-6, 3]), ] # 使用五个æˆåˆ†é€šè¿‡EM拟åˆé«˜æ–¯æ··åˆæ¨¡åž‹ gmm = mixture.GaussianMixture(n_components=5, covariance_type="full").fit(X) plot_results(X, gmm.predict(X), gmm.means_, gmm.covariances_, 0, "Gaussian Mixture") # 使用五个æˆåˆ†æ‹Ÿåˆç‹„åˆ©å…‹é›·è¿‡ç¨‹é«˜æ–¯æ··åˆæ¨¡åž‹ dpgmm = mixture.BayesianGaussianMixture(n_components=5, covariance_type="full").fit(X) plot_results( X, dpgmm.predict(X), dpgmm.means_, dpgmm.covariances_, 1, "Bayesian Gaussian Mixture with a Dirichlet process prior", ) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.091 seconds) .. _sphx_glr_download_auto_examples_mixture_plot_gmm.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/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/mixture/plot_gmm.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gmm.ipynb <plot_gmm.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gmm.py <plot_gmm.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_gmm.zip <plot_gmm.zip>` .. include:: plot_gmm.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_