Data types used with NumPy ndarrays
NumPy ndarrays are homogenous—that is, each element in an ndarray has the same data type. This is different from Python lists, which can have elements with different data types (heterogenous).
The np.array(...)
method accepts an explicit dtype=
parameter that lets us specify the data type that the ndarray should use. Common data types used are np.int32
, np.float64
, np.float128
, and np.bool
. Note that np.float128
is not supported on Windows.
The primary reason why you should be conscious about the various numeric types for ndarrays is the memory usage—the more precision the data type provides, the larger memory requirements it has. For certain operations, a smaller data type may be just enough.
Creating a numpy.float64 array
To create a 128-bit floating-values array, use the following code:
np.array([-1, 0, 1], dtype=np.float64)
The output is shown here:
array([-1., 0., 1.], dtype=float64)...