# Class Labels for Custom Datasets

**URL:** <https://discuss.huggingface.co/t/class-labels-for-custom-datasets/15130>\
**Category:** 🤗Datasets\
**Created:** [February 25, 2022, 6:13am UTC](https://discuss.huggingface.co/t/class-labels-for-custom-datasets/15130 "2022-02-25T06:13:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![calvpang](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@calvpang](https://discuss.huggingface.co/u/calvpang)\
**Post date:** [February 25, 2022, 6:13am UTC](https://discuss.huggingface.co/t/class-labels-for-custom-datasets/15130/1 "2022-02-25T06:13:42Z")

</div>

I’m currently trying to prepare my data from a .csv for a multi-class classification task, for which I have 6 classes which are strings.

```auto
{'Coded_Text': Value(dtype='string', id=None),
 'Coded_Text_Length': Value(dtype='int64', id=None),
 'Label': Value(dtype='string', id=None)}

```

What is the most appropriate way to map these strings into ClassLabel objects?  
I have attempted the solution in [How to convert string labels into ClassLabel classes for custom set in pandas](https://discuss.huggingface.co/t/how-to-convert-string-labels-into-classlabel-classes-for-custom-set-in-pandas/8473), but I encountered the following error.

```auto
ArrowInvalid: ("Could not convert 'training' with type str: tried to convert to int64", 'Conversion failed for column Label with type object')

```

---

<div class="post-metadata">

**Author:** ![mariosasko](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mariosasko/32/31548_2.png) [@mariosasko](https://discuss.huggingface.co/u/mariosasko)\
**Post date:** [February 28, 2022, 3:07pm UTC](https://discuss.huggingface.co/t/class-labels-for-custom-datasets/15130/2 "2022-02-28T15:07:21Z")

</div>

Hi! You are getting this error most likely because the label `training` is not specified as a label in the `names` list of the `ClassLabel` feature. To avoid this error, I suggest you use `class_encode_column` instead, which will automatically find all the unique string values in the column:

```auto
from datasets import Dataset
dataset = Dataset.from_pandas(df)
dataset = dataset.class_encode_column("Label")

```

---

<div class="post-metadata">

**Author:** ![calvpang](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@calvpang](https://discuss.huggingface.co/u/calvpang)\
**Post date:** [March 1, 2022, 1:28am UTC](https://discuss.huggingface.co/t/class-labels-for-custom-datasets/15130/3 "2022-03-01T01:28:07Z")

</div>

> [@mariosasko](#):
>
> class\_encode\_column

Thanks! That worked and was a lot cleaner than my alternative solution.

```auto
# Creating a ClassLabel Object
df = dataset["train"].to_pandas()
labels = df['label'].unique().tolist()
ClassLabels = ClassLabel(num_classes=len(labels), names=labels)

# Mapping Labels to IDs
def map_label2id(example):
    example['label'] = ClassLabels.str2int(example['label'])
    return example

dataset = dataset.map(map_label2id, batched=True)

# Casting label column to ClassLabel Object
dataset = dataset.cast_column('label', ClassLabels)

```

---

<div class="post-metadata">

**Author:** ![hariprasad0994](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@hariprasad0994](https://discuss.huggingface.co/u/hariprasad0994)\
**Post date:** [June 1, 2022, 10:04pm UTC](https://discuss.huggingface.co/t/class-labels-for-custom-datasets/15130/4 "2022-06-01T22:04:44Z")

</div>

Hi,  
I’m experimenting with the emotion dataset from manually downloaded files.

```auto
from datasets import load_dataset
dataset = load_dataset('csv', data_files={'train': 'train.txt', 'validation': 'val.txt', 'test': 'test.txt'}, sep=";", 
                              names=["text", "label"])
dataset.cast_column("label", ClassLabel(names = ['anger', 'fear', 'joy', 'love', 'sadness', 'surprise'] ))

```

I’m getting “ArrowInvalid: Failed to parse string: ‘anger’ as a scalar of type int64” error, even after specifying `anger` as label in the `names` list

Thanks for your suggestion `class_encode_column`, it worked.

Any idea why cast\_column isn’t working in this case?

---

<div class="post-metadata">

**Author:** ![mariosasko](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mariosasko/32/31548_2.png) [@mariosasko](https://discuss.huggingface.co/u/mariosasko)\
**Post date:** [June 2, 2022, 3:14pm UTC](https://discuss.huggingface.co/t/class-labels-for-custom-datasets/15130/5 "2022-06-02T15:14:33Z")

</div>

Hi! Currently, only integer values support casting to the `ClassLabel` type hence the error. But we’ve recently added support for casting from string values, which will be available in the next release of `datasets` (currently only available on `master` if you want to try it).
