Specifying default dtype for np.array(1.)
Question:
Is there a way to specify default dtype that’s used with constructs like np.array(1.)
?
In particular I want np.array(1.)
to be np.float32
and np.array(1)
to be np.int32
. Instead I’m getting np.float64
and np.int64
Answers:
The default depends on your system. On a 64-bit system, default types will be 64-bit. On a 32-bit system, default types will be 32-bit. There is no way to change the default short of re-compiling numpy with a different system C header.
You can of course specify dtypes explicitly, e.g.
>>> x = np.array(1, dtype='int32')
Edit: as kazemakase mentions below, the above is only true for int32/int64. In recent numpy versions, the default for floating-point is float64 regardless of the system.
You could use np.float32
or np.int32
as np.ndarray
constructor:
>>> np.float32([1.])
array([ 1.], dtype=float32)
>>> np.int32([1])
array([1], dtype=int32)
but that returns a numpy-scalar if given a scalar input (not a rank 0 array):
>>> np.float32(1)
1.
>>> np.asarray(np.float32(1)) # Use np.asarray to convert it to an array
array(1.0, dtype=float32)
Redefining the default dtype seems to be not so easy, see also:
- How to set the default datatype as 'float32' for numpy & pandas?
- Can i set float128 as the standard float-array in numpy
- Python: Making numpy default to float32
If you don’t care additional overhead you can always use a dictionary as “switch” to get correct dtype for those you find inappropriate:
defaults = {np.dtype('int64'): np.int32,
np.dtype('float64'): np.float32}
before = 1.
np.array(before, dtype=defaults.get(np.result_type(before), None))
This will however fail with complicated types like characters (strings) or objects.
I just use a set of simple wrapper functions like this:
def np_array(shape):
return np.array(shape, dtype=np.float32)
def np_zeros(shape):
return np.zeros(shape, dtype=np.float32)
def np_ones(shape):
return np.ones(shape, dtype=np.float32)
etc...
Then just substitute calls to e.g. "np.array(…" with "np_array(…"
Is there a way to specify default dtype that’s used with constructs like np.array(1.)
?
In particular I want np.array(1.)
to be np.float32
and np.array(1)
to be np.int32
. Instead I’m getting np.float64
and np.int64
The default depends on your system. On a 64-bit system, default types will be 64-bit. On a 32-bit system, default types will be 32-bit. There is no way to change the default short of re-compiling numpy with a different system C header.
You can of course specify dtypes explicitly, e.g.
>>> x = np.array(1, dtype='int32')
Edit: as kazemakase mentions below, the above is only true for int32/int64. In recent numpy versions, the default for floating-point is float64 regardless of the system.
You could use np.float32
or np.int32
as np.ndarray
constructor:
>>> np.float32([1.])
array([ 1.], dtype=float32)
>>> np.int32([1])
array([1], dtype=int32)
but that returns a numpy-scalar if given a scalar input (not a rank 0 array):
>>> np.float32(1)
1.
>>> np.asarray(np.float32(1)) # Use np.asarray to convert it to an array
array(1.0, dtype=float32)
Redefining the default dtype seems to be not so easy, see also:
- How to set the default datatype as 'float32' for numpy & pandas?
- Can i set float128 as the standard float-array in numpy
- Python: Making numpy default to float32
If you don’t care additional overhead you can always use a dictionary as “switch” to get correct dtype for those you find inappropriate:
defaults = {np.dtype('int64'): np.int32,
np.dtype('float64'): np.float32}
before = 1.
np.array(before, dtype=defaults.get(np.result_type(before), None))
This will however fail with complicated types like characters (strings) or objects.
I just use a set of simple wrapper functions like this:
def np_array(shape):
return np.array(shape, dtype=np.float32)
def np_zeros(shape):
return np.zeros(shape, dtype=np.float32)
def np_ones(shape):
return np.ones(shape, dtype=np.float32)
etc...
Then just substitute calls to e.g. "np.array(…" with "np_array(…"