{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Density Estimation for a Gaussian mixture\n\nPlot the density estimation of a mixture of two Gaussians. Data is\ngenerated from two Gaussians with different centers and covariance\nmatrices.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.colors import LogNorm\n\nfrom sklearn import mixture\n\nn_samples = 300\n\n# generate random sample, two components\nnp.random.seed(0)\n\n# generate spherical data centered on (20, 20)\nshifted_gaussian = np.random.randn(n_samples, 2) + np.array([20, 20])\n\n# generate zero centered stretched Gaussian data\nC = np.array([[0.0, -0.7], [3.5, 0.7]])\nstretched_gaussian = np.dot(np.random.randn(n_samples, 2), C)\n\n# concatenate the two datasets into the final training set\nX_train = np.vstack([shifted_gaussian, stretched_gaussian])\n\n# fit a Gaussian Mixture Model with two components\nclf = mixture.GaussianMixture(n_components=2, covariance_type=\"full\")\nclf.fit(X_train)\n\n# display predicted scores by the model as a contour plot\nx = np.linspace(-20.0, 30.0)\ny = np.linspace(-20.0, 40.0)\nX, Y = np.meshgrid(x, y)\nXX = np.array([X.ravel(), Y.ravel()]).T\nZ = -clf.score_samples(XX)\nZ = Z.reshape(X.shape)\n\nCS = plt.contour(\n    X, Y, Z, norm=LogNorm(vmin=1.0, vmax=1000.0), levels=np.logspace(0, 3, 10)\n)\nCB = plt.colorbar(CS, shrink=0.8, extend=\"both\")\nplt.scatter(X_train[:, 0], X_train[:, 1], 0.8)\n\nplt.title(\"Negative log-likelihood predicted by a GMM\")\nplt.axis(\"tight\")\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.14"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}