forked from openml/openml-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_manual_iteration_tutorial.py
More file actions
172 lines (144 loc) 路 5.75 KB
/
Copy pathtask_manual_iteration_tutorial.py
File metadata and controls
172 lines (144 loc) 路 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# %% [markdown]
# Tasks define a target and a train/test split, which we can use for benchmarking.
# %%
import openml
# %% [markdown]
# For this tutorial we will use the famous King+Rook versus King+Pawn on A7 dataset, which has
# the dataset ID 3 ([dataset on OpenML](https://www.openml.org/d/3)), and for which there exist
# tasks with all important estimation procedures. It is small enough (less than 5000 samples) to
# efficiently use it in an example.
#
# We will first start with ([task 233](https://www.openml.org/t/233)), which is a task with a
# holdout estimation procedure.
# %%
task_id = 233
task = openml.tasks.get_task(task_id)
# %% [markdown]
# Now that we have a task object we can obtain the number of repetitions, folds and samples as
# defined by the task:
# %%
n_repeats, n_folds, n_samples = task.get_split_dimensions()
# %% [markdown]
# * ``n_repeats``: Number of times the model quality estimation is performed
# * ``n_folds``: Number of folds per repeat
# * ``n_samples``: How many data points to use. This is only relevant for learning curve tasks
#
# A list of all available estimation procedures is available
# [here](https://www.openml.org/search?q=%2520measure_type%3Aestimation_procedure&type=measure).
#
# Task ``233`` is a simple task using the holdout estimation procedure and therefore has only a
# single repeat, a single fold and a single sample size:
# %%
print(
f"Task {task_id}: number of repeats: {n_repeats}, number of folds: {n_folds}, number of samples {n_samples}."
)
# %% [markdown]
# We can now retrieve the train/test split for this combination of repeats, folds and number of
# samples (indexing is zero-based). Usually, one would loop over all repeats, folds and sample
# sizes, but we can neglect this here as there is only a single repetition.
# %%
train_indices, test_indices = task.get_train_test_split_indices(
repeat=0,
fold=0,
sample=0,
)
print(train_indices.shape, train_indices.dtype)
print(test_indices.shape, test_indices.dtype)
# %% [markdown]
# And then split the data based on this:
# %%
X, y = task.get_X_and_y()
X_train = X.iloc[train_indices]
y_train = y.iloc[train_indices]
X_test = X.iloc[test_indices]
y_test = y.iloc[test_indices]
print(
f"X_train.shape: {X_train.shape}, y_train.shape: {y_train.shape}, X_test.shape: {X_test.shape}, y_test.shape: {y_test.shape}"
)
# %% [markdown]
# Obviously, we can also retrieve cross-validation versions of the dataset used in task ``233``:
# %%
task_id = 3
task = openml.tasks.get_task(task_id)
X, y = task.get_X_and_y()
n_repeats, n_folds, n_samples = task.get_split_dimensions()
print(
f"Task {task_id}: number of repeats: {n_repeats}, number of folds: {n_folds}, number of samples {n_samples}."
)
# %% [markdown]
# And then perform the aforementioned iteration over all splits:
# %%
for repeat_idx in range(n_repeats):
for fold_idx in range(n_folds):
for sample_idx in range(n_samples):
train_indices, test_indices = task.get_train_test_split_indices(
repeat=repeat_idx,
fold=fold_idx,
sample=sample_idx,
)
X_train = X.iloc[train_indices]
y_train = y.iloc[train_indices]
X_test = X.iloc[test_indices]
y_test = y.iloc[test_indices]
print(
f"Repeat #{repeat_idx}, fold #{fold_idx}, samples {sample_idx}: X_train.shape: {X_train.shape}, "
f"y_train.shape {y_train.shape}, X_test.shape {X_test.shape}, y_test.shape {y_test.shape}"
)
# %% [markdown]
# And also versions with multiple repeats:
# %%
task_id = 1767
task = openml.tasks.get_task(task_id)
X, y = task.get_X_and_y()
n_repeats, n_folds, n_samples = task.get_split_dimensions()
print(
f"Task {task_id}: number of repeats: {n_repeats}, number of folds: {n_folds}, number of samples {n_samples}."
)
# %% [markdown]
# And then again perform the aforementioned iteration over all splits:
# %%
for repeat_idx in range(n_repeats):
for fold_idx in range(n_folds):
for sample_idx in range(n_samples):
train_indices, test_indices = task.get_train_test_split_indices(
repeat=repeat_idx,
fold=fold_idx,
sample=sample_idx,
)
X_train = X.iloc[train_indices]
y_train = y.iloc[train_indices]
X_test = X.iloc[test_indices]
y_test = y.iloc[test_indices]
print(
f"Repeat #{repeat_idx}, fold #{fold_idx}, samples {sample_idx}: X_train.shape: {X_train.shape}, "
f"y_train.shape {y_train.shape}, X_test.shape {X_test.shape}, y_test.shape {y_test.shape}"
)
# %% [markdown]
# And finally a task based on learning curves:
# %%
task_id = 1702
task = openml.tasks.get_task(task_id)
X, y = task.get_X_and_y()
n_repeats, n_folds, n_samples = task.get_split_dimensions()
print(
f"Task {task_id}: number of repeats: {n_repeats}, number of folds: {n_folds}, number of samples {n_samples}."
)
# %% [markdown]
# And then again perform the aforementioned iteration over all splits:
# %%
for repeat_idx in range(n_repeats):
for fold_idx in range(n_folds):
for sample_idx in range(n_samples):
train_indices, test_indices = task.get_train_test_split_indices(
repeat=repeat_idx,
fold=fold_idx,
sample=sample_idx,
)
X_train = X.iloc[train_indices]
y_train = y.iloc[train_indices]
X_test = X.iloc[test_indices]
y_test = y.iloc[test_indices]
print(
f"Repeat #{repeat_idx}, fold #{fold_idx}, samples {sample_idx}: X_train.shape: {X_train.shape}, "
f"y_train.shape {y_train.shape}, X_test.shape {X_test.shape}, y_test.shape {y_test.shape}"
)