Skip to content

Commit 2b55c00

Browse files
committed
Pushing the docs to dev/ for branch: main, commit 0248dabaa75dd82741ca2ee0c0521df576e0d6c6
1 parent 9902b7c commit 2b55c00

File tree

1,366 files changed

+4805
-4710
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,366 files changed

+4805
-4710
lines changed
Binary file not shown.

dev/_downloads/22c1b876aa7bf8b912208cbfed5299c7/plot_gmm_covariances.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def make_ellipses(gmm, ax):
5959
angle = 180 * angle / np.pi # convert to degrees
6060
v = 2.0 * np.sqrt(2.0) * np.sqrt(v)
6161
ell = mpl.patches.Ellipse(
62-
gmm.means_[n, :2], v[0], v[1], angle=180 + angle, color=color
62+
gmm.means_[n, :2], v[0], v[1], 180 + angle, color=color
6363
)
6464
ell.set_clip_box(ax.bbox)
6565
ell.set_alpha(0.5)

dev/_downloads/2d0762e90c243d288bfdebfc1ddb009e/plot_gmm_sin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def plot_results(X, Y, means, covariances, index, title):
6767
# Plot an ellipse to show the Gaussian component
6868
angle = np.arctan(u[1] / u[0])
6969
angle = 180.0 * angle / np.pi # convert to degrees
70-
ell = mpl.patches.Ellipse(mean, v[0], v[1], angle=180.0 + angle, color=color)
70+
ell = mpl.patches.Ellipse(mean, v[0], v[1], 180.0 + angle, color=color)
7171
ell.set_clip_box(splot.bbox)
7272
ell.set_alpha(0.5)
7373
splot.add_artist(ell)

dev/_downloads/471829dadf19abf3dd2b87b08c9ffc92/plot_gmm_covariances.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"# Author: Ron Weiss <[email protected]>, Gael Varoquaux\n# Modified by Thierry Guillemot <[email protected]>\n# License: BSD 3 clause\n\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\n\nimport numpy as np\n\nfrom sklearn import datasets\nfrom sklearn.mixture import GaussianMixture\nfrom sklearn.model_selection import StratifiedKFold\n\ncolors = [\"navy\", \"turquoise\", \"darkorange\"]\n\n\ndef make_ellipses(gmm, ax):\n for n, color in enumerate(colors):\n if gmm.covariance_type == \"full\":\n covariances = gmm.covariances_[n][:2, :2]\n elif gmm.covariance_type == \"tied\":\n covariances = gmm.covariances_[:2, :2]\n elif gmm.covariance_type == \"diag\":\n covariances = np.diag(gmm.covariances_[n][:2])\n elif gmm.covariance_type == \"spherical\":\n covariances = np.eye(gmm.means_.shape[1]) * gmm.covariances_[n]\n v, w = np.linalg.eigh(covariances)\n u = w[0] / np.linalg.norm(w[0])\n angle = np.arctan2(u[1], u[0])\n angle = 180 * angle / np.pi # convert to degrees\n v = 2.0 * np.sqrt(2.0) * np.sqrt(v)\n ell = mpl.patches.Ellipse(\n gmm.means_[n, :2], v[0], v[1], angle=180 + angle, color=color\n )\n ell.set_clip_box(ax.bbox)\n ell.set_alpha(0.5)\n ax.add_artist(ell)\n ax.set_aspect(\"equal\", \"datalim\")\n\n\niris = datasets.load_iris()\n\n# Break up the dataset into non-overlapping training (75%) and testing\n# (25%) sets.\nskf = StratifiedKFold(n_splits=4)\n# Only take the first fold.\ntrain_index, test_index = next(iter(skf.split(iris.data, iris.target)))\n\n\nX_train = iris.data[train_index]\ny_train = iris.target[train_index]\nX_test = iris.data[test_index]\ny_test = iris.target[test_index]\n\nn_classes = len(np.unique(y_train))\n\n# Try GMMs using different types of covariances.\nestimators = {\n cov_type: GaussianMixture(\n n_components=n_classes, covariance_type=cov_type, max_iter=20, random_state=0\n )\n for cov_type in [\"spherical\", \"diag\", \"tied\", \"full\"]\n}\n\nn_estimators = len(estimators)\n\nplt.figure(figsize=(3 * n_estimators // 2, 6))\nplt.subplots_adjust(\n bottom=0.01, top=0.95, hspace=0.15, wspace=0.05, left=0.01, right=0.99\n)\n\n\nfor index, (name, estimator) in enumerate(estimators.items()):\n # Since we have class labels for the training data, we can\n # initialize the GMM parameters in a supervised manner.\n estimator.means_init = np.array(\n [X_train[y_train == i].mean(axis=0) for i in range(n_classes)]\n )\n\n # Train the other parameters using the EM algorithm.\n estimator.fit(X_train)\n\n h = plt.subplot(2, n_estimators // 2, index + 1)\n make_ellipses(estimator, h)\n\n for n, color in enumerate(colors):\n data = iris.data[iris.target == n]\n plt.scatter(\n data[:, 0], data[:, 1], s=0.8, color=color, label=iris.target_names[n]\n )\n # Plot the test data with crosses\n for n, color in enumerate(colors):\n data = X_test[y_test == n]\n plt.scatter(data[:, 0], data[:, 1], marker=\"x\", color=color)\n\n y_train_pred = estimator.predict(X_train)\n train_accuracy = np.mean(y_train_pred.ravel() == y_train.ravel()) * 100\n plt.text(0.05, 0.9, \"Train accuracy: %.1f\" % train_accuracy, transform=h.transAxes)\n\n y_test_pred = estimator.predict(X_test)\n test_accuracy = np.mean(y_test_pred.ravel() == y_test.ravel()) * 100\n plt.text(0.05, 0.8, \"Test accuracy: %.1f\" % test_accuracy, transform=h.transAxes)\n\n plt.xticks(())\n plt.yticks(())\n plt.title(name)\n\nplt.legend(scatterpoints=1, loc=\"lower right\", prop=dict(size=12))\n\n\nplt.show()"
29+
"# Author: Ron Weiss <[email protected]>, Gael Varoquaux\n# Modified by Thierry Guillemot <[email protected]>\n# License: BSD 3 clause\n\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\n\nimport numpy as np\n\nfrom sklearn import datasets\nfrom sklearn.mixture import GaussianMixture\nfrom sklearn.model_selection import StratifiedKFold\n\ncolors = [\"navy\", \"turquoise\", \"darkorange\"]\n\n\ndef make_ellipses(gmm, ax):\n for n, color in enumerate(colors):\n if gmm.covariance_type == \"full\":\n covariances = gmm.covariances_[n][:2, :2]\n elif gmm.covariance_type == \"tied\":\n covariances = gmm.covariances_[:2, :2]\n elif gmm.covariance_type == \"diag\":\n covariances = np.diag(gmm.covariances_[n][:2])\n elif gmm.covariance_type == \"spherical\":\n covariances = np.eye(gmm.means_.shape[1]) * gmm.covariances_[n]\n v, w = np.linalg.eigh(covariances)\n u = w[0] / np.linalg.norm(w[0])\n angle = np.arctan2(u[1], u[0])\n angle = 180 * angle / np.pi # convert to degrees\n v = 2.0 * np.sqrt(2.0) * np.sqrt(v)\n ell = mpl.patches.Ellipse(\n gmm.means_[n, :2], v[0], v[1], 180 + angle, color=color\n )\n ell.set_clip_box(ax.bbox)\n ell.set_alpha(0.5)\n ax.add_artist(ell)\n ax.set_aspect(\"equal\", \"datalim\")\n\n\niris = datasets.load_iris()\n\n# Break up the dataset into non-overlapping training (75%) and testing\n# (25%) sets.\nskf = StratifiedKFold(n_splits=4)\n# Only take the first fold.\ntrain_index, test_index = next(iter(skf.split(iris.data, iris.target)))\n\n\nX_train = iris.data[train_index]\ny_train = iris.target[train_index]\nX_test = iris.data[test_index]\ny_test = iris.target[test_index]\n\nn_classes = len(np.unique(y_train))\n\n# Try GMMs using different types of covariances.\nestimators = {\n cov_type: GaussianMixture(\n n_components=n_classes, covariance_type=cov_type, max_iter=20, random_state=0\n )\n for cov_type in [\"spherical\", \"diag\", \"tied\", \"full\"]\n}\n\nn_estimators = len(estimators)\n\nplt.figure(figsize=(3 * n_estimators // 2, 6))\nplt.subplots_adjust(\n bottom=0.01, top=0.95, hspace=0.15, wspace=0.05, left=0.01, right=0.99\n)\n\n\nfor index, (name, estimator) in enumerate(estimators.items()):\n # Since we have class labels for the training data, we can\n # initialize the GMM parameters in a supervised manner.\n estimator.means_init = np.array(\n [X_train[y_train == i].mean(axis=0) for i in range(n_classes)]\n )\n\n # Train the other parameters using the EM algorithm.\n estimator.fit(X_train)\n\n h = plt.subplot(2, n_estimators // 2, index + 1)\n make_ellipses(estimator, h)\n\n for n, color in enumerate(colors):\n data = iris.data[iris.target == n]\n plt.scatter(\n data[:, 0], data[:, 1], s=0.8, color=color, label=iris.target_names[n]\n )\n # Plot the test data with crosses\n for n, color in enumerate(colors):\n data = X_test[y_test == n]\n plt.scatter(data[:, 0], data[:, 1], marker=\"x\", color=color)\n\n y_train_pred = estimator.predict(X_train)\n train_accuracy = np.mean(y_train_pred.ravel() == y_train.ravel()) * 100\n plt.text(0.05, 0.9, \"Train accuracy: %.1f\" % train_accuracy, transform=h.transAxes)\n\n y_test_pred = estimator.predict(X_test)\n test_accuracy = np.mean(y_test_pred.ravel() == y_test.ravel()) * 100\n plt.text(0.05, 0.8, \"Test accuracy: %.1f\" % test_accuracy, transform=h.transAxes)\n\n plt.xticks(())\n plt.yticks(())\n plt.title(name)\n\nplt.legend(scatterpoints=1, loc=\"lower right\", prop=dict(size=12))\n\n\nplt.show()"
3030
]
3131
}
3232
],
Binary file not shown.

dev/_downloads/82b113d6a139ff1d978ca9188dc8f15c/plot_gmm_sin.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"import itertools\n\nimport numpy as np\nfrom scipy import linalg\nimport matplotlib.pyplot as plt\nimport matplotlib as mpl\n\nfrom sklearn import mixture\n\ncolor_iter = itertools.cycle([\"navy\", \"c\", \"cornflowerblue\", \"gold\", \"darkorange\"])\n\n\ndef plot_results(X, Y, means, covariances, index, title):\n splot = plt.subplot(5, 1, 1 + index)\n for i, (mean, covar, color) in enumerate(zip(means, covariances, color_iter)):\n v, w = linalg.eigh(covar)\n v = 2.0 * np.sqrt(2.0) * np.sqrt(v)\n u = w[0] / linalg.norm(w[0])\n # as the DP will not use every component it has access to\n # unless it needs it, we shouldn't plot the redundant\n # components.\n if not np.any(Y == i):\n continue\n plt.scatter(X[Y == i, 0], X[Y == i, 1], 0.8, color=color)\n\n # Plot an ellipse to show the Gaussian component\n angle = np.arctan(u[1] / u[0])\n angle = 180.0 * angle / np.pi # convert to degrees\n ell = mpl.patches.Ellipse(mean, v[0], v[1], angle=180.0 + angle, color=color)\n ell.set_clip_box(splot.bbox)\n ell.set_alpha(0.5)\n splot.add_artist(ell)\n\n plt.xlim(-6.0, 4.0 * np.pi - 6.0)\n plt.ylim(-5.0, 5.0)\n plt.title(title)\n plt.xticks(())\n plt.yticks(())\n\n\ndef plot_samples(X, Y, n_components, index, title):\n plt.subplot(5, 1, 4 + index)\n for i, color in zip(range(n_components), color_iter):\n # as the DP will not use every component it has access to\n # unless it needs it, we shouldn't plot the redundant\n # components.\n if not np.any(Y == i):\n continue\n plt.scatter(X[Y == i, 0], X[Y == i, 1], 0.8, color=color)\n\n plt.xlim(-6.0, 4.0 * np.pi - 6.0)\n plt.ylim(-5.0, 5.0)\n plt.title(title)\n plt.xticks(())\n plt.yticks(())\n\n\n# Parameters\nn_samples = 100\n\n# Generate random sample following a sine curve\nnp.random.seed(0)\nX = np.zeros((n_samples, 2))\nstep = 4.0 * np.pi / n_samples\n\nfor i in range(X.shape[0]):\n x = i * step - 6.0\n X[i, 0] = x + np.random.normal(0, 0.1)\n X[i, 1] = 3.0 * (np.sin(x) + np.random.normal(0, 0.2))\n\nplt.figure(figsize=(10, 10))\nplt.subplots_adjust(\n bottom=0.04, top=0.95, hspace=0.2, wspace=0.05, left=0.03, right=0.97\n)\n\n# Fit a Gaussian mixture with EM using ten components\ngmm = mixture.GaussianMixture(\n n_components=10, covariance_type=\"full\", max_iter=100\n).fit(X)\nplot_results(\n X, gmm.predict(X), gmm.means_, gmm.covariances_, 0, \"Expectation-maximization\"\n)\n\ndpgmm = mixture.BayesianGaussianMixture(\n n_components=10,\n covariance_type=\"full\",\n weight_concentration_prior=1e-2,\n weight_concentration_prior_type=\"dirichlet_process\",\n mean_precision_prior=1e-2,\n covariance_prior=1e0 * np.eye(2),\n init_params=\"random\",\n max_iter=100,\n random_state=2,\n).fit(X)\nplot_results(\n X,\n dpgmm.predict(X),\n dpgmm.means_,\n dpgmm.covariances_,\n 1,\n \"Bayesian Gaussian mixture models with a Dirichlet process prior \"\n r\"for $\\gamma_0=0.01$.\",\n)\n\nX_s, y_s = dpgmm.sample(n_samples=2000)\nplot_samples(\n X_s,\n y_s,\n dpgmm.n_components,\n 0,\n \"Gaussian mixture with a Dirichlet process prior \"\n r\"for $\\gamma_0=0.01$ sampled with $2000$ samples.\",\n)\n\ndpgmm = mixture.BayesianGaussianMixture(\n n_components=10,\n covariance_type=\"full\",\n weight_concentration_prior=1e2,\n weight_concentration_prior_type=\"dirichlet_process\",\n mean_precision_prior=1e-2,\n covariance_prior=1e0 * np.eye(2),\n init_params=\"kmeans\",\n max_iter=100,\n random_state=2,\n).fit(X)\nplot_results(\n X,\n dpgmm.predict(X),\n dpgmm.means_,\n dpgmm.covariances_,\n 2,\n \"Bayesian Gaussian mixture models with a Dirichlet process prior \"\n r\"for $\\gamma_0=100$\",\n)\n\nX_s, y_s = dpgmm.sample(n_samples=2000)\nplot_samples(\n X_s,\n y_s,\n dpgmm.n_components,\n 1,\n \"Gaussian mixture with a Dirichlet process prior \"\n r\"for $\\gamma_0=100$ sampled with $2000$ samples.\",\n)\n\nplt.show()"
29+
"import itertools\n\nimport numpy as np\nfrom scipy import linalg\nimport matplotlib.pyplot as plt\nimport matplotlib as mpl\n\nfrom sklearn import mixture\n\ncolor_iter = itertools.cycle([\"navy\", \"c\", \"cornflowerblue\", \"gold\", \"darkorange\"])\n\n\ndef plot_results(X, Y, means, covariances, index, title):\n splot = plt.subplot(5, 1, 1 + index)\n for i, (mean, covar, color) in enumerate(zip(means, covariances, color_iter)):\n v, w = linalg.eigh(covar)\n v = 2.0 * np.sqrt(2.0) * np.sqrt(v)\n u = w[0] / linalg.norm(w[0])\n # as the DP will not use every component it has access to\n # unless it needs it, we shouldn't plot the redundant\n # components.\n if not np.any(Y == i):\n continue\n plt.scatter(X[Y == i, 0], X[Y == i, 1], 0.8, color=color)\n\n # Plot an ellipse to show the Gaussian component\n angle = np.arctan(u[1] / u[0])\n angle = 180.0 * angle / np.pi # convert to degrees\n ell = mpl.patches.Ellipse(mean, v[0], v[1], 180.0 + angle, color=color)\n ell.set_clip_box(splot.bbox)\n ell.set_alpha(0.5)\n splot.add_artist(ell)\n\n plt.xlim(-6.0, 4.0 * np.pi - 6.0)\n plt.ylim(-5.0, 5.0)\n plt.title(title)\n plt.xticks(())\n plt.yticks(())\n\n\ndef plot_samples(X, Y, n_components, index, title):\n plt.subplot(5, 1, 4 + index)\n for i, color in zip(range(n_components), color_iter):\n # as the DP will not use every component it has access to\n # unless it needs it, we shouldn't plot the redundant\n # components.\n if not np.any(Y == i):\n continue\n plt.scatter(X[Y == i, 0], X[Y == i, 1], 0.8, color=color)\n\n plt.xlim(-6.0, 4.0 * np.pi - 6.0)\n plt.ylim(-5.0, 5.0)\n plt.title(title)\n plt.xticks(())\n plt.yticks(())\n\n\n# Parameters\nn_samples = 100\n\n# Generate random sample following a sine curve\nnp.random.seed(0)\nX = np.zeros((n_samples, 2))\nstep = 4.0 * np.pi / n_samples\n\nfor i in range(X.shape[0]):\n x = i * step - 6.0\n X[i, 0] = x + np.random.normal(0, 0.1)\n X[i, 1] = 3.0 * (np.sin(x) + np.random.normal(0, 0.2))\n\nplt.figure(figsize=(10, 10))\nplt.subplots_adjust(\n bottom=0.04, top=0.95, hspace=0.2, wspace=0.05, left=0.03, right=0.97\n)\n\n# Fit a Gaussian mixture with EM using ten components\ngmm = mixture.GaussianMixture(\n n_components=10, covariance_type=\"full\", max_iter=100\n).fit(X)\nplot_results(\n X, gmm.predict(X), gmm.means_, gmm.covariances_, 0, \"Expectation-maximization\"\n)\n\ndpgmm = mixture.BayesianGaussianMixture(\n n_components=10,\n covariance_type=\"full\",\n weight_concentration_prior=1e-2,\n weight_concentration_prior_type=\"dirichlet_process\",\n mean_precision_prior=1e-2,\n covariance_prior=1e0 * np.eye(2),\n init_params=\"random\",\n max_iter=100,\n random_state=2,\n).fit(X)\nplot_results(\n X,\n dpgmm.predict(X),\n dpgmm.means_,\n dpgmm.covariances_,\n 1,\n \"Bayesian Gaussian mixture models with a Dirichlet process prior \"\n r\"for $\\gamma_0=0.01$.\",\n)\n\nX_s, y_s = dpgmm.sample(n_samples=2000)\nplot_samples(\n X_s,\n y_s,\n dpgmm.n_components,\n 0,\n \"Gaussian mixture with a Dirichlet process prior \"\n r\"for $\\gamma_0=0.01$ sampled with $2000$ samples.\",\n)\n\ndpgmm = mixture.BayesianGaussianMixture(\n n_components=10,\n covariance_type=\"full\",\n weight_concentration_prior=1e2,\n weight_concentration_prior_type=\"dirichlet_process\",\n mean_precision_prior=1e-2,\n covariance_prior=1e0 * np.eye(2),\n init_params=\"kmeans\",\n max_iter=100,\n random_state=2,\n).fit(X)\nplot_results(\n X,\n dpgmm.predict(X),\n dpgmm.means_,\n dpgmm.covariances_,\n 2,\n \"Bayesian Gaussian mixture models with a Dirichlet process prior \"\n r\"for $\\gamma_0=100$\",\n)\n\nX_s, y_s = dpgmm.sample(n_samples=2000)\nplot_samples(\n X_s,\n y_s,\n dpgmm.n_components,\n 1,\n \"Gaussian mixture with a Dirichlet process prior \"\n r\"for $\\gamma_0=100$ sampled with $2000$ samples.\",\n)\n\nplt.show()"
3030
]
3131
}
3232
],

0 commit comments

Comments
 (0)