# 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:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![csauer](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/csauer/32/28552_2.png) [@csauer](https://root-forum.cern.ch/u/csauer)\
**Post date:** [February 10, 2020, 5:32pm UTC](https://root-forum.cern.ch/t/use-rdataframe-to-produce-small-training-set/37920/1 "2020-02-10T17:32:01Z")

</div>

Dear all,

I recently stumbled upon this very promising RDataFrame interface and I’d now like to utilize it to produce a small training set that will be used to train a neural network. The actual/full data set is an abundance of root files (each one with millions of events!) whereby each file only contains events within a certain range in transverse momentum (pT slices). I’d like to (randomly) sample a well-defined number of events (that will be used for training) out of all events whilst taking into account the statistics in the individual pT slices in each file. How can I archive this task using ROOT’s RDataFrame? And if RDataFrame is not the appropriate choice, what else can I do?  
I know how to use the RDataFrame interface to filter a data set and how to create a new one using the “Snapshot” method, but how to load several root files and “shuffle” them such that the result is a small representative of the full data set?  
Thank you very much in advance!

Best regards

---

<div class="post-metadata">

**Author:** ![oshadura](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/oshadura/32/14460_2.png) [@oshadura](https://root-forum.cern.ch/u/oshadura)\
**Post date:** [February 11, 2020, 11:35am UTC](https://root-forum.cern.ch/t/use-rdataframe-to-produce-small-training-set/37920/2 "2020-02-11T11:35:43Z")

</div>

@eguiraud @swunsch perhaps maybe do you have some simple example here?

Thanks,  
Oksana.

---

<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

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [February 25, 2020, 3:04pm UTC](https://root-forum.cern.ch/t/use-rdataframe-to-produce-small-training-set/37920/4 "2020-02-25T15:04:55Z")

</div>

Closed on February 25, 2020, 3:04pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
