0

Does anyone know how to fix the below problem? I am using google colab to run my neural network and want to create confusion matrix using below array however couldn't solve the problem.

label_names_array = np.chararray(3, itemsize = 4).decode("utf-8")

for key, value in label_names_array.items():
  label_names_array[key] = value
label_names_array

UnicodeDecodeError Traceback (most recent call last) in () ----> 1 label_names_array = np.chararray(3, itemsize = 4).decode("utf-8") 2 3 for key, value in label_names.items(): 4 label_names_array[key] = value 5 label_names_array

1 frames /usr/local/lib/python3.6/dist-packages/numpy/core/defchararray.py in decode(a, encoding, errors) 562 """ 563 return _to_string_or_unicode_array( --> 564 vec_string(a, object, 'decode', _clean_args(encoding, errors))) 565 566

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 0: invalid start byte

kenorb
  • 155,785
  • 88
  • 678
  • 743
  • Does https://stackoverflow.com/questions/22216076/unicodedecodeerror-utf8-codec-cant-decode-byte-0xa5-in-position-0-invalid-s help you? – Minh Nguyen Sep 20 '20 at 16:24
  • If you read the [documentation](https://numpy.org/doc/stable/reference/generated/numpy.char.chararray.html?highlight=chararray#numpy.char.chararray) for `chararray` you will learn (1) It's recommended that you don't use it (2) If you do use it, you shouldn't use this constructor directly. – snakecharmerb Sep 21 '20 at 06:45

1 Answers1

-1

there are several issues with your code: first of all I dont'seem to find an .items() method for a numpy array: it belongs to a Python dictionary I think. So you could build a dictionary directly from your array using a constructor and skipping the for loop:

label_names_array = np.chararray(3, itemsize=4).decode("utf-8")
labels_dict = dict(np.ndenumerate(label_names_array))

What do you think?

You started asking from a very early point however. You should consider posting some code where you show how do you use the data and how you want to construct the confusion matrix.

What about trying with scikit-learn?

Gigioz
  • 367
  • 5
  • 19