Chapter 2. Working with the NumPy Array As a First Step to SciPy
At the top level, SciPy is basically NumPy, since both the object creation and basic manipulation of these objects are performed by functions of the latter library. This assures much faster computations, since the memory handling is done internally in an optimal way. For instance, if an operation must be made on the elements of a big multidimensional array, a novice user might be tempted to go over columns and rows with as many for loops as necessary. Loops run much faster when they access each consecutive element in the same order in which they are stored in memory. We should not be bothered with considerations of this kind when coding. The NumPy/SciPy operations assure that this is the case. As an added advantage, the names of operations in NumPy/SciPy are intuitive and self explanatory. Code written in this fashion is extremely easy to understand and maintain, faster to correct or change in case of need.
Let's illustrate this point with an introductory example.
The scipy.misc
module in the SciPy package contains a classical image called lena
, used in the image processing community for testing and comparison purposes. This is a 512 x 512 pixel standard test image, which has been in use since 1973, and was originally cropped from the centerfold of the November 1972 issue of the Playboy magazine. It is a picture of Lena S?derberg, a Swedish model, shot by photographer Dwight Hooker. The image is probably the most widely used test image for all sorts of image processing algorithms (such as compression and noise reduction) and related scientific publications.
This image is stored as a two-dimensional array. Note that the number in the nth column and mth row of this array measures the grayscale value at the pixel position (n+1, m+1) of the image. In the following, we access this picture and store it in the img
variable, by issuing the following commands:
>>> import scipy.misc >>> img=scipy.misc.lena() >>> import matplotlib.pyplot as plt >>> plt.gray() >>> plt.imshow(img)
The image can be displayed by issuing the following command:
>>> plt.show()

We may take a peek at some of these values; say the 7 x 3 upper corner of the image (7 columns, 3 rows). Instead of issuing for loops, we could slice the corresponding portion of the image. The img[0:3,0:7]
command gives us the following:
array([[162, 162, 162, 161, 162, 157, 163], [162, 162, 162, 161, 162, 157, 163], [162, 162, 162, 161, 162, 157, 163]])
We can use the same strategy to populate arrays or change their values. For instance, let's change all entries of the previous array to hold zeros on the second row between columns 2 to 6:
>>> img[1,1:6]=0 >>> print (img[0:3,0:7])
The output is shown as follows:
[[162 162 162 161 162 157 163] [162 0 0 0 0 0 163] [162 162 162 161 162 157 163]]