NumPy, SciPy and Python Image Library
Two related third-party packages named NumPy and SciPy [3] provide tools for creating arrays (vectors, matrices, and tensors) and a plethora of tools for manipulating these arrays. These tools provide very efficient codes that make programming easier and
- PDF / 214,837 Bytes
- 22 Pages / 439.37 x 666.142 pts Page_size
- 55 Downloads / 195 Views
NumPy, SciPy and Python Image Library
Two related third-party packages named NumPy and SciPy [3] provide tools for creating arrays (vectors, matrices, and tensors) and a plethora of tools for manipulating these arrays. These tools provide very efficient codes that make programming easier and computations quick. Since neural models consider arrays of neurons these tools are essential. A third package named Python Image Library (PIL) provides tools or reading and writing image data.
3.1 NumPy The NumPy package provides functions to create and manipulate arrays. An array is a collection of uniform data types such as vector, matrix, or tensor. In order to use the NumPy package it must first be imported. Since there will be many calls to the routines in this package it is prudent to use from—import.
3.1.1 Creating Arrays There are several ways to create a vector or matrix of which a few are shown in Code 3.1. Line 2 creates a vector with ten elements all are set to 0. Line 5 creates a ten element vector in which all of the elements are integers. Lines 8–10 demonstrate that vectors can also contain complex numbers. One of the advantages of using arrays is that global operations can be performed in a single command. Code 3.2 demonstrates that a scalar can be added to a vector in a single command (Line 2). The division of an integer array with another integer will result in an integer array as seen in Line 4. Again, this may not be an exact answer as shown in Line 6.
T. Lindblad and J. M. Kinser, Image Processing Using Pulse-Coupled Neural Networks, Biological and Medical Physics, Biomedical Engineering, DOI: 10.1007/978-3-642-36877-6_3, © Springer-Verlag Berlin Heidelberg 2013
35
36
3 NumPy, SciPy and Python Image Library
Code 3.1 Creation of vectors. >>> from numpy import zeros, ones, random >>> vec = zeros( 10 ) >>> vec array([ 0., 0., 0., 0., 0., 0., 0., 0., >>> vec = ones( 10, int ) >>> vec array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) >>> vec = zeros( 4, complex ) >>> vec array([ 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j])
0.,
0.])
Code 3.2 Math operations for vectors. >>> vec = ones( 4, int ) >>> vec + 2 array([3, 3, 3, 3]) >>> (vec + 12)/4 array([3, 3, 3, 3]) >>> (vec + 12.)/4 array([ 3.25, 3.25, 3.25,
3.25])
Code 3.3 Math operations for two vectors. >>> from numpy import set_printoptions >>> set_printoptions( precision=3) >>> vec1 = random.rand( 4 ) >>> vec2 = random.rand( 4 ) >>> vec1 array([ 0.938, 0.579, 0.867, 0.066]) >>> vec2 array([ 0.574, 0.327, 0.293, 0.649]) >>> vec1 + vec2 array([ 1.512, 0.906, 1.159, 0.716])
Code 3.3 shows three conveniences. The first is to control the precision of the print to the console. The set_printoptions command is used to limit the number of digits that are printed to the screen. This function comes from NumPy and does not apply to lists or tuples. The second item in this code is the creation of two vectors of random numbers. The random.rand function creates a vector with random numbers ranging from 0 to 1 with equal distribution. The random module provides other types of distribut
Data Loading...