File size: 8,113 Bytes
c13737d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# Structure your repository

To host and share your dataset, create a dataset repository on the Hugging Face Hub and upload your data files.

This guide will show you how to structure your dataset repository when you upload it.
A dataset with a supported structure and file format (`.txt`, `.csv`, `.parquet`, `.jsonl`, `.mp3`, `.jpg`, `.zip` etc.) are loaded automatically with [`~datasets.load_dataset`], and it'll have a dataset viewer on its dataset page on the Hub.

## Main use-case

The simplest dataset structure has two files: `train.csv` and `test.csv` (this works with any supported file format).

Your repository will also contain a `README.md` file, the [dataset card](dataset_card) displayed on your dataset page.

```
my_dataset_repository/
β”œβ”€β”€ README.md
β”œβ”€β”€ train.csv
└── test.csv
```

In this simple case, you'll get a dataset with two splits: `train` (containing examples from `train.csv`) and `test` (containing examples from `test.csv`).

## Define your splits and subsets in YAML

## Splits

If you have multiple files and want to define which file goes into which split, you can use the YAML `configs` field at the top of your README.md.

For example, given a repository like this one:

```
my_dataset_repository/
β”œβ”€β”€ README.md
β”œβ”€β”€ data.csv
└── holdout.csv
```

You can define your splits by adding the `configs` field in the YAML block at the top of your README.md:

```yaml
---
configs:
- config_name: default
  data_files:
  - split: train
    path: "data.csv"
  - split: test
    path: "holdout.csv"
---
```


You can select multiple files per split using a list of paths:

```
my_dataset_repository/
β”œβ”€β”€ README.md
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ abc.csv
β”‚   └── def.csv
└── holdout/
    └── ghi.csv
```

```yaml
---
configs:
- config_name: default
  data_files:
  - split: train
    path:
    - "data/abc.csv"
    - "data/def.csv"
  - split: test
    path: "holdout/ghi.csv"
---
```

Or you can use glob patterns to automatically list all the files you need:

```yaml
---
configs:
- config_name: default
  data_files:
  - split: train
    path: "data/*.csv"
  - split: test
    path: "holdout/*.csv"
---
```

<Tip warning={true}>

Note that `config_name` field is required even if you have a single configuration.

</Tip>

## Configurations

Your dataset might have several subsets of data that you want to be able to load separately. In that case you can define a list of configurations inside the `configs` field in YAML:

```
my_dataset_repository/
β”œβ”€β”€ README.md
β”œβ”€β”€ main_data.csv
└── additional_data.csv
```

```yaml
---
configs:
- config_name: main_data
  data_files: "main_data.csv"
- config_name: additional_data
  data_files: "additional_data.csv"
---
```

Each configuration is shown separately on the Hugging Face Hub, and can be loaded by passing its name as a second parameter:

```python
from datasets import load_dataset

main_data = load_dataset("my_dataset_repository", "main_data")
additional_data = load_dataset("my_dataset_repository", "additional_data")
```

## Builder parameters

Not only `data_files`, but other builder-specific parameters can be passed via YAML, allowing for more flexibility on how to load the data while not requiring any custom code. For example, define which separator to use in which configuration to load your `csv` files:

```yaml
---
configs:
- config_name: tab
  data_files: "main_data.csv"
  sep: "\t"
- config_name: comma
  data_files: "additional_data.csv"
  sep: ","
---
```

Refer to [specific builders' documentation](./package_reference/builder_classes) to see what configuration parameters they have.

<Tip>

You can set a default configuration using `default: true`, e.g. you can run `main_data = load_dataset("my_dataset_repository")` if you set 

```yaml
- config_name: main_data
  data_files: "main_data.csv"
  default: true
```

</Tip>

## Automatic splits detection

If no YAML is provided, πŸ€— Datasets searches for certain patterns in the dataset repository to automatically infer the dataset splits.
There is an order to the patterns, beginning with the custom filename split format to treating all files as a single split if no pattern is found.

### Directory name

Your data files may also be placed into different directories named `train`, `test`, and `validation` where each directory contains the data files for that split:

```
my_dataset_repository/
β”œβ”€β”€ README.md
└── data/
    β”œβ”€β”€ train/
    β”‚   └── bees.csv
    β”œβ”€β”€ test/
    β”‚   └── more_bees.csv
    └── validation/
        └── even_more_bees.csv
```

### Filename splits

If you don't have any non-traditional splits, then you can place the split name anywhere in the data file and it is automatically inferred. The only rule is that the split name must be delimited by non-word characters, like `test-file.csv` for example instead of `testfile.csv`. Supported delimiters include underscores, dashes, spaces, dots, and numbers.

For example, the following file names are all acceptable:

- train split: `train.csv`, `my_train_file.csv`, `train1.csv`
- validation split: `validation.csv`, `my_validation_file.csv`, `validation1.csv`
- test split: `test.csv`, `my_test_file.csv`, `test1.csv`

Here is an example where all the files are placed into a directory named `data`:

```
my_dataset_repository/
β”œβ”€β”€ README.md
└── data/
    β”œβ”€β”€ train.csv
    β”œβ”€β”€ test.csv
    └── validation.csv
```

### Custom filename split

If your dataset splits have custom names that aren't `train`, `test`, or `validation`, then you can name your data files like `data/<split_name>-xxxxx-of-xxxxx.csv`.

Here is an example with three splits, `train`, `test`, and `random`:

```
my_dataset_repository/
β”œβ”€β”€ README.md
└── data/
    β”œβ”€β”€ train-00000-of-00003.csv
    β”œβ”€β”€ train-00001-of-00003.csv
    β”œβ”€β”€ train-00002-of-00003.csv
    β”œβ”€β”€ test-00000-of-00001.csv
    β”œβ”€β”€ random-00000-of-00003.csv
    β”œβ”€β”€ random-00001-of-00003.csv
    └── random-00002-of-00003.csv
```

### Single split

When πŸ€— Datasets can't find any of the above patterns, then it'll treat all the files as a single train split. If your dataset splits aren't loading as expected, it may be due to an incorrect pattern.

### Split name keywords

There are several ways to name splits. Validation splits are sometimes called "dev", and test splits may be referred to as "eval".
These other split names are also supported, and the following keywords are equivalent:

- train, training
- validation, valid, val, dev
- test, testing, eval, evaluation

The structure below is a valid repository:

```
my_dataset_repository/
β”œβ”€β”€ README.md
└── data/
    β”œβ”€β”€ training.csv
    β”œβ”€β”€ eval.csv
    └── valid.csv
```

### Multiple files per split

If one of your splits comprises several files, πŸ€— Datasets can still infer whether it is the train, validation, and test split from the file name.
For example, if your train and test splits span several files:

```
my_dataset_repository/
β”œβ”€β”€ README.md
β”œβ”€β”€ train_0.csv
β”œβ”€β”€ train_1.csv
β”œβ”€β”€ train_2.csv
β”œβ”€β”€ train_3.csv
β”œβ”€β”€ test_0.csv
└── test_1.csv
```

Make sure all the files of your `train` set have *train* in their names (same for test and validation).
Even if you add a prefix or suffix to `train` in the file name (like `my_train_file_00001.csv` for example),
πŸ€— Datasets can still infer the appropriate split.

For convenience, you can also place your data files into different directories.
In this case, the split name is inferred from the directory name.

```
my_dataset_repository/
β”œβ”€β”€ README.md
└── data/
    β”œβ”€β”€ train/
    β”‚   β”œβ”€β”€ shard_0.csv
    β”‚   β”œβ”€β”€ shard_1.csv
    β”‚   β”œβ”€β”€ shard_2.csv
    β”‚   └── shard_3.csv
    └── test/
        β”œβ”€β”€ shard_0.csv
        └── shard_1.csv
```

For more flexibility over how to load and generate a dataset, you can also write a [dataset loading script](./dataset_script).