@@ -81,31 +81,62 @@ def if_delegate_has_method(delegate):
81
81
82
82
83
83
def _safe_split (estimator , X , y , indices , train_indices = None ):
84
- """Create subset of dataset and properly handle kernels."""
85
- from ..gaussian_process .kernels import Kernel as GPKernel
84
+ """Create subset of dataset and properly handle kernels.
86
85
87
- if (hasattr (estimator , 'kernel' ) and callable (estimator .kernel ) and
88
- not isinstance (estimator .kernel , GPKernel )):
89
- # cannot compute the kernel values with custom function
90
- raise ValueError ("Cannot use a custom kernel function. "
91
- "Precompute the kernel matrix instead." )
86
+ Slice X, y according to indices for cross-validation, but take care of
87
+ precomputed kernel-matrices or pairwise affinities / distances.
92
88
93
- if not hasattr (X , "shape" ):
94
- if getattr (estimator , "_pairwise" , False ):
89
+ If ``estimator._pairwise is True``, X needs to be square and
90
+ we slice rows and columns. If ``train_indices`` is not None,
91
+ we slice rows using ``indices`` (assumed the test set) and columns
92
+ using ``train_indices``, indicating the training set.
93
+
94
+ Labels y will always be sliced only along the last axis.
95
+
96
+ Parameters
97
+ ----------
98
+ estimator : object
99
+ Estimator to determine whether we should slice only rows or rows and
100
+ columns.
101
+
102
+ X : array-like, sparse matrix or iterable
103
+ Data to be sliced. If ``estimator._pairwise is True``,
104
+ this needs to be a square array-like or sparse matrix.
105
+
106
+ y : array-like, sparse matrix or iterable
107
+ Targets to be sliced.
108
+
109
+ indices : array of int
110
+ Rows to select from X and y.
111
+ If ``estimator._pairwise is True`` and ``train_indices is None``
112
+ then ``indices`` will also be used to slice columns.
113
+
114
+ train_indices : array of int or None, default=None
F438
115
+ If ``estimator._pairwise is True`` and ``train_indices is not None``,
116
+ then ``train_indices`` will be use to slice the columns of X.
117
+
118
+ Returns
119
+ -------
120
+ X_sliced : array-like, sparse matrix or list
121
+ Sliced data.
122
+
123
+ y_sliced : array-like, sparse matrix or list
124
+ Sliced targets.
125
+
126
+ """
127
+ if getattr (estimator , "_pairwise" , False ):
128
+ if not hasattr (X , "shape" ):
95
129
raise ValueError ("Precomputed kernels or affinity matrices have "
96
130
"to be passed as arrays or sparse matrices." )
97
- X_subset = [X [index ] for index in indices ]
98
- else :
99
- if getattr (estimator , "_pairwise" , False ):
100
- # X is a precomputed square kernel matrix
101
- if X .shape [0 ] != X .shape [1 ]:
102
- raise ValueError ("X should be a square kernel matrix" )
103
- if train_indices is None :
104
- X_subset = X [np .ix_ (indices , indices )]
105
- else :
106
- X_subset = X [np .ix_ (indices , train_indices )]
131
+ # X is a precomputed square kernel matrix
132
+ if X .shape [0 ] != X .shape [1 ]:
133
+ raise ValueError ("X should be a square kernel matrix" )
134
+ if train_indices is None :
135
+ X_subset = X [np .ix_ (indices , indices )]
107
136
else :
108
- X_subset = safe_indexing (X , indices )
137
+ X_subset = X [np .ix_ (indices , train_indices )]
138
+ else :
139
+ X_subset = safe_indexing (X , indices )
109
140
110
141
if y is not None :
111
142
y_subset = safe_indexing (y , indices )
0 commit comments