Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why astype(uint) on np.array doesn't change type of an element of the np.array?

When converting an np.array to uint8 using astype the type of an element of the array doesn't change.

>>> x = np.array([[1.0, 2.3], [1.3, 2.9]])
>>> x.astype(uint8)
array([[1, 2],
       [1, 2]], dtype=uint8)
 >>> type(x[0,0])
<type 'numpy.float64'>

Why the element is still float64 and not uint8?

like image 791
Ido Avatar asked Feb 14 '23 01:02

Ido


2 Answers

astype returns a copy of the origin array.

Use x = x.astype(uint8) instead

like image 74
waitingkuo Avatar answered Feb 16 '23 15:02

waitingkuo


astype returns a copy of an array, so you must assign it:

x = x.astype(uint8)
like image 40
Krzysztof Rosiński Avatar answered Feb 16 '23 13:02

Krzysztof Rosiński