Download The Python Book: The ultimate guide to coding with Python PDF

TitleThe Python Book: The ultimate guide to coding with Python
File Size28.2 MB
Total Pages180
Document Text Contents
Page 1

Over 20
The ultimate guide to coding with Python
Learn to use Python


Page 2

Page 90

90 T
he Python BookWork with Python
SciPy is built on top of NumPy
and is more advanced
09 Plotting with Matplotlib

The 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]).


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)





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 SciPy

SciPy 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,))

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,
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.013218067177522842Fig 01
09Plotting with


Page 91

The Python Book 91Work with Python
Process and transform a PNG
image using SciPy
12 Other useful functions

It is very useful to be able to fi
nd out
the data type of the elements in an array; it

can be done using the dtype() function.
Similarly, the ndim() function returns the
number of dimensions of an array.
When reading data from external fi
les, you
can save their data columns into separate

variables using the following way:
In [10]: aa1,aa2 = np.loadtxt("timeN.txt",

usecols=(0,1), unpack=True)

The aforementioned command saves column

1 into variable aa1 and column 2 into variable

aa2. The “unpack=True” allows the data to be

assigned to two different variables. Please

note that the numbering of columns starts

with 0.
14 Array broadcasting in NumPy

To close, we will talk more about
array broadcasting because it is a very

useful characteristic. First, you should know

that array broadcasting has a rule: in order

for two arrays to be considered for array

broadcasting, “the size of the trailing axes for

both arrays in an operation must either be the

same size or one of them must be one.”
Put simply, array broadcasting allows
NumPy to “change” the dimensions of an array

by fi
lling it with data in order to be able to do
calculations with another array. Nevertheless,

you cannot stretch both dimensions of an

array to do your job.
13 Fitting to polynomials

The NumPy polyfi
t() function tries to fi
t a set of data points to a polynomial. The data

was found from the timeN.txt fi
le, created
earlier in this article.
The Python script uses a fi
fth degree
polynomial, but if you want to use a different

degree instead then you only have to change

the following line:

coefficients = np.polyfit(aa1, aa2, 5)
image = np.array('SA.png').

This line allows you to read a usual PNG

fi le and convert it into a NumPy array for
additional processing. The program will

also separate the output into four parts and

displays a different image for each of these

four parts.
11 Using SciPy for

image processing
13 Fitting to


Page 179

Page 180

The ultimate guide to coding with Python
Put Python to work
Supercharge your system and make life
easier with handy coding tutorials
Use Python with Raspberry Pi
Work on any Raspberry Pi model using its
officially recognised language
Get to grips with the basics
Learn Python the right way and complete
basic projects with our simple guides
250essential tips

Similer Documents