A data iterator for XGBoost DMatrix. `reset` and `next` are required for any data iterator, other functions here are utilites for demonstration's purpose.
| 34 | |
| 35 | |
| 36 | class IterForDMatrixDemo(xgboost.core.DataIter): |
| 37 | """A data iterator for XGBoost DMatrix. |
| 38 | |
| 39 | `reset` and `next` are required for any data iterator, other functions here |
| 40 | are utilites for demonstration's purpose. |
| 41 | |
| 42 | """ |
| 43 | |
| 44 | def __init__(self) -> None: |
| 45 | """Generate some random data for demostration. |
| 46 | |
| 47 | Actual data can be anything that is currently supported by XGBoost. |
| 48 | """ |
| 49 | self.rows = ROWS_PER_BATCH |
| 50 | self.cols = COLS |
| 51 | rng = cupy.random.RandomState(numpy.uint64(1994)) |
| 52 | self._data = [rng.randn(self.rows, self.cols)] * BATCHES |
| 53 | self._labels = [rng.randn(self.rows)] * BATCHES |
| 54 | self._weights = [rng.uniform(size=self.rows)] * BATCHES |
| 55 | |
| 56 | self.it = 0 # set iterator to 0 |
| 57 | super().__init__() |
| 58 | |
| 59 | def as_array(self) -> cupy.ndarray: |
| 60 | return cupy.concatenate(self._data) |
| 61 | |
| 62 | def as_array_labels(self) -> cupy.ndarray: |
| 63 | return cupy.concatenate(self._labels) |
| 64 | |
| 65 | def as_array_weights(self) -> cupy.ndarray: |
| 66 | return cupy.concatenate(self._weights) |
| 67 | |
| 68 | def data(self) -> cupy.ndarray: |
| 69 | """Utility function for obtaining current batch of data.""" |
| 70 | return self._data[self.it] |
| 71 | |
| 72 | def labels(self) -> cupy.ndarray: |
| 73 | """Utility function for obtaining current batch of label.""" |
| 74 | return self._labels[self.it] |
| 75 | |
| 76 | def weights(self) -> cupy.ndarray: |
| 77 | return self._weights[self.it] |
| 78 | |
| 79 | def reset(self) -> None: |
| 80 | """Reset the iterator""" |
| 81 | self.it = 0 |
| 82 | |
| 83 | def next(self, input_data: Callable) -> bool: |
| 84 | """Yield the next batch of data.""" |
| 85 | if self.it == len(self._data): |
| 86 | # Return False to let XGBoost know this is the end of iteration |
| 87 | return False |
| 88 | |
| 89 | # input_data is a keyword-only function passed in by XGBoost and has the similar |
| 90 | # signature to the ``DMatrix`` constructor. |
| 91 | input_data(data=self.data(), label=self.labels(), weight=self.weights()) |
| 92 | self.it += 1 |
| 93 | return True |