Using datatypes
There are several approaches to impose the datatype. For instance, if we want all entries of an already created array to be 32-bit floating point values, we may cast it as follows:
>>> import scipy.misc >>> img=scipy.misc.lena().astype('float32')
We can also use an optional argument, dtype
through the command:
>>> import numpy >>> scores = numpy.array([101,103,84], dtype='float32') >>> scores
The output is shown as follows:
array([ 101., 103., 84.], dtype=float32)
This can be simplified even further with a third clever method (although this practice offers code that are not so easy to interpret):
>>> scores = numpy.float32([101,103,84]) >>> scores
The output is shown as follows:
array([ 101., 103., 84.], dtype=float32)
The choice of datatypes for NumPy arrays is very flexible; we may choose the basic Python types (including bool
, dict
, list
, set
, tuple
, str
, and unicode
), although for numerical computations we...