|
3 | 3 | # Andreas Mueller
|
4 | 4 | # License: BSD
|
5 | 5 |
|
| 6 | +import numpy as np |
| 7 | +from ..utils import safe_indexing |
6 | 8 | from operator import attrgetter
|
7 | 9 | from functools import update_wrapper
|
8 | 10 | from ..externals import six
|
@@ -120,3 +122,38 @@ def if_delegate_has_method(delegate):
|
120 | 122 |
|
121 | 123 | return lambda fn: _IffHasAttrDescriptor(fn, delegate,
|
122 | 124 | attribute_name=fn.__name__)
|
| 125 | + |
| 126 | + |
| 127 | +def _safe_split(estimator, X, y, indices, train_indices=None): |
| 128 | + """Create subset of dataset and properly handle kernels.""" |
| 129 | + from ..gaussian_process.kernels import Kernel as GPKernel |
| 130 | + |
| 131 | + if (hasattr(estimator, 'kernel') and callable(estimator.kernel) and |
| 132 | + not isinstance(estimator.kernel, GPKernel)): |
| 133 | + # cannot compute the kernel values with custom function |
| 134 | + raise ValueError("Cannot use a custom kernel function. " |
| 135 | + "Precompute the kernel matrix instead.") |
| 136 | + |
| 137 | + if not hasattr(X, "shape"): |
| 138 | + if getattr(estimator, "_pairwise", False): |
| 139 | + raise ValueError("Precomputed kernels or affinity matrices have " |
| 140 | + "to be passed as arrays or sparse matrices.") |
| 141 | + X_subset = [X[index] for index in indices] |
| 142 | + else: |
| 143 | + if getattr(estimator, "_pairwise", False): |
| 144 | + # X is a precomputed square kernel matrix |
| 145 | + if X.shape[0] != X.shape[1]: |
| 146 | + raise ValueError("X should be a square kernel matrix") |
| 147 | + if train_indices is None: |
| 148 | + X_subset = X[np.ix_(indices, indices)] |
| 149 | + else: |
| 150 | + X_subset = X[np.ix_(indices, train_indices)] |
| 151 | + else: |
| 152 | + X_subset = safe_indexing(X, indices) |
| 153 | + |
| 154 | + if y is not None: |
| 155 | + y_subset = safe_indexing(y, indices) |
| 156 | + else: |
| 157 | + y_subset = None |
| 158 | + |
| 159 | + return X_subset, y_subset |
0 commit comments