Skip to content

Commit a2bfaea

Browse files
committed
Pushing the docs to dev/ for branch: master, commit 1641f31cfa87727f23c7d63aaa2e17ad981da84d
1 parent 59a236e commit a2bfaea

File tree

1,087 files changed

+4471
-3310
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,087 files changed

+4471
-3310
lines changed
7.7 KB
Binary file not shown.
5.65 KB
Binary file not shown.

dev/_downloads/plot_cv_indices.ipynb

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"%matplotlib inline"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"\nVisualizing cross-validation behavior in scikit-learn\n=====================================================\n\nChoosing the right cross-validation object is a crucial part of fitting a\nmodel properly. There are many ways to split data into training and test\nsets in order to avoid model overfitting, to standardize the number of\ngroups in test sets, etc.\n\nThis example visualizes the behavior of several common scikit-learn objects\nfor comparison.\n\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [],
28+
"source": [
29+
"from sklearn.model_selection import (TimeSeriesSplit, KFold, ShuffleSplit,\n StratifiedKFold, GroupShuffleSplit,\n GroupKFold, StratifiedShuffleSplit)\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.patches import Patch\nnp.random.seed(1338)\ncmap_data = plt.cm.Paired\ncmap_cv = plt.cm.coolwarm\nn_splits = 4"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"Visualize our data\n------------------\n\nFirst, we must understand the structure of our data. It has 100 randomly\ngenerated input datapoints, 3 classes split unevenly across datapoints,\nand 10 \"groups\" split evenly across datapoints.\n\nAs we'll see, some cross-validation objects do specific things with\nlabeled data, others behave differently with grouped data, and others\ndo not use this information.\n\nTo begin, we'll visualize our 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+
"# Generate the class/group data\nn_points = 100\nX = np.random.randn(100, 10)\n\npercentiles_classes = [.1, .3, .6]\ny = np.hstack([[ii] * int(100 * perc)\n for ii, perc in enumerate(percentiles_classes)])\n\n# Evenly spaced groups repeated once\ngroups = np.hstack([[ii] * 10 for ii in range(10)])\n\n\ndef visualize_groups(classes, groups, name):\n # Visualize dataset groups\n fig, ax = plt.subplots()\n ax.scatter(range(len(groups)), [.5] * len(groups), c=groups, marker='_',\n lw=50, cmap=cmap_data)\n ax.scatter(range(len(groups)), [3.5] * len(groups), c=classes, marker='_',\n lw=50, cmap=cmap_data)\n ax.set(ylim=[-1, 5], yticks=[.5, 3.5],\n yticklabels=['Data\\ngroup', 'Data\\nclass'], xlabel=\"Sample index\")\n\n\nvisualize_groups(y, groups, 'no groups')"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"Define a function to visualize cross-validation behavior\n--------------------------------------------------------\n\nWe'll define a function that lets us visualize the behavior of each\ncross-validation object. We'll perform 4 splits of the data. On each\nsplit, we'll visualize the indices chosen for the training set\n(in blue) and the test set (in red).\n\n"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [],
64+
"source": [
65+
"def plot_cv_indices(cv, X, y, group, ax, n_splits, lw=10):\n \"\"\"Create a sample plot for indices of a cross-validation object.\"\"\"\n\n # Generate the training/testing visualizations for each CV split\n for ii, (tr, tt) in enumerate(cv.split(X=X, y=y, groups=group)):\n # Fill in indices with the training/test groups\n indices = np.array([np.nan] * len(X))\n indices[tt] = 1\n indices[tr] = 0\n\n # Visualize the results\n ax.scatter(range(len(indices)), [ii + .5] * len(indices),\n c=indices, marker='_', lw=lw, cmap=cmap_cv,\n vmin=-.2, vmax=1.2)\n\n # Plot the data classes and groups at the end\n ax.scatter(range(len(X)), [ii + 1.5] * len(X),\n c=y, marker='_', lw=lw, cmap=cmap_data)\n\n ax.scatter(range(len(X)), [ii + 2.5] * len(X),\n c=group, marker='_', lw=lw, cmap=cmap_data)\n\n # Formatting\n yticklabels = list(range(n_splits)) + ['class', 'group']\n ax.set(yticks=np.arange(n_splits+2) + .5, yticklabels=yticklabels,\n xlabel='Sample index', ylabel=\"CV iteration\",\n ylim=[n_splits+2.2, -.2], xlim=[0, 100])\n ax.set_title('{}'.format(type(cv).__name__), fontsize=15)\n return ax"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"Let's see how it looks for the `KFold` cross-validation object:\n\n"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": false
80+
},
81+
"outputs": [],
82+
"source": [
83+
"fig, ax = plt.subplots()\ncv = KFold(n_splits)\nplot_cv_indices(cv, X, y, groups, ax, n_splits)"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"As you can see, by default the KFold cross-validation iterator does not\ntake either datapoint class or group into consideration. We can change this\nby using the ``StratifiedKFold`` like so.\n\n"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {
97+
"collapsed": false
98+
},
99+
"outputs": [],
100+
"source": [
101+
"fig, ax = plt.subplots()\ncv = StratifiedKFold(n_splits)\nplot_cv_indices(cv, X, y, groups, ax, n_splits)"
102+
]
103+
},
104+
{
105+
"cell_type": "markdown",
106+
"metadata": {},
107+
"source": [
108+
"In this case, the cross-validation retained the same ratio of classes across\neach CV split. Next we'll visualize this behavior for a number of CV\niterators.\n\nVisualize cross-validation indices for many CV objects\n------------------------------------------------------\n\nLet's visually compare the cross validation behavior for many\nscikit-learn cross-validation objects. Below we will loop through several\ncommon cross-validation objects, visualizing the behavior of each.\n\nNote how some use the group/class information while others do not.\n\n"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": null,
114+
"metadata": {
115+
"collapsed": false
116+
},
117+
"outputs": [],
118+
"source": [
119+
"cvs = [KFold, GroupKFold, ShuffleSplit, StratifiedKFold,\n GroupShuffleSplit, StratifiedShuffleSplit, TimeSeriesSplit]\n\n\nfor cv in cvs:\n this_cv = cv(n_splits=n_splits)\n fig, ax = plt.subplots(figsize=(6, 3))\n plot_cv_indices(this_cv, X, y, groups, ax, n_splits)\n\n ax.legend([Patch(color=cmap_cv(.8)), Patch(color=cmap_cv(.02))],\n ['Testing set', 'Training set'], loc=(1.02, .8))\n # Make the legend fit\n plt.tight_layout()\n fig.subplots_adjust(right=.7)\nplt.show()"
120+
]
121+
}
122+
],
123+
"metadata": {
124+
"kernelspec": {
125+
"display_name": "Python 3",
126+
"language": "python",
127+
"name": "python3"
128+
},
129+
"language_info": {
130+
"codemirror_mode": {
131+
"name": "ipython",
132+
"version": 3
133+
},
134+
"file_extension": ".py",
135+
"mimetype": "text/x-python",
136+
"name": "python",
137+
"nbconvert_exporter": "python",
138+
"pygments_lexer": "ipython3",
139+
"version": "3.6.6"
140+
}
141+
},
142+
"nbformat": 4,
143+
"nbformat_minor": 0
144+
}

dev/_downloads/plot_cv_indices.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
"""
2+
Visualizing cross-validation behavior in scikit-learn
3+
=====================================================
4+
5+
Choosing the right cross-validation object is a crucial part of fitting a
6+
model properly. There are many ways to split data into training and test
7+
sets in order to avoid model overfitting, to standardize the number of
8+
groups in test sets, etc.
9+
10+
This example visualizes the behavior of several common scikit-learn objects
11+
for comparison.
12+
"""
13+
14+
from sklearn.model_selection import (TimeSeriesSplit, KFold, ShuffleSplit,
15+
StratifiedKFold, GroupShuffleSplit,
16+
GroupKFold, StratifiedShuffleSplit)
17+
import numpy as np
18+
import matplotlib.pyplot as plt
19+
from matplotlib.patches import Patch
20+
np.random.seed(1338)
21+
cmap_data = plt.cm.Paired
22+
cmap_cv = plt.cm.coolwarm
23+
n_splits = 4
24+
25+
###############################################################################
26+
# Visualize our data
27+
# ------------------
28+
#
29+
# First, we must understand the structure of our data. It has 100 randomly
30+
# generated input datapoints, 3 classes split unevenly across datapoints,
31+
# and 10 "groups" split evenly across datapoints.
32+
#
33+
# As we'll see, some cross-validation objects do specific things with
34+
# labeled data, others behave differently with grouped data, and others
35+
# do not use this information.
36+
#
37+
# To begin, we'll visualize our data.
38+
39+
# Generate the class/group data
40+
n_points = 100
41+
X = np.random.randn(100, 10)
42+
43+
percentiles_classes = [.1, .3, .6]
44+
y = np.hstack([[ii] * int(100 * perc)
45+
for ii, perc in enumerate(percentiles_classes)])
46+
47+
# Evenly spaced groups repeated once
48+
groups = np.hstack([[ii] * 10 for ii in range(10)])
49+
50+
51+
def visualize_groups(classes, groups, name):
52+
# Visualize dataset groups
53+
fig, ax = plt.subplots()
54+
ax.scatter(range(len(groups)), [.5] * len(groups), c=groups, marker='_',
55+
lw=50, cmap=cmap_data)
56+
ax.scatter(range(len(groups)), [3.5] * len(groups), c=classes, marker='_',
57+
lw=50, cmap=cmap_data)
58+
ax.set(ylim=[-1, 5], yticks=[.5, 3.5],
59+
yticklabels=['Data\ngroup', 'Data\nclass'], xlabel="Sample index")
60+
61+
62+
visualize_groups(y, groups, 'no groups')
63+
64+
###############################################################################
65+
# Define a function to visualize cross-validation behavior
66+
# --------------------------------------------------------
67+
#
68+
# We'll define a function that lets us visualize the behavior of each
69+
# cross-validation object. We'll perform 4 splits of the data. On each
70+
# split, we'll visualize the indices chosen for the training set
71+
# (in blue) and the test set (in red).
72+
73+
74+
def plot_cv_indices(cv, X, y, group, ax, n_splits, lw=10):
75+
"""Create a sample plot for indices of a cross-validation object."""
76+
77+
# Generate the training/testing visualizations for each CV split
78+
for ii, (tr, tt) in enumerate(cv.split(X=X, y=y, groups=group)):
79+
# Fill in indices with the training/test groups
80+
indices = np.array([np.nan] * len(X))
81+
indices[tt] = 1
82+
indices[tr] = 0
83+
84+
# Visualize the results
85+
ax.scatter(range(len(indices)), [ii + .5] * len(indices),
86+
c=indices, marker='_', lw=lw, cmap=cmap_cv,
87+
vmin=-.2, vmax=1.2)
88+
89+
# Plot the data classes and groups at the end
90+
ax.scatter(range(len(X)), [ii + 1.5] * len(X),
91+
c=y, marker='_', lw=lw, cmap=cmap_data)
92+
93+
ax.scatter(range(len(X)), [ii + 2.5] * len(X),
94+
c=group, marker='_', lw=lw, cmap=cmap_data)
95+
96+
# Formatting
97+
yticklabels = list(range(n_splits)) + ['class', 'group']
98+
ax.set(yticks=np.arange(n_splits+2) + .5, yticklabels=yticklabels,
99+
xlabel='Sample index', ylabel="CV iteration",
100+
ylim=[n_splits+2.2, -.2], xlim=[0, 100])
101+
ax.set_title('{}'.format(type(cv).__name__), fontsize=15)
102+
return ax
103+
104+
105+
###############################################################################
106+
# Let's see how it looks for the `KFold` cross-validation object:
107+
108+
fig, ax = plt.subplots()
109+
cv = KFold(n_splits)
110+
plot_cv_indices(cv, X, y, groups, ax, n_splits)
111+
112+
###############################################################################
113+
# As you can see, by default the KFold cross-validation iterator does not
114+
# take either datapoint class or group into consideration. We can change this
115+
# by using the ``StratifiedKFold`` like so.
116+
117+
fig, ax = plt.subplots()
118+
cv = StratifiedKFold(n_splits)
119+
plot_cv_indices(cv, X, y, groups, ax, n_splits)
120+
121+
###############################################################################
122+
# In this case, the cross-validation retained the same ratio of classes across
123+
# each CV split. Next we'll visualize this behavior for a number of CV
124+
# iterators.
125+
#
126+
# Visualize cross-validation indices for many CV objects
127+
# ------------------------------------------------------
128+
#
129+
# Let's visually compare the cross validation behavior for many
130+
# scikit-learn cross-validation objects. Below we will loop through several
131+
# common cross-validation objects, visualizing the behavior of each.
132+
#
133+
# Note how some use the group/class information while others do not.
134+
135+
cvs = [KFold, GroupKFold, ShuffleSplit, StratifiedKFold,
136+
GroupShuffleSplit, StratifiedShuffleSplit, TimeSeriesSplit]
137+
138+
139+
for cv in cvs:
140+
this_cv = cv(n_splits=n_splits)
141+
fig, ax = plt.subplots(figsize=(6, 3))
142+
plot_cv_indices(this_cv, X, y, groups, ax, n_splits)
143+
144+
ax.legend([Patch(color=cmap_cv(.8)), Patch(color=cmap_cv(.02))],
145+
['Testing set', 'Training set'], loc=(1.02, .8))
146+
# Make the legend fit
147+
plt.tight_layout()
148+
fig.subplots_adjust(right=.7)
149+
plt.show()

dev/_downloads/scikit-learn-docs.pdf

457 KB
Binary file not shown.

dev/_images/iris.png

0 Bytes

0 commit comments

Comments
 (0)