Every now and then I wrote code like this:
import numpy as np
a = np.array([1,2,3])
a[1]=3.3
a[2] *= 50
print(a)
Here I do not inted a
to be initalized as int
, but as float
, but as I said I forget it.
Now is there a way to make sure such initializations default to float, unless dtype is explicitly specified?
Not without changing the source, no. Your options are:
Get in the habit of putting dots at the end of your numbers:
np.array([1.,2.,3.])
Use dtype
explicitly:
np.array([1,2,3], dtype=float)
Make a new function:
def ozi_array(*args, **kwargs):
if 'dtype' not in kwargs:
return np.array(*args, dtype=float, **kwargs)
return np.array(*args, **kwargs)
Use the dtype
parameter see here:
>>> import numpy as np
>>> np.array([1, 2, 3], dtype=float)
array([ 1., 2., 3.])
2