Book Image

Learning SciPy for Numerical and Scientific Computing

Book Image

Learning SciPy for Numerical and Scientific Computing

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

The array object


At this point, we are ready for a thorough study of all interesting attributes of ndarray for scientific computing purposes. We have already covered a few, such as dtype, shape, and size. Other useful attributes are ndim (to compute the number of dimensions in the array), real, and imag (to obtain the real and imaginary parts of the data, should this be formed by complex numbers) or flat (which creates a one-dimensional indexable iterator from the data).

For instance, if we desired to add all the values of an array together, we could use the flat attribute to run over all the elements sequentially, and accumulate all the values in a variable. A possible code to perform this task should look like the following code snippet (compare this code with the ndarray.sum() method, which will be explained in object calculation ahead):

>>> value=0; import scipy.misc; img=scipy.misc.lena()
>>> for item in img.flat: value+=item
>>> value

The output is shown as...