# How to apply training ClassLabels on test / validation Dataset

**URL:** <https://discuss.huggingface.co/t/how-to-apply-training-classlabels-on-test-validation-dataset/55621>\
**Category:** 🤗Datasets\
**Created:** [September 19, 2023, 2:02pm UTC](https://discuss.huggingface.co/t/how-to-apply-training-classlabels-on-test-validation-dataset/55621 "2023-09-19T14:02:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![WalterSobchack](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@WalterSobchack](https://discuss.huggingface.co/u/WalterSobchack)\
**Post date:** [September 19, 2023, 2:02pm UTC](https://discuss.huggingface.co/t/how-to-apply-training-classlabels-on-test-validation-dataset/55621/1 "2023-09-19T14:02:42Z")

</div>

I am trying to create a custom dataset using flat files. The data elements are 'Tokens" and ‘NER Tags’ strings. I am able to create the ClassLabels for the Training dataset and now want to apply the same ClassLabels on the Test & Validation datasets. I have tried several approaches, but none have worked. Any help or insight will be greatly appreciated. The code segment is below :

def convert\_to\_featured\_dataset(data):  
formatted\_data = {“tokens”: , “ner\_tags”: }  
features = Features(  
{“tokens”: Value(dtype=“string”), “ner\_tags”: Value(dtype=“string”)}  
)  
for sentence in data:  
tokens = [token\_data[1] for token\_data in sentence]  
ner\_tags = [token\_data[0] for token\_data in sentence]

```
    formatted_data["tokens"].append(tokens)
    formatted_data["ner_tags"].append(ner_tags)

dataset = Dataset.from_dict(formatted_data, features=features)
return dataset

```

…  
…  
#Works  
train\_dataset = convert\_to\_featured\_dataset(train\_data)  
train\_dataset = train\_dataset.class\_encode\_column(“ner\_tags”)  
label\_feature = train\_dataset.features[“ner\_tags”]  
test\_features = train\_dataset.features.copy()  
…  
…  
#Does not work  
def convert\_ids(rec):  
#Error : ValueError: Invalid string class label [‘O’, ‘O’, ‘B-RATINGS\_AVERAGE’, ‘I-RATINGS\_AVERAGE’, …']  
rec[“ner\_tags”] = label\_feature.str2int(rec[“ner\_tags”])  
return rec

```
test_dataset = convert_to_featured_dataset(test_data)
# Did not work : ValueError: Invalid string class label ['O'...
#test_dataset = test_dataset.map(convert_ids, batched=True)
#test_features["ner_tags"] = ClassLabel(label_feature.feature.names)
#test_dataset = test_dataset.cast(test_features)
 test_dataset = test_dataset.cast_column('ner_tags',label_feature)

```

Thank you for your time and help.

---

<div class="post-metadata">

**Author:** ![WalterSobchack](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@WalterSobchack](https://discuss.huggingface.co/u/WalterSobchack)\
**Post date:** [September 19, 2023, 4:44pm UTC](https://discuss.huggingface.co/t/how-to-apply-training-classlabels-on-test-validation-dataset/55621/2 "2023-09-19T16:44:32Z")

</div>

Tried encoding the ner\_tags separately and then casting them to ClassLabel but got stuck on another point

```
# Failed : may have worked if ner_tags feature was like ['1','2','3'] but it is like [['1','2','3'] ,['1','2','3'] ]  
# Error : ValueError: Invalid string class label ['24', '24', '24', '0', '12']
train_dataset = train_dataset.cast_column(
    "ner_tags", ClassLabel(names=id_label_list)
)

```

---

<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:** [September 20, 2023, 12:45pm UTC](https://discuss.huggingface.co/t/how-to-apply-training-classlabels-on-test-validation-dataset/55621/3 "2023-09-20T12:45:32Z")

</div>

You can pass `Sequence(ClassLabel(...))` to `.cast_column` to cast a list of labels.
