Skip to content

Commit 550815a

Browse files
committed
Pushing the docs to dev/ for branch: main, commit 2dc35b97b0824cb8bc3f17d2382700a5ebf3ab74
1 parent 9a9aa78 commit 550815a

File tree

1,208 files changed

+4601
-4426
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,208 files changed

+4601
-4426
lines changed
Binary file not shown.

dev/_downloads/493307eb257cfb3d4e056ee73a41842e/plot_sparse_cov.ipynb

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,72 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"# author: Gael Varoquaux <[email protected]>\n# License: BSD 3 clause\n# Copyright: INRIA\n\nimport numpy as np\nfrom scipy import linalg\nfrom sklearn.datasets import make_sparse_spd_matrix\nfrom sklearn.covariance import GraphicalLassoCV, ledoit_wolf\nimport matplotlib.pyplot as plt\n\n# #############################################################################\n# Generate the data\nn_samples = 60\nn_features = 20\n\nprng = np.random.RandomState(1)\nprec = make_sparse_spd_matrix(\n n_features, alpha=0.98, smallest_coef=0.4, largest_coef=0.7, random_state=prng\n)\ncov = linalg.inv(prec)\nd = np.sqrt(np.diag(cov))\ncov /= d\ncov /= d[:, np.newaxis]\nprec *= d\nprec *= d[:, np.newaxis]\nX = prng.multivariate_normal(np.zeros(n_features), cov, size=n_samples)\nX -= X.mean(axis=0)\nX /= X.std(axis=0)\n\n# #############################################################################\n# Estimate the covariance\nemp_cov = np.dot(X.T, X) / n_samples\n\nmodel = GraphicalLassoCV()\nmodel.fit(X)\ncov_ = model.covariance_\nprec_ = model.precision_\n\nlw_cov_, _ = ledoit_wolf(X)\nlw_prec_ = linalg.inv(lw_cov_)\n\n# #############################################################################\n# Plot the results\nplt.figure(figsize=(10, 6))\nplt.subplots_adjust(left=0.02, right=0.98)\n\n# plot the covariances\ncovs = [\n (\"Empirical\", emp_cov),\n (\"Ledoit-Wolf\", lw_cov_),\n (\"GraphicalLassoCV\", cov_),\n (\"True\", cov),\n]\nvmax = cov_.max()\nfor i, (name, this_cov) in enumerate(covs):\n plt.subplot(2, 4, i + 1)\n plt.imshow(\n this_cov, interpolation=\"nearest\", vmin=-vmax, vmax=vmax, cmap=plt.cm.RdBu_r\n )\n plt.xticks(())\n plt.yticks(())\n plt.title(\"%s covariance\" % name)\n\n\n# plot the precisions\nprecs = [\n (\"Empirical\", linalg.inv(emp_cov)),\n (\"Ledoit-Wolf\", lw_prec_),\n (\"GraphicalLasso\", prec_),\n (\"True\", prec),\n]\nvmax = 0.9 * prec_.max()\nfor i, (name, this_prec) in enumerate(precs):\n ax = plt.subplot(2, 4, i + 5)\n plt.imshow(\n np.ma.masked_equal(this_prec, 0),\n interpolation=\"nearest\",\n vmin=-vmax,\n vmax=vmax,\n cmap=plt.cm.RdBu_r,\n )\n plt.xticks(())\n plt.yticks(())\n plt.title(\"%s precision\" % name)\n if hasattr(ax, \"set_facecolor\"):\n ax.set_facecolor(\".7\")\n else:\n ax.set_axis_bgcolor(\".7\")\n\n# plot the model selection metric\nplt.figure(figsize=(4, 3))\nplt.axes([0.2, 0.15, 0.75, 0.7])\nplt.plot(model.cv_results_[\"alphas\"], model.cv_results_[\"mean_score\"], \"o-\")\nplt.axvline(model.alpha_, color=\".5\")\nplt.title(\"Model selection\")\nplt.ylabel(\"Cross-validation score\")\nplt.xlabel(\"alpha\")\n\nplt.show()"
29+
"# author: Gael Varoquaux <[email protected]>\n# License: BSD 3 clause\n# Copyright: INRIA"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"## Generate the data\n\n"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"collapsed": false
44+
},
45+
"outputs": [],
46+
"source": [
47+
"import numpy as np\nfrom scipy import linalg\nfrom sklearn.datasets import make_sparse_spd_matrix\n\nn_samples = 60\nn_features = 20\n\nprng = np.random.RandomState(1)\nprec = make_sparse_spd_matrix(\n n_features, alpha=0.98, smallest_coef=0.4, largest_coef=0.7, random_state=prng\n)\ncov = linalg.inv(prec)\nd = np.sqrt(np.diag(cov))\ncov /= d\ncov /= d[:, np.newaxis]\nprec *= d\nprec *= d[:, np.newaxis]\nX = prng.multivariate_normal(np.zeros(n_features), cov, size=n_samples)\nX -= X.mean(axis=0)\nX /= X.std(axis=0)"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"## Estimate the covariance\n\n"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [],
64+
"source": [
65+
"from sklearn.covariance import GraphicalLassoCV, ledoit_wolf\n\nemp_cov = np.dot(X.T, X) / n_samples\n\nmodel = GraphicalLassoCV()\nmodel.fit(X)\ncov_ = model.covariance_\nprec_ = model.precision_\n\nlw_cov_, _ = ledoit_wolf(X)\nlw_prec_ = linalg.inv(lw_cov_)"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"## Plot the results\n\n"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": false
80+
},
81+
"outputs": [],
82+
"source": [
83+
"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(10, 6))\nplt.subplots_adjust(left=0.02, right=0.98)\n\n# plot the covariances\ncovs = [\n (\"Empirical\", emp_cov),\n (\"Ledoit-Wolf\", lw_cov_),\n (\"GraphicalLassoCV\", cov_),\n (\"True\", cov),\n]\nvmax = cov_.max()\nfor i, (name, this_cov) in enumerate(covs):\n plt.subplot(2, 4, i + 1)\n plt.imshow(\n this_cov, interpolation=\"nearest\", vmin=-vmax, vmax=vmax, cmap=plt.cm.RdBu_r\n )\n plt.xticks(())\n plt.yticks(())\n plt.title(\"%s covariance\" % name)\n\n\n# plot the precisions\nprecs = [\n (\"Empirical\", linalg.inv(emp_cov)),\n (\"Ledoit-Wolf\", lw_prec_),\n (\"GraphicalLasso\", prec_),\n (\"True\", prec),\n]\nvmax = 0.9 * prec_.max()\nfor i, (name, this_prec) in enumerate(precs):\n ax = plt.subplot(2, 4, i + 5)\n plt.imshow(\n np.ma.masked_equal(this_prec, 0),\n interpolation=\"nearest\",\n vmin=-vmax,\n vmax=vmax,\n cmap=plt.cm.RdBu_r,\n )\n plt.xticks(())\n plt.yticks(())\n plt.title(\"%s precision\" % name)\n if hasattr(ax, \"set_facecolor\"):\n ax.set_facecolor(\".7\")\n else:\n ax.set_axis_bgcolor(\".7\")"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": null,
89+
"metadata": {
90+
"collapsed": false
91+
},
92+
"outputs": [],
93+
"source": [
94+
"# plot the model selection metric\nplt.figure(figsize=(4, 3))\nplt.axes([0.2, 0.15, 0.75, 0.7])\nplt.plot(model.cv_results_[\"alphas\"], model.cv_results_[\"mean_test_score\"], \"o-\")\nplt.axvline(model.alpha_, color=\".5\")\nplt.title(\"Model selection\")\nplt.ylabel(\"Cross-validation score\")\nplt.xlabel(\"alpha\")\n\nplt.show()"
3095
]
3196
}
3297
],
Binary file not shown.

dev/_downloads/ef716d06d01e43235aa0be61f66bd68d/plot_sparse_cov.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,13 @@
5454
# License: BSD 3 clause
5555
# Copyright: INRIA
5656

57+
# %%
58+
# Generate the data
59+
# -----------------
5760
import numpy as np
5861
from scipy import linalg
5962
from sklearn.datasets import make_sparse_spd_matrix
60-
from sklearn.covariance import GraphicalLassoCV, ledoit_wolf
61-
import matplotlib.pyplot as plt
6263

63-
# #############################################################################
64-
# Generate the data
6564
n_samples = 60
6665
n_features = 20
6766

@@ -79,8 +78,11 @@
7978
X -= X.mean(axis=0)
8079
X /= X.std(axis=0)
8180

82-
# #############################################################################
81+
# %%
8382
# Estimate the covariance
83+
# -----------------------
84+
from sklearn.covariance import GraphicalLassoCV, ledoit_wolf
85+
8486
emp_cov = np.dot(X.T, X) / n_samples
8587

8688
model = GraphicalLassoCV()
@@ -91,8 +93,11 @@
9193
lw_cov_, _ = ledoit_wolf(X)
9294
lw_prec_ = linalg.inv(lw_cov_)
9395

94-
# #############################################################################
96+
# %%
9597
# Plot the results
98+
# ----------------
99+
import matplotlib.pyplot as plt
100+
96101
plt.figure(figsize=(10, 6))
97102
plt.subplots_adjust(left=0.02, right=0.98)
98103

@@ -139,10 +144,12 @@
139144
else:
140145
ax.set_axis_bgcolor(".7")
141146

147+
# %%
148+
142149
# plot the model selection metric
143150
plt.figure(figsize=(4, 3))
144151
plt.axes([0.2, 0.15, 0.75, 0.7])
145-
plt.plot(model.cv_results_["alphas"], model.cv_results_["mean_score"], "o-")
152+
plt.plot(model.cv_results_["alphas"], model.cv_results_["mean_test_score"], "o-")
146153
plt.axvline(model.alpha_, color=".5")
147154
plt.title("Model selection")
148155
plt.ylabel("Cross-validation score")

dev/_downloads/scikit-learn-docs.zip

7.71 KB
Binary file not shown.
-69 Bytes
-335 Bytes
-95 Bytes
9 Bytes
-79 Bytes

0 commit comments

Comments
 (0)