# Use RDataFrame to produce small training set

**URL:** <https://root-forum.cern.ch/t/use-rdataframe-to-produce-small-training-set/37920>\
**Category:** ROOT\
**Created:** [February 10, 2020, 5:32pm UTC](https://root-forum.cern.ch/t/use-rdataframe-to-produce-small-training-set/37920 "2020-02-10T17:32:01Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![swunsch](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@swunsch](https://root-forum.cern.ch/u/swunsch)\
**Post date:** [February 11, 2020, 2:50pm UTC](https://root-forum.cern.ch/t/use-rdataframe-to-produce-small-training-set/37920/3 "2020-02-11T14:50:42Z")

</div>

Hi Christof!

I’m afraid that shuffling is not a feature that `RDataFrame` provides. So you’ll have to work around to get this done. Moreover, it strongly depends on the size of your data you need in memory for the training. I would propose the following solution, which suits most use-cases:

1. Preprocess your (generic?) samples and write out the pt slices with only the information (so columns) you need for the training - aka slimming and skimming.
2. Load exactly the information you need into memory and shuffle then from there.

**Skimming/slimming/preprocessing:**

```python
df = ROOT.RDataFrame("<treename>", filelist)
df.Filter("<select slice>")\
  .Define("<var name>", "<preprocessing>")\
  .Snapshot("training_data", "file.root", only_columns_you_need)

```

**Data-loading for machine learning and shuffling:**

```python
df = ROOT.RDataFrame("training_data", "file.root")
data = df.AsNumpy() # no arguments means we load all columns!
x = numpy.vstack((data[v] for v in ["order", "the", "variables"]))
x_batch = x[numpy.random.choice(x.shape[0], batch_size)]

```

In principal you can also filter and load in one go (just put the respective filter in front of the `AsNumpy` call) but since you’ll do this many, many times, it’s probably more efficient to have an intermediate step.

In case you want to split the dataset into multiple parts, assuming that each event is statistically independent, you can use the “magic column” `rdfentry_` to make this happen. For example you add a split for a two-fold to the skimming like this:

```python
df = ROOT.RDataFrame(...)
df.Filter("rdfentry_ % 2 == 0").Snapshot(...)
df.Filter("rdfentry_ % 1 == 0").Snapshot(...)

```

We know that the selection of a random subset of a given dataset interfaced by `RDataFrame` is very interesting for machine learning workflows, we are working on it to make it happen more smoothly 🙂

In case the proposal above is not efficient enough because after the skimming/slimming you have still too much data, the most efficient solutions are dependent on the ML library you are targeting and their specialized input pipelines (for example `TFRecord` in TensorFlow).

Best  
Stefan

---

_[View the full topic](https://root-forum.cern.ch/t/use-rdataframe-to-produce-small-training-set/37920)._
