Book Image

Learning SciPy for Numerical and Scientific Computing Second Edition

Book Image

Learning SciPy for Numerical and Scientific Computing Second Edition

Overview of this book

Table of Contents (15 chapters)
Learning SciPy for Numerical and Scientific Computing Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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...