A data iterator for XGBoost DMatrix. `reset` and `next` are required for any data iterator, other functions here are utilites for demonstration's purpose.
| 282 | |
| 283 | |
| 284 | class IterForDMatrixTest(xgb.core.DataIter): |
| 285 | """A data iterator for XGBoost DMatrix. |
| 286 | |
| 287 | `reset` and `next` are required for any data iterator, other functions here |
| 288 | are utilites for demonstration's purpose. |
| 289 | |
| 290 | """ |
| 291 | |
| 292 | ROWS_PER_BATCH = 100 # data is splited by rows |
| 293 | BATCHES = 16 |
| 294 | |
| 295 | def __init__(self, categorical: bool) -> None: |
| 296 | """Generate some random data for demostration. |
| 297 | |
| 298 | Actual data can be anything that is currently supported by XGBoost. |
| 299 | """ |
| 300 | self.rows = self.ROWS_PER_BATCH |
| 301 | |
| 302 | if categorical: |
| 303 | self._data = [] |
| 304 | self._labels = [] |
| 305 | for i in range(self.BATCHES): |
| 306 | X, y = tm.make_categorical(self.ROWS_PER_BATCH, 4, 13, onehot=False) |
| 307 | self._data.append(cudf.from_pandas(X)) |
| 308 | self._labels.append(y) |
| 309 | else: |
| 310 | rng = np.random.RandomState(1994) |
| 311 | self._data = [ |
| 312 | cudf.DataFrame( |
| 313 | { |
| 314 | "a": rng.randn(self.ROWS_PER_BATCH), |
| 315 | "b": rng.randn(self.ROWS_PER_BATCH), |
| 316 | } |
| 317 | ) |
| 318 | ] * self.BATCHES |
| 319 | self._labels = [rng.randn(self.rows)] * self.BATCHES |
| 320 | |
| 321 | self.it = 0 # set iterator to 0 |
| 322 | super().__init__(cache_prefix=None) |
| 323 | |
| 324 | def as_array(self) -> "cudf.DataFrame": |
| 325 | return cudf.concat(self._data) |
| 326 | |
| 327 | def as_array_labels(self) -> np.ndarray: |
| 328 | return np.concatenate(self._labels) |
| 329 | |
| 330 | def data(self) -> "cudf.DataFrame": |
| 331 | """Utility function for obtaining current batch of data.""" |
| 332 | return self._data[self.it] |
| 333 | |
| 334 | def labels(self) -> Any: |
| 335 | """Utility function for obtaining current batch of label.""" |
| 336 | return self._labels[self.it] |
| 337 | |
| 338 | def reset(self) -> None: |
| 339 | """Reset the iterator""" |
| 340 | self.it = 0 |
| 341 |
no outgoing calls