# Creating a Sequence of ClassLabel for multi-label and multi-class problems

**URL:** <https://discuss.huggingface.co/t/creating-a-sequence-of-classlabel-for-multi-label-and-multi-class-problems/75724>\
**Category:** 🤗Datasets\
**Created:** [March 3, 2024, 7:11pm UTC](https://discuss.huggingface.co/t/creating-a-sequence-of-classlabel-for-multi-label-and-multi-class-problems/75724 "2024-03-03T19:11:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon6729996](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@anon6729996](https://discuss.huggingface.co/u/anon6729996)\
**Post date:** [March 3, 2024, 7:11pm UTC](https://discuss.huggingface.co/t/creating-a-sequence-of-classlabel-for-multi-label-and-multi-class-problems/75724/1 "2024-03-03T19:11:48Z")

</div>

Given a `df` with the following columns:

| text | labels |
| --- | --- |
| This is a sentence | [0, 5, 3] |
| I am unhappy | [1, 9, 10] |

**OR**

| text | labels1 | label2 | label3 |
| --- | --- | --- | --- |
| This is a sentence | 0 | 5 | 3 |
| I am unhappy | 1 | 9 | 10 |

How would one create a `Dataset` with the following structure of `dataset.features`:

```auto
features = Features({
    'text': Value(dtype='string', id=None),
    'labels': Sequence({
        'label1': ClassLabel(num_classes=some_number, names=some_names,
        'label2': ClassLabel(num_classes=some_number, names=some_names,
        'label3': ClassLabel(num_classes=some_number, names=some_names
    })
})

```

Using `from_pandas()` with the first df results in a Sequence, but of type int64 and with the second df it results in three features of type int64 which can be encoded as a ClassLabel using `class_encode_column()`, but then one would end up with multiple label features, whereas they are required to be in one feature called `labels` as I understood from [this tutorial](https://huggingface.co/docs/transformers/en/training).

Therefore, how to create a DataSet with a Sequence feature that consists of ClassLabels that can be used for a classifier in multi-class and multi-label scenarios?

I saw [this post](https://discuss.huggingface.co/t/multi-class-using-dataset/8970/2), but the implementation is unclear to me.

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [March 4, 2024, 2:09pm UTC](https://discuss.huggingface.co/t/creating-a-sequence-of-classlabel-for-multi-label-and-multi-class-problems/75724/2 "2024-03-04T14:09:06Z")

</div>

You can use the first format and then cast the column to a Sequence of ClassLabel 🙂

```python
ds = ds.from_pandas(df)
ds = ds.cast_column("labels", Sequence(ClassLabel(names=...)))

```

---

<div class="post-metadata">

**Author:** ![anon6729996](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@anon6729996](https://discuss.huggingface.co/u/anon6729996)\
**Post date:** [March 4, 2024, 2:37pm UTC](https://discuss.huggingface.co/t/creating-a-sequence-of-classlabel-for-multi-label-and-multi-class-problems/75724/3 "2024-03-04T14:37:45Z")

</div>

Amazing, thank you!

If we assume this to be the complete dataframe (same as above):

| text | labels |
| --- | --- |
| This is a sentence | [0, 5, 3] |
| I am unhappy | [1, 9, 10] |

The `names=` to added would be `[0, 5, 3, 1, 9, 10]`:

```auto
ds = ds.from_pandas(df)
ds = ds.cast_column("labels", Sequence(ClassLabel(names=[0, 5, 3, 1, 9, 10])))

```

However, for `label1` (first list value respectively) only the labels `0` and `1` would be valid (for `label2` it’d be `5` and `9` and so on)  
Will this lead to any problems further on? E.g., could it mistakenly predict a `5` for `label1`?

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [March 4, 2024, 4:50pm UTC](https://discuss.huggingface.co/t/creating-a-sequence-of-classlabel-for-multi-label-and-multi-class-problems/75724/4 "2024-03-04T16:50:53Z")

</div>

In this case it seems you have multiple classifications to make, ideally each one should have its own column (and be a single integer if there is only one possibility, or a list if there are multiple possible labels per column)

---

<div class="post-metadata">

**Author:** ![Zahra99](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/zahra99/32/31827_2.png) [@Zahra99](https://discuss.huggingface.co/u/Zahra99)\
**Post date:** [March 23, 2024, 7:27pm UTC](https://discuss.huggingface.co/t/creating-a-sequence-of-classlabel-for-multi-label-and-multi-class-problems/75724/5 "2024-03-23T19:27:29Z")

</div>

Hi @lhoestq ,

I have a dataset formatted for multi-class classification with 4 classes. My objective is to convert the labels to one-hot encodings and create a Hugging Face dataset. To achieve this, I utilized the `sklearn` `OneHotEncoder` function for encoding. However, I encountered an error when attempting to convert the dataset to the Hugging Face format using the following line of code:

```python
features = Features({"text": Value("string"), "label": Sequence(ClassLabel(names=['ang', 'hap', 'neu', 'sad']))})

```

The error message I received is as follows:

```auto
TypeError: Couldn't cast array of type
string
to
Sequence(feature=ClassLabel(names=['ang', 'hap', 'neu', 'sad'], id=None), length=-1, id=None)

```

Could you please provide guidance on how to resolve this issue?

Thank you!

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [March 26, 2024, 8:56pm UTC](https://discuss.huggingface.co/t/creating-a-sequence-of-classlabel-for-multi-label-and-multi-class-problems/75724/6 "2024-03-26T20:56:00Z")

</div>

Hi ! ClassLabel is for columns of integers. To transform your strings to integers you can use `.map()` (or the `.class_encode_column()` method which infers the `names` automatically)
