Skip to content

Commit b9678f7

Browse files
committed
Pushing the docs to dev/ for branch: master, commit 7b346e784b1391815bd17f64fd239d3fbd78dd5a
1 parent dee1c39 commit b9678f7

File tree

1,076 files changed

+2974
-2974
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,076 files changed

+2974
-2974
lines changed
15 Bytes
Binary file not shown.
15 Bytes
Binary file not shown.

dev/_downloads/plot_grid_search_refit_callable.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"cell_type": "markdown",
1616
"metadata": {},
1717
"source": [
18-
"\n# Balance model complexity and cross-validated score\n\n\nThis example balances model complexity and cross-validated score by\nfinding a decent accuracy within 1 standard deviation of the best accuracy\nscore while minimising the number of PCA components [1].\n\nThe figure shows the trade-off between cross-validated score and the number\nof PCA components. The balanced case is when n_components=6 and accuracy=0.80,\nwhich falls into the range within 1 standard deviation of the best accuracy\nscore.\n\n[1] Hastie, T., Tibshirani, R.,, Friedman, J. (2001). Model Assessment and\nSelection. The Elements of Statistical Learning (pp. 219-260). New York,\nNY, USA: Springer New York Inc..\n\n"
18+
"\n# Balance model complexity and cross-validated score\n\n\nThis example balances model complexity and cross-validated score by\nfinding a decent accuracy within 1 standard deviation of the best accuracy\nscore while minimising the number of PCA components [1].\n\nThe figure shows the trade-off between cross-validated score and the number\nof PCA components. The balanced case is when n_components=10 and accuracy=0.88,\nwhich falls into the range within 1 standard deviation of the best accuracy\nscore.\n\n[1] Hastie, T., Tibshirani, R.,, Friedman, J. (2001). Model Assessment and\nSelection. The Elements of Statistical Learning (pp. 219-260). New York,\nNY, USA: Springer New York Inc..\n\n"
1919
]
2020
},
2121
{
@@ -26,7 +26,7 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"# Author: Wenhao Zhang <[email protected]>\n\nprint(__doc__)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.datasets import load_digits\nfrom sklearn.decomposition import PCA\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.svm import LinearSVC\n\n\ndef lower_bound(cv_results):\n \"\"\"\n Calculate the lower bound within 1 standard deviation\n of the best `mean_test_scores`.\n\n Parameters\n ----------\n cv_results : dict of numpy(masked) ndarrays\n See attribute cv_results_ of `GridSearchCV`\n\n Returns\n -------\n float\n Lower bound within 1 standard deviation of the\n best `mean_test_score`.\n \"\"\"\n best_score_idx = np.argmax(cv_results['mean_test_score'])\n\n return (cv_results['mean_test_score'][best_score_idx]\n - cv_results['std_test_score'][best_score_idx])\n\n\ndef best_low_complexity(cv_results):\n \"\"\"\n Balance model complexity with cross-validated score.\n\n Parameters\n ----------\n cv_results : dict of numpy(masked) ndarrays\n See attribute cv_results_ of `GridSearchCV`.\n\n Return\n ------\n int\n Index of a model that has the fewest PCA components\n while has its test score within 1 standard deviation of the best\n `mean_test_score`.\n \"\"\"\n threshold = lower_bound(cv_results)\n candidate_idx = np.flatnonzero(cv_results['mean_test_score'] >= threshold)\n best_idx = candidate_idx[cv_results['param_reduce_dim__n_components']\n [candidate_idx].argmin()]\n return best_idx\n\n\npipe = Pipeline([\n ('reduce_dim', PCA(random_state=42)),\n ('classify', LinearSVC(random_state=42)),\n])\n\nparam_grid = {\n 'reduce_dim__n_components': [2, 4, 6, 8]\n}\n\ngrid = GridSearchCV(pipe, cv=10, n_jobs=1, param_grid=param_grid,\n scoring='accuracy', refit=best_low_complexity)\nX, y = load_digits(return_X_y=True)\ngrid.fit(X, y)\n\nn_components = grid.cv_results_['param_reduce_dim__n_components']\ntest_scores = grid.cv_results_['mean_test_score']\n\nplt.figure()\nplt.bar(n_components, test_scores, width=1.3, color='b')\n\nlower = lower_bound(grid.cv_results_)\nplt.axhline(np.max(test_scores), linestyle='--', color='y',\n label='Best score')\nplt.axhline(lower, linestyle='--', color='.5', label='Best score - 1 std')\n\nplt.title(\"Balance model complexity and cross-validated score\")\nplt.xlabel('Number of PCA components used')\nplt.ylabel('Digit classification accuracy')\nplt.xticks(n_components.tolist())\nplt.ylim((0, 1.0))\nplt.legend(loc='upper left')\n\nbest_index_ = grid.best_index_\n\nprint(\"The best_index_ is %d\" % best_index_)\nprint(\"The n_components selected is %d\" % n_components[best_index_])\nprint(\"The corresponding accuracy score is %.2f\"\n % grid.cv_results_['mean_test_score'][best_index_])\nplt.show()"
29+
"# Author: Wenhao Zhang <[email protected]>\n\nprint(__doc__)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.datasets import load_digits\nfrom sklearn.decomposition import PCA\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.svm import LinearSVC\n\n\ndef lower_bound(cv_results):\n \"\"\"\n Calculate the lower bound within 1 standard deviation\n of the best `mean_test_scores`.\n\n Parameters\n ----------\n cv_results : dict of numpy(masked) ndarrays\n See attribute cv_results_ of `GridSearchCV`\n\n Returns\n -------\n float\n Lower bound within 1 standard deviation of the\n best `mean_test_score`.\n \"\"\"\n best_score_idx = np.argmax(cv_results['mean_test_score'])\n\n return (cv_results['mean_test_score'][best_score_idx]\n - cv_results['std_test_score'][best_score_idx])\n\n\ndef best_low_complexity(cv_results):\n \"\"\"\n Balance model complexity with cross-validated score.\n\n Parameters\n ----------\n cv_results : dict of numpy(masked) ndarrays\n See attribute cv_results_ of `GridSearchCV`.\n\n Return\n ------\n int\n Index of a model that has the fewest PCA components\n while has its test score within 1 standard deviation of the best\n `mean_test_score`.\n \"\"\"\n threshold = lower_bound(cv_results)\n candidate_idx = np.flatnonzero(cv_results['mean_test_score'] >= threshold)\n best_idx = candidate_idx[cv_results['param_reduce_dim__n_components']\n [candidate_idx].argmin()]\n return best_idx\n\n\npipe = Pipeline([\n ('reduce_dim', PCA(random_state=42)),\n ('classify', LinearSVC(random_state=42, C=0.01)),\n])\n\nparam_grid = {\n 'reduce_dim__n_components': [6, 8, 10, 12, 14]\n}\n\ngrid = GridSearchCV(pipe, cv=10, n_jobs=1, param_grid=param_grid,\n scoring='accuracy', refit=best_low_complexity)\nX, y = load_digits(return_X_y=True)\ngrid.fit(X, y)\n\nn_components = grid.cv_results_['param_reduce_dim__n_components']\ntest_scores = grid.cv_results_['mean_test_score']\n\nplt.figure()\nplt.bar(n_components, test_scores, width=1.3, color='b')\n\nlower = lower_bound(grid.cv_results_)\nplt.axhline(np.max(test_scores), linestyle='--', color='y',\n label='Best score')\nplt.axhline(lower, linestyle='--', color='.5', label='Best score - 1 std')\n\nplt.title(\"Balance model complexity and cross-validated score\")\nplt.xlabel('Number of PCA components used')\nplt.ylabel('Digit classification accuracy')\nplt.xticks(n_components.tolist())\nplt.ylim((0, 1.0))\nplt.legend(loc='upper left')\n\nbest_index_ = grid.best_index_\n\nprint(\"The best_index_ is %d\" % best_index_)\nprint(\"The n_components selected is %d\" % n_components[best_index_])\nprint(\"The corresponding accuracy score is %.2f\"\n % grid.cv_results_['mean_test_score'][best_index_])\nplt.show()"
3030
]
3131
}
3232
],

dev/_downloads/plot_grid_search_refit_callable.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
score while minimising the number of PCA components [1].
99
1010
The figure shows the trade-off between cross-validated score and the number
11-
of PCA components. The balanced case is when n_components=6 and accuracy=0.80,
11+
of PCA components. The balanced case is when n_components=10 and accuracy=0.88,
1212
which falls into the range within 1 standard deviation of the best accuracy
1313
score.
1414
@@ -77,11 +77,11 @@ def best_low_complexity(cv_results):
7777

7878
pipe = Pipeline([
7979
('reduce_dim', PCA(random_state=42)),
80-
('classify', LinearSVC(random_state=42)),
80+
('classify', LinearSVC(random_state=42, C=0.01)),
8181
])
8282

8383
param_grid = {
84-
'reduce_dim__n_components': [2, 4, 6, 8]
84+
'reduce_dim__n_components': [6, 8, 10, 12, 14]
8585
}
8686

8787
grid = GridSearchCV(pipe, cv=10, n_jobs=1, param_grid=param_grid,

dev/_downloads/scikit-learn-docs.pdf

-577 Bytes
Binary file not shown.

dev/_images/iris.png

0 Bytes
674 Bytes
674 Bytes
-251 Bytes
-251 Bytes

0 commit comments

Comments
 (0)