generated output
Finding help is easy
Resources
NumPy:
www.numpy.org
SciPy:
www.scipy.org
Matplotlib:
www.matplotlib.org
01
Installing NumPyMost Linux distributions have a ready-to-install package you can use. After installation, you can find out the NumPy version you are using by executing the following:
$ python
Python 2.7.3 (default, Mar 13 2014, 11:03:55) [GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or
"license" for more information.
>>> numpy.version.version
Work with Python
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'numpy' is not defined
>>> import numpy
>>> numpy.version.version '1.6.2'
>>>
Not only have you found the NumPy version but you also know that NumPy is properly installed.
02
About NumPyDespite its simplistic name, NumPy is a powerful Python package that is mainly for working with arrays and matrices.
There are many ways to create an array but the simplest is by using the array() function:
>>> oneD = array([1,2,3,4])
The aforementioned command creates a one-dimensional array. If you want to create a two-dimensional array, you can use the array() function as follows:
>>> twoD = array([ [1,2,3], ... [3,3,3], ... [-1,-0.5,4], ... [0,1,0]] )
You can also create arrays with more dimensions.
03
Making simple calculations using NumPyGiven an array named myArray, you can fi nd the minimum and maximum values in it by executing the following commands:
>>> myArray.min()
>>> myArray.max()
Should you wish to fi nd the mean value of all array elements, run the next command:
>>> myArray.mean()
Similarly, you can fi nd the median of the array by running the following command:
>>> median(myArray)
The median value of a set is an element that divides the data set into two subsets (left and right subsets) with the same number of elements. If the data set has an odd number of elements, then the median is part of the data set. On the other side, if the data set has an even number of elements, then the median is the mean value of the two centre elements of the sorted data set.
04
Using arrays with NumPyNumPy not only embraces the indexing methods used in typical Python for strings and lists but also extends them. If you want to select a given element from an array, you can use the following notation:
>>> twoD[1,2]
You can also select a part of an array (a slice) using the following notation:
>>> twoD[:1,1:3]
Finally, you can convert an array into a Python list using the tolist() function.
05
Reading fi lesImagine that you have just extracted information from an Apache log file using AWK and you want to process the text file using NumPy.
The following AWK code fi nds out the total number of requests per hour:
$ cat access.log | cut -d[ -f2 | cut -d]
-f1 | awk -F: '{print $2}' | sort -n | uniq -c | awk '{print $2, $1}' > timeN.txt The format of the text fi le (timeN.txt) with the data is the following:
00 191 01 225 02 121 03 104
Reading the timeN.txt fi le and assigning it to a new array variable can be done as follows:
aa = np.loadtxt("timeN.txt")
06
Writing to fi lesWriting variables to a fi le is largely similar to reading a fi le. If you have an array variable named aa1, you can easily save its contents into a fi le called aa1.txt by using the following command:
In [17]: np.savetxt("aa1.txt", aa1)
As you can easily imagine, you can read the contents of aa1.txt later by using the loadtxt() function.
07
Common functionsNumPy supports many numerical and statistical functions. When you apply a function to an array, the function is automatically applied to all array elements.
When working with matrices, you can fi nd the inverse of a matrix AA by typing “AA.I”. You can also fi nd its eigenvalues by typing “np.linalg.
eigvals(AA)” and its eigenvector by typing “np.
linalg.eig(BB)”.
08
Working with matricesA special subtype of a two-dimensional NumPy array is a matrix. A matrix is like an array except that matrix multiplication replaces element-by-element multiplication. Matrices are generated using the matrix (or mat) function as follows:
In [2]: AA = np.mat('0 1 1; 1 1 1; 1 1 1') You can add two matrices named AA and BB by typing AA + BB. Similarly, you can multiply them by typing AA * BB.
03
Making simple calculations90 The Python Book
Work with Python
SciPy is built on top of NumPy and is more advanced
09
Plotting with MatplotlibThe fi rst move you should make is to install Matplotlib. As you can see, Matplotlib has many dependencies that you should also install.
The fi rst thing you will learn is how to plot a polynomial function. The necessary commands for plotting the 3x^2-x+1 polynomial are the following:
import numpy as np
import matplotlib.pyplot as plt myPoly = np.poly1d(np.array([3, -1, 1]).
astype(float))
x = np.linspace(-5, 5, 100) y = myPoly(x)
plt.xlabel('x values') plt.ylabel('f(x) values') xticks = np.arange(-5, 5, 10) yticks = np.arange(0, 100, 10) plt.xticks(xticks)
plt.yticks(yticks) plt.grid(True) plt.plot(x,y)
The variable that holds the polynomial is myPoly. The range of values that will be plotted for x is defi ned using “x = np.linspace(-5, 5, 100)”. The other important variable is y, which calculates and holds the values of f(x) for each x value.
It is important that you start ipython using the “ipython --pylab=qt” parameters in order to see the output on your screen. If you are interested in plotting polynomial functions, you should experiment more, as NumPy can also calculate the derivatives of a function and plot multiple functions in the same output.
10
About SciPySciPy is built on top of NumPy and is more advanced than NumPy. It supports numerical integration, optimisations, signal processing, image and audio processing, and statistics. The example in Fig. 01 (to the left) uses a small part of the scipy.stats package that is about statistics.
The example uses two statistics distributions and may be diffi cult to understand even if you know mathematics, but it is presented in order to give you a better taste of SciPy commands.
11
Using SciPy for image processing Now we will show you how to process and transform a PNG image using SciPy.The most important part of the code is the following line:
In [36]: from scipy.stats import poisson, lognorm In [37]: mySh = 10;
In [38]: myMu = 10;
In [39]: ln = lognorm(mySh) In [40]: p = poisson(myMu) In [41]: ln.rvs((10,)) Out[41]:
array([ 9.29393114e-02, 1.15957068e+01, 9.78411983e+01, 8.26370734e-07, 5.64451441e-03, 4.61744055e-09, 4.98471222e-06, 1.45947948e+02, 9.25502852e-06, 5.87353720e-02])
In [42]: p.rvs((10,))
Out[42]: array([12, 11, 9, 9, 9, 10, 9, 4, 13, 8]) In [43]: ln.pdf(3)
Out[43]: 0.013218067177522842