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 focus on int, float, long, and complex.

NumPy has its own set of datatypes optimized to use with instances of ndarray, and with the same precision as the previously given native types. We distinguish them with a trailing underscore (_). For instance, ndarray of strings could be initialized, as follows:

>>> a=numpy.array(['Cleese', 'Idle', 'Gilliam'], dtype='str_')
>>> a.dtype

The output is shown as follows (it depends on your Python version):

dtype('<U7')

Note two things; unlike it's purely Python counterpart, the usage of the 'str_' datatype requires the name to be quoted; we could use the longer unquoted version, numpy.str_.

When prompted for datatype, the system returns its C-derived equivalent: '<U7' ('<U for strings, and 7' to indicate the largest size of any of its elements).

The most common way to address numerical types is with the bit width nomenclature: boolXX, intXX, uintXX, floatXX, or complexXX, where XX indicates the bit size (for example, uint32 for 32-bit unsigned integers).

It is also possible to design our own datatypes, and this is where the full potential of the flexibility of NumPy datatypes arise. For instance, a datatype to indicate the name and grades of a student could be created, as follows:

>>> dt = numpy.dtype([ ('name', numpy.str_, 16), ('grades',numpy.float64, (2,)) ])
>>> dt 

The output is shown as follows (it depends on your Python version):

dtype([('name', '<U16'), ('grades', '<f8', (2,))])

This means that the dt datatype has two parts: the first part, the name, that must be a numpy.str_ string with 16 characters. The second part, the grades, is a subarray of dimension 2 with scores as 64-bit floating point values. A valid array with elements in this datatype would then look like the following:

>>> MA141=numpy.array([ ('Cleese', (7.0,8.0)), ('Gilliam',(9.0,10.0)) ], dtype=dt)
>>> MA141

The output is shown as follows (it depends on your Python version):

array([('Cleese', [7.0, 8.0]), ('Gilliam', [9.0, 10.0])],dtype=[('name', '<U16'), ('grades', '<f8', (2,))])
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.226.66