MCPcopy Create free account
hub / github.com/scikit-learn/scikit-learn / _transform

Method _transform

sklearn/preprocessing/_encoders.py:194–265  ·  view source on GitHub ↗
(
        self,
        X,
        handle_unknown="error",
        ensure_all_finite=True,
        warn_on_unknown=False,
        ignore_category_indices=None,
    )

Source from the content-addressed store, hash-verified

192 return output
193
194 def _transform(
195 self,
196 X,
197 handle_unknown="error",
198 ensure_all_finite=True,
199 warn_on_unknown=False,
200 ignore_category_indices=None,
201 ):
202 X_list, n_samples, n_features = self._check_X(
203 X, ensure_all_finite=ensure_all_finite
204 )
205 validate_data(self, X=X, reset=False, skip_check_array=True)
206
207 X_int = np.zeros((n_samples, n_features), dtype=int)
208 X_mask = np.ones((n_samples, n_features), dtype=bool)
209
210 columns_with_unknown = []
211 for i in range(n_features):
212 Xi = X_list[i]
213 diff, valid_mask = _check_unknown(Xi, self.categories_[i], return_mask=True)
214
215 if not np.all(valid_mask):
216 if handle_unknown == "error":
217 msg = (
218 "Found unknown categories {0} in column {1}"
219 " during transform".format(diff, i)
220 )
221 raise ValueError(msg)
222 else:
223 if warn_on_unknown:
224 columns_with_unknown.append(i)
225 # Set the problematic rows to an acceptable value and
226 # continue `The rows are marked `X_mask` and will be
227 # removed later.
228 X_mask[:, i] = valid_mask
229 # cast Xi into the largest string type necessary
230 # to handle different lengths of numpy strings
231 if (
232 self.categories_[i].dtype.kind in ("U", "S")
233 and self.categories_[i].itemsize > Xi.itemsize
234 ):
235 Xi = Xi.astype(self.categories_[i].dtype)
236 elif self.categories_[i].dtype.kind == "O" and Xi.dtype.kind == "U":
237 # categories are objects and Xi are numpy strings.
238 # Cast Xi to an object dtype to prevent truncation
239 # when setting invalid values.
240 Xi = Xi.astype("O")
241 else:
242 Xi = Xi.copy()
243
244 Xi[~valid_mask] = self.categories_[i][0]
245 # We use check_unknown=False, since _check_unknown was
246 # already called above.
247 X_int[:, i] = _encode(Xi, uniques=self.categories_[i], check_unknown=False)
248 if columns_with_unknown:
249 if handle_unknown == "infrequent_if_exist":
250 msg = (
251 "Found unknown categories in columns "

Callers 4

transformMethod · 0.45
_fit_encodings_allMethod · 0.45
transformMethod · 0.45
transformMethod · 0.45

Calls 6

_check_XMethod · 0.95
validate_dataFunction · 0.90
_check_unknownFunction · 0.90
_encodeFunction · 0.90
formatMethod · 0.80

Tested by

no test coverage detected