Skip to content

Commit fdbe6fb

Browse files
committed
Pushing the docs to dev/ for branch: master, commit e33e84dbf7281c4e090df176b7d82e7152a1078f
1 parent f0bb966 commit fdbe6fb

File tree

962 files changed

+2887
-2887
lines changed

Some content is hidden

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

962 files changed

+2887
-2887
lines changed
4 Bytes
Binary file not shown.
4 Bytes
Binary file not shown.

dev/_downloads/plot_sparse_logistic_regression_mnist.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 time\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn.datasets import fetch_mldata\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.utils import check_random_state\n\nprint(__doc__)\n\n# Author: Arthur Mensch <[email protected]>\n# License: BSD 3 clause\n\n# Turn down for faster convergence\nt0 = time.time()\ntrain_samples = 5000\n\nmnist = fetch_mldata('MNIST original')\nX = mnist.data.astype('float64')\ny = mnist.target\nrandom_state = check_random_state(0)\npermutation = random_state.permutation(X.shape[0])\nX = X[permutation]\ny = y[permutation]\nX = X.reshape((X.shape[0], -1))\n\nX_train, X_test, y_train, y_test = train_test_split(\n X, y, train_size=train_samples, test_size=10000)\n\nscaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)\n\n# Turn up tolerance for faster convergence\nclf = LogisticRegression(C=50 / train_samples,\n multi_class='multinomial',\n penalty='l1', solver='saga', tol=0.1)\nclf.fit(X_train, y_train)\nsparsity = np.mean(clf.coef_ == 0) * 100\nscore = clf.score(X_test, y_test)\n# print('Best C % .4f' % clf.C_)\nprint(\"Sparsity with L1 penalty: %.2f%%\" % sparsity)\nprint(\"Test score with L1 penalty: %.4f\" % score)\n\ncoef = clf.coef_.copy()\nplt.figure(figsize=(10, 5))\nscale = np.abs(coef).max()\nfor i in range(10):\n l1_plot = plt.subplot(2, 5, i + 1)\n l1_plot.imshow(coef[i].reshape(28, 28), interpolation='nearest',\n cmap=plt.cm.RdBu, vmin=-scale, vmax=scale)\n l1_plot.set_xticks(())\n l1_plot.set_yticks(())\n l1_plot.set_xlabel('Class %i' % i)\nplt.suptitle('Classification vector for...')\n\nrun_time = time.time() - t0\nprint('Example run in %.3f s' % run_time)\nplt.show()"
29+
"import time\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn.datasets import fetch_mldata\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.utils import check_random_state\n\nprint(__doc__)\n\n# Author: Arthur Mensch <[email protected]>\n# License: BSD 3 clause\n\n# Turn down for faster convergence\nt0 = time.time()\ntrain_samples = 5000\n\nmnist = fetch_mldata('MNIST original')\nX = mnist.data.astype('float64')\ny = mnist.target\nrandom_state = check_random_state(0)\npermutation = random_state.permutation(X.shape[0])\nX = X[permutation]\ny = y[permutation]\nX = X.reshape((X.shape[0], -1))\n\nX_train, X_test, y_train, y_test = train_test_split(\n X, y, train_size=train_samples, test_size=10000)\n\nscaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)\n\n# Turn up tolerance for faster convergence\nclf = LogisticRegression(C=50. / train_samples,\n multi_class='multinomial',\n penalty='l1', solver='saga', tol=0.1)\nclf.fit(X_train, y_train)\nsparsity = np.mean(clf.coef_ == 0) * 100\nscore = clf.score(X_test, y_test)\n# print('Best C % .4f' % clf.C_)\nprint(\"Sparsity with L1 penalty: %.2f%%\" % sparsity)\nprint(\"Test score with L1 penalty: %.4f\" % score)\n\ncoef = clf.coef_.copy()\nplt.figure(figsize=(10, 5))\nscale = np.abs(coef).max()\nfor i in range(10):\n l1_plot = plt.subplot(2, 5, i + 1)\n l1_plot.imshow(coef[i].reshape(28, 28), interpolation='nearest',\n cmap=plt.cm.RdBu, vmin=-scale, vmax=scale)\n l1_plot.set_xticks(())\n l1_plot.set_yticks(())\n l1_plot.set_xlabel('Class %i' % i)\nplt.suptitle('Classification vector for...')\n\nrun_time = time.time() - t0\nprint('Example run in %.3f s' % run_time)\nplt.show()"
3030
]
3131
}
3232
],

dev/_downloads/plot_sparse_logistic_regression_mnist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
X_test = scaler.transform(X_test)
5353

5454
# Turn up tolerance for faster convergence
55-
clf = LogisticRegression(C=50 / train_samples,
55+
clf = LogisticRegression(C=50. / train_samples,
5656
multi_class='multinomial',
5757
penalty='l1', solver='saga', tol=0.1)
5858
clf.fit(X_train, y_train)

dev/_downloads/plot_tomography_l1_reconstruction.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-
"print(__doc__)\n\n# Author: Emmanuelle Gouillart <[email protected]>\n# License: BSD 3 clause\n\nimport numpy as np\nfrom scipy import sparse\nfrom scipy import ndimage\nfrom sklearn.linear_model import Lasso\nfrom sklearn.linear_model import Ridge\nimport matplotlib.pyplot as plt\n\n\ndef _weights(x, dx=1, orig=0):\n x = np.ravel(x)\n floor_x = np.floor((x - orig) / dx)\n alpha = (x - orig - floor_x * dx) / dx\n return np.hstack((floor_x, floor_x + 1)), np.hstack((1 - alpha, alpha))\n\n\ndef _generate_center_coordinates(l_x):\n X, Y = np.mgrid[:l_x, :l_x].astype(np.float64)\n center = l_x / 2.\n X += 0.5 - center\n Y += 0.5 - center\n return X, Y\n\n\ndef build_projection_operator(l_x, n_dir):\n \"\"\" Compute the tomography design matrix.\n\n Parameters\n ----------\n\n l_x : int\n linear size of image array\n\n n_dir : int\n number of angles at which projections are acquired.\n\n Returns\n -------\n p : sparse matrix of shape (n_dir l_x, l_x**2)\n \"\"\"\n X, Y = _generate_center_coordinates(l_x)\n angles = np.linspace(0, np.pi, n_dir, endpoint=False)\n data_inds, weights, camera_inds = [], [], []\n data_unravel_indices = np.arange(l_x ** 2)\n data_unravel_indices = np.hstack((data_unravel_indices,\n data_unravel_indices))\n for i, angle in enumerate(angles):\n Xrot = np.cos(angle) * X - np.sin(angle) * Y\n inds, w = _weights(Xrot, dx=1, orig=X.min())\n mask = np.logical_and(inds >= 0, inds < l_x)\n weights += list(w[mask])\n camera_inds += list(inds[mask] + i * l_x)\n data_inds += list(data_unravel_indices[mask])\n proj_operator = sparse.coo_matrix((weights, (camera_inds, data_inds)))\n return proj_operator\n\n\ndef generate_synthetic_data():\n \"\"\" Synthetic binary data \"\"\"\n rs = np.random.RandomState(0)\n n_pts = 36\n x, y = np.ogrid[0:l, 0:l]\n mask_outer = (x - l / 2) ** 2 + (y - l / 2) ** 2 < (l / 2) ** 2\n mask = np.zeros((l, l))\n points = l * rs.rand(2, n_pts)\n mask[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1\n mask = ndimage.gaussian_filter(mask, sigma=l / n_pts)\n res = np.logical_and(mask > mask.mean(), mask_outer)\n return np.logical_xor(res, ndimage.binary_erosion(res))\n\n\n# Generate synthetic images, and projections\nl = 128\nproj_operator = build_projection_operator(l, l / 7.)\ndata = generate_synthetic_data()\nproj = proj_operator * data.ravel()[:, np.newaxis]\nproj += 0.15 * np.random.randn(*proj.shape)\n\n# Reconstruction with L2 (Ridge) penalization\nrgr_ridge = Ridge(alpha=0.2)\nrgr_ridge.fit(proj_operator, proj.ravel())\nrec_l2 = rgr_ridge.coef_.reshape(l, l)\n\n# Reconstruction with L1 (Lasso) penalization\n# the best value of alpha was determined using cross validation\n# with LassoCV\nrgr_lasso = Lasso(alpha=0.001)\nrgr_lasso.fit(proj_operator, proj.ravel())\nrec_l1 = rgr_lasso.coef_.reshape(l, l)\n\nplt.figure(figsize=(8, 3.3))\nplt.subplot(131)\nplt.imshow(data, cmap=plt.cm.gray, interpolation='nearest')\nplt.axis('off')\nplt.title('original image')\nplt.subplot(132)\nplt.imshow(rec_l2, cmap=plt.cm.gray, interpolation='nearest')\nplt.title('L2 penalization')\nplt.axis('off')\nplt.subplot(133)\nplt.imshow(rec_l1, cmap=plt.cm.gray, interpolation='nearest')\nplt.title('L1 penalization')\nplt.axis('off')\n\nplt.subplots_adjust(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0,\n right=1)\n\nplt.show()"
29+
"print(__doc__)\n\n# Author: Emmanuelle Gouillart <[email protected]>\n# License: BSD 3 clause\n\nimport numpy as np\nfrom scipy import sparse\nfrom scipy import ndimage\nfrom sklearn.linear_model import Lasso\nfrom sklearn.linear_model import Ridge\nimport matplotlib.pyplot as plt\n\n\ndef _weights(x, dx=1, orig=0):\n x = np.ravel(x)\n floor_x = np.floor((x - orig) / dx)\n alpha = (x - orig - floor_x * dx) / dx\n return np.hstack((floor_x, floor_x + 1)), np.hstack((1 - alpha, alpha))\n\n\ndef _generate_center_coordinates(l_x):\n X, Y = np.mgrid[:l_x, :l_x].astype(np.float64)\n center = l_x / 2.\n X += 0.5 - center\n Y += 0.5 - center\n return X, Y\n\n\ndef build_projection_operator(l_x, n_dir):\n \"\"\" Compute the tomography design matrix.\n\n Parameters\n ----------\n\n l_x : int\n linear size of image array\n\n n_dir : int\n number of angles at which projections are acquired.\n\n Returns\n -------\n p : sparse matrix of shape (n_dir l_x, l_x**2)\n \"\"\"\n X, Y = _generate_center_coordinates(l_x)\n angles = np.linspace(0, np.pi, n_dir, endpoint=False)\n data_inds, weights, camera_inds = [], [], []\n data_unravel_indices = np.arange(l_x ** 2)\n data_unravel_indices = np.hstack((data_unravel_indices,\n data_unravel_indices))\n for i, angle in enumerate(angles):\n Xrot = np.cos(angle) * X - np.sin(angle) * Y\n inds, w = _weights(Xrot, dx=1, orig=X.min())\n mask = np.logical_and(inds >= 0, inds < l_x)\n weights += list(w[mask])\n camera_inds += list(inds[mask] + i * l_x)\n data_inds += list(data_unravel_indices[mask])\n proj_operator = sparse.coo_matrix((weights, (camera_inds, data_inds)))\n return proj_operator\n\n\ndef generate_synthetic_data():\n \"\"\" Synthetic binary data \"\"\"\n rs = np.random.RandomState(0)\n n_pts = 36\n x, y = np.ogrid[0:l, 0:l]\n mask_outer = (x - l / 2.) ** 2 + (y - l / 2.) ** 2 < (l / 2.) ** 2\n mask = np.zeros((l, l))\n points = l * rs.rand(2, n_pts)\n mask[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1\n mask = ndimage.gaussian_filter(mask, sigma=l / n_pts)\n res = np.logical_and(mask > mask.mean(), mask_outer)\n return np.logical_xor(res, ndimage.binary_erosion(res))\n\n\n# Generate synthetic images, and projections\nl = 128\nproj_operator = build_projection_operator(l, l / 7.)\ndata = generate_synthetic_data()\nproj = proj_operator * data.ravel()[:, np.newaxis]\nproj += 0.15 * np.random.randn(*proj.shape)\n\n# Reconstruction with L2 (Ridge) penalization\nrgr_ridge = Ridge(alpha=0.2)\nrgr_ridge.fit(proj_operator, proj.ravel())\nrec_l2 = rgr_ridge.coef_.reshape(l, l)\n\n# Reconstruction with L1 (Lasso) penalization\n# the best value of alpha was determined using cross validation\n# with LassoCV\nrgr_lasso = Lasso(alpha=0.001)\nrgr_lasso.fit(proj_operator, proj.ravel())\nrec_l1 = rgr_lasso.coef_.reshape(l, l)\n\nplt.figure(figsize=(8, 3.3))\nplt.subplot(131)\nplt.imshow(data, cmap=plt.cm.gray, interpolation='nearest')\nplt.axis('off')\nplt.title('original image')\nplt.subplot(132)\nplt.imshow(rec_l2, cmap=plt.cm.gray, interpolation='nearest')\nplt.title('L2 penalization')\nplt.axis('off')\nplt.subplot(133)\nplt.imshow(rec_l1, cmap=plt.cm.gray, interpolation='nearest')\nplt.title('L1 penalization')\nplt.axis('off')\n\nplt.subplots_adjust(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0,\n right=1)\n\nplt.show()"
3030
]
3131
}
3232
],

dev/_downloads/plot_tomography_l1_reconstruction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def generate_synthetic_data():
101101
rs = np.random.RandomState(0)
102102
n_pts = 36
103103
x, y = np.ogrid[0:l, 0:l]
104-
mask_outer = (x - l / 2) ** 2 + (y - l / 2) ** 2 < (l / 2) ** 2
104+
mask_outer = (x - l / 2.) ** 2 + (y - l / 2.) ** 2 < (l / 2.) ** 2
105105
mask = np.zeros((l, l))
106106
points = l * rs.rand(2, n_pts)
107107
mask[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1

dev/_downloads/scikit-learn-docs.pdf

4.2 KB
Binary file not shown.
84 Bytes
84 Bytes
-167 Bytes

0 commit comments

Comments
 (0)