[Failure story] When I added 1 to ndarray, dtype changed unintentionally.

Premise

Bugs encountered during the development of my reinforcement learning library cpprb (issue / 112)))

It is a library for temporarily storing data for experience replay of reinforcement learning, the type of the data is specified by the user in the constructor, and internally [Numpy ndarray](https://numpy.org/doc/ It was saved as stable / reference / generated / numpy.ndarray.html).

Bug (failure)

The other day, when I added to ndarray with a code like this (strictly different), it changed to dtype unintentionally and the behavior became strange.

bug


import numpy as np

def create_buffer(shape,dtype):
    return np.zeros(shape=shape,dtype=dtype) + 1

create_buffer(1,np.bool) # array([1]) np.It's no longer a bool!! np.int64

Even if you use numpy.add instead of the operator +, the output type will change. (If you specify the original variable once stored in the output destination ʻout, numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc'add' output from dtype ('int64') to dtype ('bool') with casting rule I get angry with'same_kind'`.)

solution

By substituting the calculation result for each element as shown below (only when the type can be converted), the calculation can be performed while maintaining the original type.

Fix


import numpy as np

def create_buffer(shape,dtype):
    a = np.zeros(shape=shape,dtype=dtype)
    a[:] = a + 1
    return a

create_buffer(1,np.bool) # array([True])

Digression

There was another purpose, and I just happened to perform the addition, and the addition itself has no meaning. (The addition of bool ... is not the main subject of this article.)

To the last, the failure story that the type was unintentionally converted when the object level operation was performed on ndarray.

Recommended Posts

[Failure story] When I added 1 to ndarray, dtype changed unintentionally.
A story I was addicted to when inserting from Python to a PostgreSQL table
What I referred to when studying tkinter
A story that I was addicted to when I made SFTP communication with python