-
Notifications
You must be signed in to change notification settings - Fork 850
/
bootstrap_outofbag.py
91 lines (66 loc) · 2.45 KB
/
bootstrap_outofbag.py
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
# Sebastian Raschka 2014-2022
# mlxtend Machine Learning Library Extensions
#
# Bootstrap functions
# Author: Sebastian Raschka <sebastianraschka.com>
#
# License: BSD 3 clause
import numpy as np
class BootstrapOutOfBag(object):
"""
Parameters
----------
n_splits : int (default=200)
Number of bootstrap iterations.
Must be larger than 1.
random_seed : int (default=None)
If int, random_seed is the seed used by
the random number generator.
Returns
-------
train_idx : ndarray
The training set indices for that split.
test_idx : ndarray
The testing set indices for that split.
Examples
-----------
For usage examples, please see
http://rasbt.github.io/mlxtend/user_guide/evaluate/BootstrapOutOfBag/
"""
def __init__(self, n_splits=200, random_seed=None):
self.random_seed = random_seed
if not isinstance(n_splits, int) or n_splits < 1:
raise ValueError("Number of splits must be greater than 1.")
self.n_splits = n_splits
def split(self, X, y=None, groups=None):
"""
y : array-like or None (default: None)
Argument is not used and only included as parameter
for compatibility, similar to `KFold` in scikit-learn.
groups : array-like or None (default: None)
Argument is not used and only included as parameter
for compatibility, similar to `KFold` in scikit-learn.
"""
rng = np.random.RandomState(self.random_seed)
sample_idx = np.arange(X.shape[0])
set_idx = set(sample_idx)
for _ in range(self.n_splits):
train_idx = rng.choice(sample_idx, size=sample_idx.shape[0], replace=True)
test_idx = np.array(list(set_idx - set(train_idx)))
yield train_idx, test_idx
def get_n_splits(self, X=None, y=None, groups=None):
"""Returns the number of splitting iterations in the cross-validator
Parameters
----------
X : object
Always ignored, exists for compatibility with scikit-learn.
y : object
Always ignored, exists for compatibility with scikit-learn.
groups : object
Always ignored, exists for compatibility with scikit-learn.
Returns
-------
n_splits : int
Returns the number of splitting iterations in the cross-validator.
"""
return self.n_splits