# Numpy.str\_ error during training phase

**URL:** <https://discuss.huggingface.co/t/numpy-str-error-during-training-phase/12425>\
**Category:** Course\
**Created:** [December 1, 2021, 4:48pm UTC](https://discuss.huggingface.co/t/numpy-str-error-during-training-phase/12425 "2021-12-01T16:48:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivanlau](https://avatars.discourse-cdn.com/v4/letter/i/b38774/32.png) [@ivanlau](https://discuss.huggingface.co/u/ivanlau)\
**Post date:** [December 1, 2021, 4:48pm UTC](https://discuss.huggingface.co/t/numpy-str-error-during-training-phase/12425/1 "2021-12-01T16:48:18Z")

</div>

Hi, I was using [amazon datasets](https://huggingface.co/datasets/amazon_reviews_multi) for trying to build a small language detector. but bumped up the numpy.str\_ error during training phase. You can view my colab notebook here: [Google Colab](https://colab.research.google.com/drive/1b_08I1gU57luRdC6vUtG7P1Yxk3PB4Xp?usp=sharing) .

I was using `review body` field as text and `language` field as label. and dropped the other fields.

I found that the ‘language’ data field is type of `datasets.Value`, not the `datasets.ClassLabel`. I guessed this causing the numpy.str\_ error during training.

Question: how do I convert `datasets.Value` to `datasets.ClassLabel` ? One way I can think of is doing str2int inside preprocess\_function/tokenize method but curious that is there any existing conversion method to do that.

Thanks

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](https://discuss.huggingface.co/u/lewtun)\
**Post date:** [December 2, 2021, 2:36pm UTC](https://discuss.huggingface.co/t/numpy-str-error-during-training-phase/12425/2 "2021-12-02T14:36:15Z")

</div>

Hey @ivanlau I think your idea to apply `ClassLabel.str2int()` is the simplest approach, e.g.

```python
from datasets import load_dataset, ClassLabel, Features

dset = load_dataset("amazon_reviews_multi", "all_languages", split="test")
# Create ClassLabel feature
langs = dset.unique("language")
lang_feature = ClassLabel(names=langs)
# Update default features
features = dset.features
features["language"] = lang_feature
# Update dataset
dset_with_classlabel = dset.map(lambda x : {"language": lang_feature.str2int(x["language"])}, features=features)

dset_with_classlabel.features
# {'language': ClassLabel(num_classes=6, names=['de', 'en', 'es', 'fr', 'ja', 'zh'], names_file=None, id=None),
# 'product_category': Value(dtype='string', id=None),
# 'product_id': Value(dtype='string', id=None),
# 'review_body': Value(dtype='string', id=None),
# 'review_id': Value(dtype='string', id=None),
# 'review_title': Value(dtype='string', id=None),
# 'reviewer_id': Value(dtype='string', id=None),
# 'stars': Value(dtype='int32', id=None)}

```

Alternatively, you can provide the `features` dictionary when you load the dataset with `load_dataset`. Hope that helps!

---

<div class="post-metadata">

**Author:** ![ivanlau](https://avatars.discourse-cdn.com/v4/letter/i/b38774/32.png) [@ivanlau](https://discuss.huggingface.co/u/ivanlau)\
**Post date:** [December 2, 2021, 4:16pm UTC](https://discuss.huggingface.co/t/numpy-str-error-during-training-phase/12425/3 "2021-12-02T16:16:28Z")

</div>

I see… I don’t know `ClassLabel` has such method exist. I will try it then.  
Thanks for the pointer. 🙂
