add more labels in dtype=uint8 array in python

To add more labels to a dtype=uint8 array in Python using NumPy, you can assign the values directly to the array elements. Here is an example:

main.py
import numpy as np

# Create a uint8 array
arr = np.zeros((10, 10), dtype=np.uint8)

# Assign labels to array elements
arr[2, 3] = 1
arr[5, 7] = 2
arr[8, 1] = 3

# Print the array
print(arr)
191 chars
13 lines

Output:

main.py
[[0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 1 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 2 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 3 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]]
231 chars
11 lines

In this example, the array arr is created as a 10x10 uint8 array filled with zeros. Then, labels 1, 2, and 3 are assigned to specific elements in the array (e.g., arr[2, 3] = 1). Finally, the array is printed to verify the labels have been added correctly.

related categories

gistlibby LogSnag