Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning SciPy for Numerical and Scientific Computing Second Edition

You're reading from  Learning SciPy for Numerical and Scientific Computing Second Edition

Product type Book
Published in Feb 2015
Publisher Packt
ISBN-13 9781783987702
Pages 188 pages
Edition 1st Edition
Languages

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

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}