Sandeep Nagar Introduction To Python - For Scientists and Engineers PDF
Sandeep Nagar Introduction To Python - For Scientists and Engineers PDF
www.bookmuft.com
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
2
Introduction to Python: For Scientists
and Engineers
2
Contents
3 Data types 21
3.1 Introduction to Various types of data . . . . . . . . . . . . . . 21
3.2 Logical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3 Numeric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.3.1 Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.3.2 Floating point numbers . . . . . . . . . . . . . . . . . 24
3.3.3 complex Numbers . . . . . . . . . . . . . . . . . . . . 25
3.4 Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.4.1 String . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
3.4.2 list and tuples . . . . . . . . . . . . . . . . . . . . . . 27
3.5 Set and Frozen Set . . . . . . . . . . . . . . . . . . . . . . . . 28
3.6 Mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.7 Null object . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4 Operators 31
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4.2 Concept of variables . . . . . . . . . . . . . . . . . . . . . . . 32
4.2.1 Rules of naming variables . . . . . . . . . . . . . . . . 33
4.3 Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . 34
4.4 Arithmetic operators . . . . . . . . . . . . . . . . . . . . . . . 35
4.5 Changing and defining data type . . . . . . . . . . . . . . . . 37
4.6 Order of usage . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4.7 Logical operators . . . . . . . . . . . . . . . . . . . . . . . . . 37
4.8 Membership Operator . . . . . . . . . . . . . . . . . . . . . . 38
4.9 Identity Operator . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.10 Bitwise operators . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.10.1 Using bitwise operations . . . . . . . . . . . . . . . . . 41
4.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
5 Arrays 43
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.2 Numpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
5.3 ndarray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.4 Automatic creation of arrays . . . . . . . . . . . . . . . . . . 47
5.4.1 zeros . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.4.2 ones . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.4.3 ones like . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.4.4 empty . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.4.5 empty like . . . . . . . . . . . . . . . . . . . . . . . . . 49
5.4.6 eye . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
5.4.7 identity . . . . . . . . . . . . . . . . . . . . . . . . . . 50
5.4.8 full . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
5.4.9 full like . . . . . . . . . . . . . . . . . . . . . . . . . . 50
5.4.10 random . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5.4.11 diagonal . . . . . . . . . . . . . . . . . . . . . . . . . . 53
5.5 Numerical ranges . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.5.1 arange . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.5.2 linspace . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
5.5.3 logspace . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.5.4 meshgrid . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.5.5 mgrid and ogrid . . . . . . . . . . . . . . . . . . . . . 57
5.5.6 tile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.6 Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5.7 Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.8 Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.9 Copies and views . . . . . . . . . . . . . . . . . . . . . . . . . 63
5.10 Masking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.10.1 Fancy indexing . . . . . . . . . . . . . . . . . . . . . . 64
5.10.2 Indexing with Boolean arrays . . . . . . . . . . . . . . 65
5.11 Arrays are not matrices . . . . . . . . . . . . . . . . . . . . . 65
5.12 Some basic operations . . . . . . . . . . . . . . . . . . . . . . 69
5.12.1 sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
5.12.2 Minimum and maximum . . . . . . . . . . . . . . . . . 70
5.12.3 Statistics: mean median and standard deviation . . . 71
5.12.4 sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
5.12.5 Rounding off . . . . . . . . . . . . . . . . . . . . . . . 72
5.13 asarray and asmatrix . . . . . . . . . . . . . . . . . . . . . . . 73
5.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
6 Plotting 75
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
6.2 Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
6.2.1 Build Dependencies . . . . . . . . . . . . . . . . . . . 77
6.2.2 pylab versus pyplot . . . . . . . . . . . . . . . . . . . . 77
6.3 Plotting basic plots . . . . . . . . . . . . . . . . . . . . . . . . 78
6.3.1 Plotting more than one graph on same axes . . . . . . 80
6.3.2 Various features of a plot . . . . . . . . . . . . . . . . 80
6.4 Setting up to properties . . . . . . . . . . . . . . . . . . . . . 85
6.5 Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
6.6 Bar charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
6.7 Error Bar Charts . . . . . . . . . . . . . . . . . . . . . . . . . 89
6.8 Scatter plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
6.9 Pie Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
6.10 Polar Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
6.11 Decorating plots with text, arrows and annotations . . . . . . 94
6.12 Subplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
6.13 Saving plot to a file . . . . . . . . . . . . . . . . . . . . . . . . 97
6.14 Displaying plots on web application servers . . . . . . . . . . 98
5
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
6.15 Working with matplotlib in object mode . . . . . . . . . . . . 100
6.16 Logarithmic plots . . . . . . . . . . . . . . . . . . . . . . . . . 102
6.17 Two plots on same figure with atleast one axis different . . . 104
6.18 Contour plots . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
6.19 3D plotting in matplotlib . . . . . . . . . . . . . . . . . . . . 107
6.19.1 Line and scatter plots . . . . . . . . . . . . . . . . . . 107
6.19.2 Wiremesh and Surface plots . . . . . . . . . . . . . . . 109
6.19.3 Contour plots in 3D . . . . . . . . . . . . . . . . . . . 111
6.19.4 Quiver plots . . . . . . . . . . . . . . . . . . . . . . . . 112
6.20 Other libraries for plotting data . . . . . . . . . . . . . . . . . 113
6.20.1 Plotly . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
6.21 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
6
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
9 Numerical Computing formalism 135
9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
9.2 Physical problems . . . . . . . . . . . . . . . . . . . . . . . . 136
9.3 Defining a model . . . . . . . . . . . . . . . . . . . . . . . . . 136
9.4 Python Packages . . . . . . . . . . . . . . . . . . . . . . . . . 139
9.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
7
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
License
License type:
information
Attribution-NonCommercial-ShareAlike 4.0 Inter-
national License
Present book is presented under Creative Commons
Attribution-NonCommercial-ShareAlike 4.0 International
license under which:
You are free to:
Share copy and redistribute the material in any medium
or format Adapt remix, transform, and build upon the
material
More information at http://creativecommons.org/
licenses/by-nc-sa/4.0/
8
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
9
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
10
Introduction to pythonic way of life
1
1.1 Introduction
11
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
1.2 History
Development of python programming language dates back to 1980s. Its
implementation was started by Guido van Rossum at CWI in the Nether-
lands in December 1989. This was an era when computing devices were
becomes powerful and reliable every day. Python 1.0 was released to public
in 1994, Python 2.0 in 2000 and Python 3.0 in 2008 but Python 3 was not
made to be back compatible with Python 2 which made it less usable to
users who were already developing with Python 2. This made a lot of devel-
opers stick to Python 2 even now with very few taker of Python 3 in general.
12
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
Another aspect of engineering problems is ability of a programming lan-
guages to communicate with physical devices efficiently. Electronic devices
are connected via wires, blue-tooth, wireless and Internet. Using an ap-
propriate python module, one can connect to a compatible device to de-
rive data from it and then visualize it in desired platform. A variety of
micro-controllers (like Arduino) allow python to run its hardware with ease.
Micro-computers like Raspberry Pi allow running python programs access-
ing the input-output devices. This enables cost effective prototyping of an
engineering problem.
Users of M AT LAB
R
argue that Simulink is one of the easiest way of
prototyping and simulating a machine. Scilab also provide a similar platform
called Xcos. Python still lacks this ability and budding programmers from
coming generations can take this up as a challenge. A large community of
developers are eagerly waiting for such a solution.
13
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
simple for developer working with ubuntu, one simply needs to write
1 $ sudo p i p i n s t a l l numpy
Mentioning use of each modules is beyond the scope of present book. Mod-
ules will be introduced as per requirement of the topic. User is encourage
of check various modules and their documentation for usage. A general use
of modules and their function will be dealt at later point in present book.
1.5 summary
Python has gained a lot of attention world-wide owing to its flat learning
and steep development curves. It has gained number one spot in recent
times in terms of popularity and choice of programming language. Owing
to a large base of developers due to open-source model, it has a rich library
of modules for various tasks required to solve an engineering problem at
hand. Hence python educated engineers can fulfill the demands of modern
industry which demands fast and efficient solutions to their problems.
14
Introduction to basics of python
2
2.1 Introduction to python as an interpreted lan-
guage
Python is an interpreted language
as opposed to compiled languages
like C, C++, Java etc. Each line
of code is interpreted and executed
one by one, as per their order. This
makes the architecture of computa-
tion quite different than traditional
languages. For example, suppose
line 5 of a python program has syn-
tax error, in this case the program
will executes all commands till line
4 and will then show an error. In case of compiled languages, the compilation
steps would show error and program will not run at all. To understand this
difference elaborately one needs to understand the processes of compilation
and interpretation.
15
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
3 #i n c l u d e <s t d i o . h>
4
5 main ( )
6 {
7 p r i n t f ( H e l l o World ) ;
8
9 }
./hello
The object file can be shared by the user with anybody and if the mi-
croprocessor architecture is same as that of user then, it will be executed
uniformly.
But this is not the case with python. Being interpreted language, it
employs an interpreter which interprets the code into an intermediate code
and then to machine code. An interpreter reads the source text of a program,
analyzes it, and executes it one line at a time. This process is very slow as
the interpreter spends a lot of time in analyzing strings of characters to
figure out what they mean. For example, to type hello world as done by
above C program, a python program will simply require:
1 p r i n t h e l l o world
In just one line, an interpreter scans the world print and looks for what
it means. In python interpreter, it means to print to a particular device.
A device can be a computer terminal, printer plotter etc. By default, its
16
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
a computer terminal. Print commands also demands arguments which is
scanned in second step as a string hello world (A string in python can be
enclosed in or ). Hence the complete interpretation of the line is to print
the string hello world on a computer terminal.
Despite these odds of being inherently slow, it has becomes favorite amongst
scientists and engineers for being extremely simple, intuitive and powerful
due to rich library of modules for various computational tasks. Present
chapter will discuss some of them in detail.
2.2 Installation
To work with python, it must be installed first. Present book is written us-
ing Ubuntu 14.04 system where python comes per-installed. In case of other
systems, user is advised to visit (https://www.python.org/downloads/)
and download python 2.x where x shows the version number of python.
Users who wish to work in Integrated Development Environment (IDEs)
needs to explore the website at (http://en.wikipedia.org/wiki/Comparison_
of_integrated_development_environments#Python). Canopy is quite con-
venient python IDE and its academic version is free for students and teach-
ers at an educational institution. User is free to install any one of them
and run python commands at command prompt or save a script file with
filename.py extension and then run the command python filename.py
at the command prompt.
17
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
5 >6.0
2.4 Modules
Python multi-verse has expanded with thousands of modules and being
open source, most of them are readily available too. Modules are collec-
tion of python programs to accomplish specific tasks. For example, numpy
has various facilities for numerical computation which was further expanded
into scipy for scientific computation in general. matplotlib is acronym
for mathematical plotting library, which has rich features to plot a vari-
ety of publication-ready graphs. pandas is the library for data analysis,
scikit-learn for machine learning, scikit-image for image processing
sympy for symbolic computing etc.
18
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
Running a simple command:
installs the program pip first. It can then be used to install a package
say numpy by simply issuing the command
Replacing the name of package with the desired package will simply do
the trick of installing the packages hassle free.
Installing scipy stack is most useful for the present book because it in-
stall a variety of programs which will be used henceforth. It can be installed
by issuing the command on an Ubuntu terminal:
2.5 Summary
Python has an extremely flat learning curves owning to the fact that its
interpretive language due to which can be insert instructions line by line and
run them subsequently. This methods avoids compilation and subsequent
errors which prove to be major stumbling block for a beginner, who has
limited knowledge of the inner workings of the programming language.
19
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
20
Data types
3
3.1 Introduction to Various types of data
3.2 Logical
logical: This type of data stores boolean values True or False boolean
values and can be operated by boolean operators like AND, OR etc. Most
programming languages use the values 1 or 0 for boolean values but python
differs in this approach.
21
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
3.3 Numeric
Numeric: There are four types of numeric data types:
int: Integers
3.3.1 Integer
Python has arbitrary precision for float, long, complex, hence the
limit to length of these numbers is subject to availability of memory. The
positive side of this architecture is that one is not limited to a range of num-
bers. But one must always ensure that sufficient memory is available during
the calculation, to avoid erroneous results. Python 2 limits the size of int
to bytes (to same size as C programming language), whereas Python 3 has
merged int and long as int. On a 32-bit system, Python 2 stores int as
32 bits. The range of integers can be obtained using the module sys, whose
function maxint returns the value of maximum values of integer stored by
python.
1 >>>import s y s
2 >>>s y s . maxint
3 2147483647
4 >>>s y s . maxint+1
5 2147483648L
6 >>>1s y s . maxint
7 2147483648
8
22
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
Python doesnt have built-in unsigned types for integers as with some
other programming languages like C. To make a negative number positive,
one can simply use abs() function like abs(-1) is obtained as 1.
23
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
3.3.2 Floating point numbers
In computing, floating point notation is a scheme of representing an
approximation of a mathematical real number. This scheme can trade-
off between range and precision. A real number is usually written with a
decimal point. For example, 2 is an integer whereas 2.0 is a real number.
These two numbers are quite different for a computer. Whereas 2 will be
stored as int type, 2.0 will be stored as float type.
1 >>>type ( 2 )
2 int
3
4 >>>type ( 2 . 0 )
5 float
The issue with floating point number based arithmetic is that the answer
is an approximation of real number since real numbers are defined for 10
as their base whereas computer works with numbers where 2 is used as the
base. For example: 0.123 is defined as
1 2 3
+ 0.123
+
101 102 103
in the number system with base 10 whereas in number system with base 2,
it is represented as
1 2 3
0.123 + +
21 22 23
Above calculation shows that 0.1232 = 0.13510 . If one uses more number
of bits to store the value, one gets a better approximation of the real num-
ber, but one is always limited to use approximated values instead of real
values.
1 >>>from d e c i m a l import Decimal
2 >>>Decimal ( 0 . 1 2 3 )
3 Decimal ( 0 . 1 2 2 9 9 9 9 9 9 9 9 9 9 9 9 9 9 8 2 2 3 6 4 3 1 6 0 5 9 9 7 4 9 5 3 5 3 2 2 1 8 9 3 3 1 0 5 4 6 8 7 5
)
4 >>>Decimal ( 1 . 2 3 4 5 )
5 Decimal ( 1 . 2 3 4 4 9 9 9 9 9 9 9 9 9 9 9 9 3 0 7 2 2 0 8 3 2 6 3 3 9 0 2 3 1 8 7 7 5 6 5 3 8 3 9 1 1 1 3 2 8 1 2 5
)
6 >>>type ( Decimal ( 0 . 1 2 3 ) )
7 Out [ 3 9 ] : d e c i m a l . Decimal
24
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
One can use the decimal module which has a function Decimal() that re-
turns the number as stored by the computer. As seen above 0.123 is stored as
0.1229999999999999982236431605997495353221893310546875 which is still
an approximation of the real number 0.123. For most cases, the error is
insignificant and one can ignore the fact that calculations using digital com-
puter (running on binary system of number) has introduced some error. But
for some cases, this error is significant and one must take proper measures
to calculate this error and counter the same.
1 >>>s y s . g e t s i z e o f ( Decimal ( 0 . 1 2 3 ) )
2 72
3 >>>s y s . g e t s i z e o f ( 0 . 1 2 3 )
4 24
5 >>>p r i n t %e %(Decimal ( 0 . 1 2 3 ) )
6 1 . 2 3 0 0 0 0 e 01
7 >>>p r i n t %f % ( 0 . 1 2 3 )
8 0.123000
25
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
8 (2+3 j )
3.4 Sequences
Any symbol which requires storage is known as a character. Everything
that appears on computer screen and printed papers, is considered character
in programming languages. This includes ASCII and extended ASCII char-
acters. Examples of characters include letters, numeral digits, whitespace,
punctuation marks, exclamation mark etc. In general, all keys on keyboard,
produce characters.
3.4.1 String
A string is simply a sequence of 8 bit characters. Lower case and upper
case characters has different encoding hence strings are case sensitive.
1 >>>type ( a )
2 str
3 >>>type ( abba )
4 str
5 >>>type ( a )
6 str
7 >>>type ( abba )
26
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
8 str
27
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
3.5 Set and Frozen Set
The set data type is implementation of mathematical set. It is an un-
ordered collection of objects. Unlike sequence objects like list and tuple,
where elements are ordered, sets do not have such requirements. Sets do not
permit duplicity in occurrence of an element, i.e an element wither exist 0
or 1 times.
1 >>>s e t ( [ h , e , l , l , o ,1 ,2.0 ,3+4 j ] )
2 {1 , 2 . 0 , e , h , l , o , (3+4 j ) }
3 >>>f r o z e n s e t ( [ h ,0 ,1.0 ,2+3 j ] )
4 frozenset ({0 , h , (2+3 j ) , 1 . 0 } )
Please note that an since l occurred two times while defining the set, it
was gives only one membership. Set operations are discussed in detail in
subsequent chapter. A frozen set is simply immutable set.
3.6 Mappings
Mapping is a scheme of defining data where each element is identified with
a key called hash tag. The element can be accessed by referring to the
key. One of the data type in this category is a dictionary.
28
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
In example above, we created a dictionary containing two characters a
and b identified by two keys 1 and 10. Subsequent commands checks if
characters a, b, c are past of the dictionary to which we get an answer
True for a and b but False for c. A variety of operators can operate on
dictionaries as discussed in subsequent chapter.
3.8 Summary
Object oriented programming uses the fact that all computing entities are
merely objects which interact with each other as per their defined behavior.
Some built-in data types have been discussed in present chapter. Some data
types are defined inside the modules. One can define ones own data types
and define its properties. Before going to these advanced topics, ti will be
useful to know how operators operate on various kinds of data. This will be
subject of next chapter.
29
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
30
Operators
4
4.1 Introduction
Operators work in similar fashion
as mathematical functions. They
provide a relationship between two
different domains. For example,
multiplication operator makes an
ordered pair of operands (data on
which operator works) and produce
another data point. This can be
done to any number of data points.
In this way, operator transforms
data from domain of operands to
domain of results.
In the domain of numerical calculations, one needs to use basic and com-
plex mathematical functions/operators like multiplication (*), addition (+),
subtraction (-), division (/), Modulus (%), Exponentiation (**) etc. These
operators can be combined in complex manner to perform an arithmetic
operation. Depending on data type, they define thier functionality. For ex-
ample, on numeric data + performs numeric addition where as on a string
it will perform concatenation.
1 >>> 2+3
2 5
3 >>> a + b
31
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
4 ab
5 >>> h e l l o + + world + !
6 h e l l o world !
There are a variety of operators which can operate on data types as dis-
cussed in chapter 3.
32
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
Python variables needs not be explicitly defined for their type.
Using the module keyword, one can obtain the list of keywords using the
function keyword.kwlist. The code given in keyword.py gives a list of
keywords, which cannot be used as variable names.
1 import keyword
2
3 p r i n t Python keywords : , keyword . k w l i s t
Keyword.py
33
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
4.3 Assignment Operator
The concept of variables used a symbol = which is not same as equal to
in mathematics. Instead its one of the assignment operator. Below is the
list of assignment operators.
Operator Example
= v = a+b
+= v +=a v = v + a
-= v -=a v = v - a
/= v /=a v = v / a
//= v //=a v = v // a
*= v *=a v = v * a
**= v **=a v = v ** a
%= v %=a v = v % a
Multiple assignment within the same statement can be done using = op-
erator as follows:
1 a = b = c = 10
2 a
3 10
4 b
5 10
6 c
7 10
While assigning a value, its data type need not be explicitly defined. It
is judged by python interpreter by the data itself. i.e. 4.0 will be taken as
floating point number, 4 will be taken as integer, A single character a or a
group of characters like sandeep will be taken as a string. This is shows in
the following example of code.
1 a = 4 . 0 ; type ( a )
2 float
3
4 a = 4 ; type ( a )
34
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
5 int
6
7 a = 4 e1 ; type ( a )
8 float
9
10 b = a ; type ( a )
11 int
12
13 b = a ; type ( b )
14 str
15
16 b = sandeep ; type ( b )
17 str
35
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
1 >>> h e l l o + + world + !
2 h e l l o world !
3 >>> [ 1 , 2 , 3 ] + [ 4 , 5 , 6 ]
4 [1 , 2 , 3 , 4 , 5 , 6]
5 >>> h e l l o 3
6 hellohellohello
7 >>> [ 1 , 2 , 3 ] 3
8 [1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3]
9 >>> h e l l o /3
10 Traceback ( most r e c e n t c a l l l a s t ) :
11 F i l e <s t d i n > , l i n e 1 , i n <module>
12 TypeError : unsupported operand type ( s ) f o r / : s t r and i n t
13 >>> [ 1 , 2 , 3 ] // 3
14 Traceback ( most r e c e n t c a l l l a s t ) :
15 F i l e <s t d i n > , l i n e 1 , i n <module>
16 TypeError : unsupported operand type ( s ) f o r / / : l i s t and i n t
17 >>> h e l l o % 3
18 Traceback ( most r e c e n t c a l l l a s t ) :
19 F i l e <s t d i n > , l i n e 1 , i n <module>
20 TypeError : not a l l arguments c o n v e r t e d during s t r i n g formatting
21 >>> [ 1 , 2 , 3 ] % 3
22 Traceback ( most r e c e n t c a l l l a s t ) :
23 F i l e <s t d i n > , l i n e 1 , i n <module>
24 TypeError : unsupported operand type ( s ) f o r %: l i s t and i n t
25 >>> [ 1 , 2 , 3 ] 3
26 Traceback ( most r e c e n t c a l l l a s t ) :
27 F i l e <s t d i n > , l i n e 1 , i n <module>
28 TypeError : unsupported operand type ( s ) f o r : l i s t and i n t
29 >>> h e l l o 3
30 Traceback ( most r e c e n t c a l l l a s t ) :
31 F i l e <s t d i n > , l i n e 1 , i n <module>
32 TypeError : unsupported operand type ( s ) f o r : s t r and i n t
Using + and * operator on strings and list produce the effect of concate-
nation. Whereas + concatenates the two or more strings or list objects it is
operated upon, * concatenates them m number of times where m is an inte-
ger used after the operators. The behaviour of * on strings and lists can be
understood if one considers mathmatical multiplication in terms of addition
i.e. 25 = 2+2+2+2+5. Hence multiplication with m means adding m times.
36
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
4.5 Changing and defining data type
Data types of objects can be changed as per their definitions.
1 >>>i n t ( 4 . 2 3 4 5 )
2 4
3
4 >>>i n t ( 4 . 7 3 4 5 )
5 4
6
7 >>>f l o a t ( 4 )
8 4.0
9
10 >>>f l o a t ( sandeep )
11
12 ValueError Traceback ( most r e c e n t c a l l l a s t )
13 <ipython input 26e b a b 5 3 f a f 0 b c > i n <module >()
14 > 1 f l o a t ( sandeep )
15
16 V a l u e E r r o r : c o u l d not c o n v e r t s t r i n g t o f l o a t : sandeep
37
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
Operator Symbol Operator meaning Example
== equal to 1==1 is True, 1==2 is False
!= not equal to 1!=1 is False, 1==2 is True
<> not equal to 1==1 is False, 1==2 is True
< less than 1<2 is True, 2<1 is False
> greater than 1>2 is False, 2>1 is True
<= less than equal to 1<=1 is True, 1<=2 is True
>= greater than equal to 1>=1 is True, 1>=2 is False
The result of logical operators is either of the two binary objects aptly
named True and False. In some programming languages, binary operators
are represented as 1 and 0. They can also be compared for equality.
1 >>> not True
2 False
3 >>> a = True
4 >>> b = F a l s e
5 >>> a and b
6 False
7 >>> a o r b
8 True
9 >>>
10 >>1 >= 2 == 2 >= 1
11 False
12 >>1 >= 2
13 False
14 >>>2 >= 1
15 True
16 >>>F a l s e == True
17 False
18 >>>F a l s e > True
19 False
20 >>>F a l s e < True
21 True
38
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
2 True
3 >>> name i n h e l l o world
4 False
5 >>> a=3
6 >>> b = [ 1 , 2 , 3 , 4 , 5 ]
7 >>> a i n b
8 True
9 >>> 10 i n b
10 False
At line 1, both objects i.e. 1 are int type whereas at line 3, left hand
side has int and right hand side has float. Hence the result for 1 is 1.0
is given by boolean value False. At line 5, both 1 and 2 are int, hence the
result is true again.
39
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
4 False
AND 0 1
0 0 0
1 0 1
OR 0 1
0 0 1
1 1 1
XOR 0 1
0 0 1
1 1 0
NOT 0 1
1 0
The use of these operators is mentioned below
40
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
1 >>> p r i n t b i n ( 1 ) , o c t ( 1 ) , hex ( 1 )
2 0 b1 01 0 x1
3 >>> b i n ( 1 0 )
4 0 b1010
5 >>> b i n ( 1 )
6 0 b1
7 >>> 0 b1010 >> 0 b1
8 5
9 >>> b i n ( 5 )
10 0 b101
11 # 0 b1010 t r a n s f o r m s i n t o 0 b101 by s h i f t i n g one b i t s t o r i g h t
12 >>> b i n ( 2 )
13 0 b10
14 >>> 0 b1010 >> 0 b10
15 2
16 >>> b i n ( 2 )
17 0 b10
18 # 0 b1010 t r a n s f o r m s i n t o 0 b10 by s h i f t i n g two b i t s t o r i g h t
19 >>> 0 b1010 << 0 b1
20 20
21 >>> b i n ( 2 0 )
22 0 b10100
23 >>> 0 b1010 << 0 b10
24 40
25 >>> b i n ( 4 0 )
26 0 b101000
27 # h e r e l e f t s h i f t i n g i s done by adding 0 s t o r i g h t
28 >>> 0 b1010 & 0 b10
29 2
30 >>> b i n ( 2 )
31 0 b10
32 # AND (&) i s 1 o n l y i f both o f i t s i n p u t s a r e 1 , o t h e r w i s e i t s
0
33 # The z e r o b i t s i n t h i s c a s e e f f e c t i v e l y a c t a s a f i l t e r ,
f o r c i n g t h e b i t s i n t h e r e s u l t t o be z e r o a s w e l l
34 >>> 0 b1010 | 0 b10
35 10
36 >>> b i n ( 1 0 )
37 0 b1010
38 OR i s 1 i f one o r both o f i t s i n p u t s a r e 1 , o t h e r w i s e i t s 0
41
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
a specific memory address) that control whether an interrupt is enabled or
disabled. When an interrupt is enabled, signals can be communicated. Inter-
rupts are enabled by setting the enable bit for that particular interrupt and
most importantly, not modifying any of the other bits in the register. When
an interrupt communicates with a data stream, it typically sets a bit in a sta-
tus register so that a single service routine can determine the precise reason
for the interrupt. Testing the individual bits allows for a fast decode of the
interrupt source. This is where bit operations comes handy. Shift operators
are used to shift the bits as per a formula whereas AND and OR operations
are used to check the status of bits at a specific location. The same concept
is used to alter the system file permission. In Linux file system, each file has
a number called its mode, which indicates the permission about accessing
the file. This integer can be retrieved in a program to know the status of
permissions for the file. Example: if ((mode & 128) != 0) {<do this>}
will check the mode by checking if an appropriate bit is 0, in 128 bit-system.
Bitwise operations are also preferred for their speed of operation since they
directly operate on bits in the memory.
4.11 Summary
Operators plays a very important part in computing as they provide the
backbone of defining pathways for computing. All mathematical functions
are expressed wither by individual operators or by combination of them. For
a programming language that caters to a variety of fields like science, engi-
neering, business, arts etc, a lot of different kinds of operators are needed.
Python is now being applied in various dimensions of life and maturing with
rich library of in-built as well as module wise operators.
42
Arrays
5
5.1 Introduction
Most often during scientific com-
putation, a series of numbers needs
to be operated upon together. The
list data type stores a number
of values within the same variable
name. All elements of list can be
accessed by their index. But indi-
vidual list elements can belong to
any data type. Hence a new kind
of object needs to be defined, simi-
lar to list, but which stores only nu-
meric values. This data type is called an array.
The numpy modules carries a unique object class called array. It carries
only one data type as per its initial definition in the program. The concept
of using arrays to store numerals, gave rise to a powerful idea of array
based computing. The origins of this method can be traced back to matrix
algebra. A matrix is also a collection of numbers. Similar to matrices,
arrays can be multi dimensional and can be used to be operated upon by
operators defined same as that for mathematical matrices. Using matrices,
problems involving a system of equations can be solved i.e. solving many
equations (which can even be coupled to each other) in one instance. Using
43
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
the method of indexing of elements, particular elements can be accessed for
operations. Using the concept of slicing, array dimensions can be altered
as per requirements. Using operators acting on this object, mathematical
formulations can be implemented. Present chapter will discuss the use of
arrays for mathematical computations.
5.2 Numpy
The numpy package contains various items which can be used for numeri-
cal computation, hence the name numerical python. NumPy originated from
Numeric which was originally created by Jim Hugunin along with contri-
butions from several other developers. Travis Oliphant created NumPy in
2005, by incorporating features of the competing Numarray into Numeric,
with extensive modifications. numpy is released under open source license.
Present chapter has been tested for version 1.8.2 (stable at 1 March 2015).
Line 1, import whole module named numpy for our use. Line 2 uses a
function version which further uses further a function version to find out
the installed version of numpy on the system. Users are encouraged to check
their version of python.
44
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
4 add newdocs . pyc core d u a l . py i m p o r t t o o l s . py
lib m a t l i b . pyc p o l y n o m i a l testing
5 compat c t y p e s l i b . py d u a l . pyc i m p o r t t o o l s . pyc
linalg matrixlib random tests
6 c o n f i g . py c t y p e s l i b . pyc f2py init . py
ma numarray s e t u p . py v e r s i o n . py
Now by issuing UNIX shell command cd numarray, one can see how
python programs are defined to work with arrays. Explanation of these
programs is beyond the scope of this introductory text on python, but the
digital adventurer would like to explore them on a text editor to understand
how modules work and in particular, how arrays in numpy works.
5.3 ndarray
ndarray is the main object of numpy, which is homogeneous multidimen-
sional array. It is termed homogeneous since it can contain only one data
type. Also it can be multidimensional as seen in examples below. They are
indexed by a tuple of positive integers.
1 >>>a = [ 1 , 2 , 3 ]
2 >>>a
3 [1 , 2 , 3]
4 >>>ty pe ( a )
5 list
6 >>>import numpy
7 >>>b = numpy . a r r a y ( [ 1 , 2 , 3 ] )
8 >>>b
9 array ( [ 1 , 2 , 3 ] )
10 >>>type ( b )
11 numpy . nd array
12 >>>b . dtype
13 dtype ( i n t 3 2 )
14 >>>c = numpy . a r r a y ( [ 1 . 0 , 2 . 0 , 3 . 0 ] )
15 >>>c
16 array ( [ 1. , 2. , 3.])
17 >>>c . dtype
18 dtype ( f l o a t 6 4 )
45
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
Above example explains how list and arrays in numpy are created dif-
ferently. The numpy object array takes a list as input. Line 12 explains that
element of array b are of the type int32 i.e. 32 bit integers. Similarly, c
is defined to have floating point numbers. The data type can be defined at
the time of creation too.
1 >>>a1 = numpy . a r r a y ( [ 1 , 2 , 3 ] , dtype=f l o a t )
2 >>>a1
3 array ( [ 1. , 2. , 3.])
4 >>>a1 . dtype
5 dtype ( f l o a t 6 4 )
6 >>>a2 = numpy . a r r a y ( [ 1 , 2 , 3 ] , dtype=complex )
7 >>>a2
8 array ( [ 1.+0. j , 2.+0. j , 3.+0. j ] )
9 >>>a2 . dtype
10 dtype ( complex128 )
ndarray is also known by its alias array. Apart from knowing the data
type using dtype, there are a variety of methods to get information about
various attributes of ndarray
The above table can be understood using the code below. We define a 3
array named a3.
1 >>>a3 = numpy . a r r a y ( [ ( 1 , 2 , 3 ) , ( 4 , 5 , 6 ) , ( 2 , 7 , 8 ) ] )
2 >>>a3
3 array ( [ [ 1 , 2 , 3] ,
4 [4 , 5 , 6] ,
5 [2 , 7 , 8]])
6 >>>a3 . ndim
7 2
8 >>>a3 . s i z e
9 9
10 >>>a3 . shape
46
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
11 ( 3L , 3L)
12 >>>a3 . dtype
13 dtype ( i n t 3 2 )
14 >>>a3 . i t e m s i z e
15 4
16 >>>a3 . data
17 <readw r i t e b u f f e r f o r 0x00000000085BAFE0 , s i z e 3 6 , o f f s e t 0 a t
0 x0000000009A1D2D0>
18 >>>a3 . r e s h a p e ( 1 , 9 )
19 array ( [ [ 1 , 2 , 3 , 4 , 5 , 6 , 2 , 7 , 8 ] ] )
20 # r e s h a p e s a 3 X 3 a r r a y t o 1 X 9 a r r a y ( 1 row and 9 coloumns )
21 >>>a3 . r e s h a p e ( 9 , 1 )
22 array ( [ [ 1 ] ,
23 [2] ,
24 [3] ,
25 [4] ,
26 [5] ,
27 [6] ,
28 [2] ,
29 [7] ,
30 [8]])
31 # r e s h a p e s a 3 X 3 a r r a y t o a 9 X 1 a r r a y ( 9 rows and 1 coloumns
)
32 >>>a3 . r e s h a p e ( 9 , 1 ) i s a3 . r e s h a p e ( 1 , 9 )
33 False
34 # r e s u l t i s f a l s e b e c a u s e both a r r a y s have d i f f e r e n t s h a p e s
5.4.1 zeros
To create an array where all elements are 0, we use zeros() function.
1 >>>z e r o s ( ( 3 , 4 ) , dtype=f l o a t )
2
3 array ( [ [ 0. , 0. , 0. , 0.] ,
4 [ 0. , 0. , 0. , 0.] ,
5 [ 0. , 0. , 0. , 0.]])
47
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
During initialization to zero values for matrix computations, zeros()
function is extensively used.
5.4.2 ones
To create an array where all elements are 1, we use ones() function.
1 >>>o n e s ( ( 3 , 4 ) , dtype=f l o a t )
2
3 array ( [ [ 1. , 1. , 1. , 1.] ,
4 [ 1. , 1. , 1. , 1.] ,
5 [ 1. , 1. , 1. , 1.]])
5.4.4 empty
empty() returns a new array of given shape and type, without initializing
entries.
1 >>>empty ( ( 2 , 2 ) )
2
3 array ( [ [ 1 . 8 5 3 2 3 2 3 3 e 316 , 1 . 4 8 5 2 3 1 6 9 e 316] ,
4 [ 1 . 4 8 5 2 3 1 6 9 e 316 , 3.15208230 e 316]])
48
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
5.4.5 empty like
Taking cue from an existing array, empty_like() creates an empty array
of similar shape and type.
1 >>>a = np . a r r a y ( [ [ 1 . 1 , 2 . 2 , 4 . 1 ] , [ 2 . 5 , 5 . 2 , 6 . 4 ] ] )
2 >>>a
3
4 array ( [ [ 1.1 , 2.2 , 4.1] ,
5 [ 2.5 , 5.2 , 6.4]])
6
7 >>>e m p t y l i k e ( a )
8
9 array ( [ [ 4 . 5 4 8 9 2 8 2 3 e +174 , 1 . 7 7 2 8 9 9 9 7 e +160 , 6.56350603 e
091] ,
10 [ 7 . 6 7 5 4 7 1 1 4 e 042 , 4 . 5 7 7 4 9 9 9 7 e 315 , 2.47032823 e 323]])
5.4.6 eye
Similar to a identity matrix, eye() returns a two dimensional array where
diagonal elements are 1.
1 >>>eye ( 3 , k=0)
2
3 array ( [ [ 1. , 0. , 0.] ,
4 [ 0. , 1. , 0.] ,
5 [ 0. , 0. , 1.]])
6
7 # k i s index of d i a g i n a l
8 # k = 0 means d i a g o n a l i n c e n t e r
9 # k = p o s i t i v e i n t e g e r means i t i s s h i f t e d i n upper t r i a n g l e
10 # k = n e g a t i v e i n t e r g e r means i t i s s h i f t e d i n l o w e r t r a i n g l e
11
12 >>>eye ( 3 , k=1)
13
14 array ( [ [ 0. , 1. , 0.] ,
15 [ 0. , 0. , 1.] ,
16 [ 0. , 0. , 0.]])
17
18 >>>eye ( 3 , k=2)
19
20 array ( [ [ 0. , 0. , 1.] ,
21 [ 0. , 0. , 0.] ,
22 [ 0. , 0. , 0.]])
23
24 >>>eye ( 3 , k=1)
49
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
25
26 array ( [ [ 0. , 0. , 0.] ,
27 [ 1. , 0. , 0.] ,
28 [ 0. , 1. , 0.]])
29
30 >>>eye ( 3 , k=2)
31
32 array ( [ [ 0. , 0. , 0.] ,
33 [ 0. , 0. , 0.] ,
34 [ 1. , 0. , 0.]])
5.4.7 identity
identity() function generates a two dimensional identity array.
1 identity (4)
2 Out [ 6 7 ] :
3 array ( [ [ 1. , 0. , 0. , 0.] ,
4 [ 0. , 1. , 0. , 0.] ,
5 [ 0. , 0. , 1. , 0.] ,
6 [ 0. , 0. , 0. , 1.]])
5.4.8 full
full fills up particular data into all elemental positions.
1 >>> f u l l ( ( 3 , 2 ) , 5 )
2
3 array ( [ [ 5. , 5.] ,
4 [ 5. , 5.] ,
5 [ 5. , 5.]])
50
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
4 [ 2.5 , 5.2 , 6.4]])
5
6 >>> f u l l l i k e ( a , 5 )
7
8 array ( [ [ 5. , 5. , 5.] ,
9 [ 5. , 5. , 5.]])
5.4.10 random
To create a random array (filled up with random numbers), one uses the
random function as follows:
1 a1 = random . rand ( 4 )
2
3 a1
4 Out [ 8 9 ] : a r r a y ( [ 0 . 9 1 9 9 4 1 4 7 , 0.75093653 , 0.03770014 ,
0.82726801])
5
6 a2 = random . rand ( 4 , 4 )
7
8 a2
9 Out [ 9 1 ] :
10 array ( [ [ 0.04817544 , 0.96832776 , 0.94496133 , 0.13974019] ,
11 [ 0.88772227 , 0.55457598 , 0.54588295 , 0.8659888 ] ,
12 [ 0.98772077 , 0.93785153 , 0.32630535 , 0.20258845] ,
13 [ 0.28838472 , 0.90353493 , 0.50091164 , 0.76243246]])
Note that the function rand() comes inside the subpackage random. To
get complete details of this wonderful package, user is encouraged to explore
it using help(numpy.random) after issuing the command import numpy.
51
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
10 random U ni f o r m l y d i s t r i b u t e d v a l u e s o f a g i v e n
shape .
11 bytes U n i f or m l y d i s t r i b u t e d random b y t e s .
12 random integers U ni f o r m l y d i s t r i b u t e d i n t e g e r s i n a g i v e n
range .
13 random sample U n i fo r m l y d i s t r i b u t e d f l o a t s i n a g i v e n
range .
14 permutation Randomly permute a s e q u e n c e / g e n e r a t e a
random s e q u e n c e .
15 shuffle Randomly permute a s e q u e n c e i n p l a c e .
16 seed Seed t h e random number g e n e r a t o r .
17 ====================
18
19 ====================
20 Compatibility functions
21 ====================
22 rand U n i f or m l y d i s t r i b u t e d v a l u e s .
23 randn Normally d i s t r i b u t e d v a l u e s .
24 ranf U ni f o r m l y d i s t r i b u t e d f l o a t i n g p o i n t
numbers .
25 randint U n i f o r ml y d i s t r i b u t e d i n t e g e r s i n a g i v e n
range .
26 ====================
27
28 ====================
29 Univariate d i st r i b u t io n s
30 ====================
31 beta Beta d i s t r i b u t i o n o v e r [ 0 , 1 ] .
32 binomial Binomial d i s t r i b u t i o n .
33 chisquare : math : \ c h i 2 d i s t r i b u t i o n .
34 exponential Exponential d i s t r i b u t i o n .
35 f F ( F i s h e r S n e d e c o r ) d i s t r i b u t i o n .
36 gamma Gamma d i s t r i b u t i o n .
37 geometric Geometric d i s t r i b u t i o n .
38 gumbel Gumbel d i s t r i b u t i o n .
39 hypergeometric Hypergeometric d i s t r i b u t i o n .
40 laplace Laplace d i s t r i b u t i o n .
41 logistic Logistic distribution .
42 lognormal Lognormal d i s t r i b u t i o n .
43 logseries Logarithmic s e r i e s d i s t r i b u t i o n .
44 negative binomial Negative binomial d i s t r i b u t i o n .
45 n o n c e n t r a l c h i s q u a r e Nonc e n t r a l c h i s q u a r e d i s t r i b u t i o n .
46 noncentral f Nonc e n t r a l F d i s t r i b u t i o n .
47 normal Normal / Gaussian d i s t r i b u t i o n .
48 pareto Pareto d i s t r i b u t i o n .
49 poisson Poisson d i s t r i b u t i o n .
50 power Power d i s t r i b u t i o n .
51 rayleigh Rayleigh d i s t r i b u t i o n .
52 triangular Triangular d i s t r i b u t i o n .
52
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
53 u n i f o rm Uniform d i s t r i b u t i o n .
54 v on mises Von Mises c i r c u l a r d i s t r i b u t i o n .
55 wald Wald ( i n v e r s e Gaussian ) d i s t r i b u t i o n .
56 weibull We i bu ll d i s t r i b u t i o n .
57 zipf Z i p f d i s t r i b u t i o n o v e r ranked data .
58 ====================
59
60 ====================
61 Multivariate distributions
62 ====================
63 dirichlet M u l t i v a r i a t e g e n e r a l i z a t i o n o f Beta
distribution .
64 multinomial M u l t i v a r i a t e g e n e r a l i z a t i o n of the binomial
distribution .
65 multivariate normal M u l t i v a r i a t e g e n e r a l i z a t i o n o f t h e normal
distribution .
66 ====================
67
68 ====================
69 Standard d i s t r i b u t i o n s
70 ====================
71 standard cauchy Standard CauchyL o r e n t z d i s t r i b u t i o n .
72 s t a n d a r d e x p o n e n t i a l Standard exponential distribution .
73 standard gamma Standard Gamma d i s t r i b u t i o n .
74 standard normal Standard normal d i s t r i b u t i o n .
75 standard t Standard Student td i s t r i b u t i o n .
76 ====================
77
78 ====================
79 Internal functions
80 ====================
81 get state Get t u p l e r e p r e s e n t i n g i n t e r n a l s t a t e o f
generator .
82 set state Set s t a t e of generator .
83 ====================
5.4.11 diagonal
diag() commands makes an array of defined dimensions as follows:
1 >>>a1 = random . randn ( 4 , 4 )
53
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
2 >>>a1
3
4 a r r a y ( [ [ 0 . 3 2 3 0 0 6 5 9 , 0.80867401 , 0 . 7 3 0 5 5 2 0 4 , 0 . 4 2 1 9 3 6 3 6 ] ,
5 [ 0 . 2 6 7 6 6 3 0 7 , 1.41864706 , 0.52676398 , 1 . 6 8 0 0 7 2 4 7 ] ,
6 [ 0.39765223 , 0.40380447 , 0.51565046 , 1.18807724] ,
7 [ 1 . 0 1 9 3 7 5 8 9 , 1 . 5 8 6 6 1 3 5 7 , 0.86241172 , 0 . 8 6 3 3 9 4 5 4 ] ] )
8
9 >>>a2 = d i a g ( a1 , k=0)
10 >>>a2
11 a r r a y ( [ 0 . 3 2 3 0 0 6 5 9 , 1.41864706 , 0 . 5 1 5 6 5 0 4 6 , 0 . 8 6 3 3 9 4 5 4 ] )
12 # an a r r a y o f t r u e d i a g o n a l e l e m e n t s o f a r r a y a2 i s r e t u r n e d .
13
14 >>>a2 = d i a g ( a1 , k=1)
15 >>>a2
16 a r r a y ( [ 0 . 8 0 8 6 7 4 0 1 , 0.52676398 , 1 . 1 8 8 0 7 7 2 4 ] )
17 # an a r r a y o f d i a g o n a l e l e m e n t s o f a r r a y a2 i s r e t u r n e d where
d i m e s n i o n a l a x i s i s s h i f t e d upwards by one u n i t .
18
19 >>>a2 = d i a g ( a1 , k=2)
20 >>>a2
21 array ([ 0.39765223 , 1.58661357])
22 # an a r r a y o f d i a g o n a l e l e m e n t s o f a r r a y a2 i s r e t u r n e d where
d i m e s n i o n a l a x i s i s s h i f t e d downwards by two u n i t .
5.5.1 arange
The syntax for automatically generating a range of numbers from a starting
point to a stop point with a step size is given by
54
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
7 >>>a r a n g e ( 1 0 )
8 array ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] )
9
10 #d e f a u l t v a l u e o f s t e p s i z e i s 1
11 >>>a r a n g e ( 1 , 1 0 )
12 array ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] )
5.5.2 linspace
Whereas arange() has good control over step size, one cannot specify
number of elements in the array. To solve this issue linspace() function is
defined with following syntax.
5.5.3 logspace
Just as linearly spaced points are generated by linspace(), logspace()
generates linearly spaced points on a logarithmic axis.
55
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
5.5.4 meshgrid
56
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
5.5.5 mgrid and ogrid
mgrid and ogrid are used to created mesh directly i.e. without using
linspace, arange etc. A simple statement like
mgrid[a:b , c:d]
constructs a grid where x-axis has points from a to b and y-axis has
points from c to d.
57
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
35 >>>y
36
37 array ( [ [ 0 , 1 , 2 , 3 , 4 ] ] )
5.5.6 tile
tile() functions makes the copy of existing array by the defined number
of times to make a new array as follows:
1 >>>a = a r r a y ( [ 1 , 2 , 3 ] )
2 >>>b = t i l e ( a , 3 )
3 >>>b
4
5 array ( [ 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 ] )
6
7 # a r r a y a i s r e p e a t e d 3 t i m e s t o make a new a r r a y b
8
9 # Another example t o do t h e same f o r two d i m e s n i o n a l a r r a y
10
11 >>>a1 = eye ( 4 )
12 >>>a1
13
14 array ([[ 1. , 0. , 0. , 0.] ,
15 [ 0. , 1. , 0. , 0.] ,
16 [ 0. , 0. , 1. , 0.] ,
17 [ 0. , 0. , 0. , 1.]])
18
19 >>>a2 = t i l e ( a1 , 2 )
20 >>>a2
21
22 array ( [ [ 1. , 0. , 0. , 0. , 1. , 0. , 0. , 0.] ,
23 [ 0. , 1. , 0. , 0. , 0. , 1. , 0. , 0.] ,
24 [ 0. , 0. , 1. , 0. , 0. , 0. , 1. , 0.] ,
25 [ 0. , 0. , 0. , 1. , 0. , 0. , 0. , 1.]])
26 >>>a2 = t i l e ( a1 , ( 2 , 2 ) )
27 >>>a2
28
29 array ([[ 1. , 0. , 0. , 0. , 1. , 0. , 0. , 0.] ,
30 [ 0. , 1. , 0. , 0. , 0. , 1. , 0. , 0.] ,
31 [ 0. , 0. , 1. , 0. , 0. , 0. , 1. , 0.] ,
32 [ 0. , 0. , 0. , 1. , 0. , 0. , 0. , 1.] ,
33 [ 1. , 0. , 0. , 0. , 1. , 0. , 0. , 0.] ,
34 [ 0. , 1. , 0. , 0. , 0. , 1. , 0. , 0.] ,
35 [ 0. , 0. , 1. , 0. , 0. , 0. , 1. , 0.] ,
36 [ 0. , 0. , 0. , 1. , 0. , 0. , 0. , 1.]])
37
58
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
38 # h e r e t h e e x t e n s i o n i s done on both d i m e n s i o n s
5.6 Broadcasting
Basic operations on numpy arrays are element-wise. This needs that di-
mensions of the arrays should be compatible for the desired operation i.e. 2
arrays and 2 arrays would be incompatible as first array has one less column
than the second one.
1 >>>a = eye ( 4 )
2 >>>b = a r r a y ( [ 1 , 2 , 3 , 4 ] )
3 >>>c = a + b
4 >>>c
5
6 array ([[ 2. , 2. , 3. , 4.] ,
7 [ 1. , 3. , 3. , 4.] ,
8 [ 1. , 2. , 4. , 4.] ,
9 [ 1. , 2. , 3. , 5.]])
10
11 >>>a . shape
12 ( 4L , 4L)
13
14 >>>b . shape
15 ( 4L , )
16
17 >>>c . shape
18 ( 4L , 4L)
19
20 # b r a d c a s t i n g e n a b l e s a r r a y a ( 4 X 4 ) t o be added t o b ( 4 X 1 )
t o produce an a r r a y c ( 4 X 4 )
21
22 # ANother example
23 >>>a = eye ( 4 )
24 >>>a
25
26 array ([[ 1. , 0. , 0. , 0.] ,
27 [ 0. , 1. , 0. , 0.] ,
28 [ 0. , 0. , 1. , 0.] ,
29 [ 0. , 0. , 0. , 1.]])
30
31 >>>b = a r r a y ( [ 1 0 , 1 0 , 1 0 , 1 0 ] )
32 >>>c = a + b
33 >>>c
34
35 array ( [ [ 11. , 10. , 10. , 10.] ,
59
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
36 [ 10. , 11. , 10. , 10.] ,
37 [ 10. , 10. , 11. , 10.] ,
38 [ 10. , 10. , 10. , 11.]])
39
40 # A 4 X 4 matrix can be o p e r a t e d with a 4 X 1 matrix by making
the i n v i s i b l e elemets zero .
5.7 Indexing
Elements of a array or list start with 0 in python i.e. first element is
indexed 0. All elements can be accessed using their indexes.
1 >>>a = [ 1 , 2 , 3 , 4 , 5 , 6 ]
2 >>>type ( a )
3 l i s t # a stores a l i s t object
4 >>>b = a r r a y ( a )
5 >>>type ( b )
6 numpy . n darray # b s t o r e s a a r r a y o b j e c t
7 >>>a [ 1 ]
8 2 # a c c e s s i n g s e c o n d e l e m e n t from l e f t hand s i d e f o r t h e l i s t a
9 >>>b [ 0 ]
10 1 # a c c e s s i n g f i r s t e l e m e n t from l e f t hand s i d e f o r t h e a r r a y b
11 >>>a [ 1]
12 6 # a c c e s s i n g t h e f i r s t e l e m e n t from t h e r i g h t hand s i d e f o r
l i s t a
13 >>>b [ 2]
14 5 # a c c e s s i n g t h e s e c o n d e l e m e n t from t h e r i g h t hand s i d e f o r
array b
Above examples make it clear that arrays are simply homogeneous lists
and follow the same rules of indexing. Multidimensional arrays also follow
the same pattern of indexing. For two dimensional arrays, first number
indicates the row and second number indicates the columns.
1 >>>a1 = a r r a y ( [ [ 1 , 2 , 3 ] , [ 3 , 2 , 1 ] ] )
2 >>>a1
3
4 array ( [ [ 1 , 2 , 3] ,
5 [3 , 2 , 1]])
6
60
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
7 >>>a1 [ 1 , 2 ]
8 1 # c h o o s i n g e l e m n e t whose row i s i n d e x e d 1 and coloumn i s
i n d e x e d 2 i . e s e c o n d row and t h i e r d coloumn i . e downr i g h t
l a s t element
9
10 >>>a1 [ 1 , 1 ]
11 2 # c h o o i s n g an e l e m e n t whose row i s i n d e x e d 1 and clolumn i s
i n d e x e d 1 i . e . s e c o n d row and s e c o n d coloumn
12
13 >>>a1 [ 1 ]
14 a r r a y ( [ 3 , 2 , 1 ] ) # c h o o i s n g row with i n d e x 1 i . e s e c o n d row
5.8 Slicing
Amongst the first operations to be applied on arrays is slicing. Slicing
employs the operator : which is used to separate the data on the row.
1 >>>a1 = a r a n g e ( 1 0 )
2 >>>a1
3 array ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] )
4
5 >>>a1 [ 0 : 5 ]
6 array ( [ 0 , 1 , 2 , 3 , 4 ] )
7 # [ 0 : 5 ] s e l e c e t s from i n d e x 0 u n t i l l i n d e x 5 i . e . e x c l u d i n g
index 5
8
9 >>>a1 [ : 5 ]
61
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
10 array ( [ 0 , 1 , 2 , 3 , 4 ] )
11 # [ : 5 ] s e l e c e t s from s t a r t i n g u n t i l l i n d e x 5 i . e . e x c l u d i n g
index 5
12
13 >>>a1 [ 2 : 5 ]
14 array ( [ 2 , 3 , 4 ] )
15 # [2:5] selecets from i n d e x 2 u n t i l l i n d e x 5 i . e . e x c l u d i n g
index 5
16
17 >>>a1 [ 2 : 2 ]
18 array ( [ 2 , 3 , 4 , 5 , 6 , 7 ] )
19 # [ 2 : 2 ] s e l e c e t s from i n d e x 2 u n t i l l i n d e x 2 ( c o u n t i n g
from r i g h t s t a r t s from 1) i . e . e x c l u s i n g i n d e x 2
20
21 >>>a1 [ 2 : ]
22 array ( [ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] )
23 # [ 2 : ] s e l e c e t s from i n d e x 2 u n t i l l l a s t i n d e x
24
25 >>>a1 [ 0 : 5 : 2 ]
26 array ( [ 0 , 2 , 4 ] )
27 # [ s t a r t : s t o p : s t e p ] = [ 0 : 5 : 2 ] hence i t t a k e s a t e p o f 2 w h i l e
c h o o s i n g i n d i c e s from 0 t o 5
1 >>>a = [ 1 , 2 , 3 , 4 , 5 ]
2 >>>b = [ 5 , 6 , 7 , 8 , 9 ]
3 a1 = a r r a y ( [ a , b ] )
4 >>>a1
5
6 array ( [ [ 1 , 2 , 3 , 4 , 5] ,
7 [5 , 6 , 7 , 8 , 9]])
8
9 >>>a1 . ndim
10 2 # t h e d i m e n s i o n o f a r r a y a1 i s 2
11
12 >>>a1 [ 0 : 2 , 0 : 2 ]
13
14 array ( [ [ 1 , 2] ,
15 [5 , 6]])
16 # S t a r t c o l l e c t i n g e l e m e n t s from row i n d e x e d 0 and coloumn
indexed 2
17
18 >>>a1 [ 0 : 2 , 0 : 4 ]
19
20 array ( [ [ 1 , 2 , 3 , 4] ,
62
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
21 [5 , 6 , 7 , 8]])
22 # F i r s t s l i c e i n d i c a t e s t o c o l l e c t o n l y f i r s t two e l e m e n t s by
s e c o n d s l i c e i n d i c a t e s t o c o l l e c t f i r s t f o u r e l e m e n t s , hence
u s i n g b r o a d c a s t i n g t h e r e s u l t i s implemented
23
24 >>>a1 [ 1 : 2 , 1 : ]
25 array ( [ [ 9 ] ] ) # because second s l i c e i n d i c a t e s to c o l l e c t the
l a s t element
63
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
original array instead of providing a view.
1 >>>a = a r a n g e ( 1 0 )
2 >>>c = a [ 2 : 5 ] . copy ( )
3 >>>c # c h e c k i n g e l e m e n t s o f a r r a y c
4 array ( [ 2 , 3 , 4 ] )
5 >>>c [ 0 ] = 10 # c h a n g i n g f i r s t e l e m e n t t o 10
6 >>>c # c h e c k i n g f o r change
7 array ( [ 1 0 , 3 , 4])
8 >>>a
9 array ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] )
10 # a r r a y a r e m a i n s unchanged
11 >>>may share memory ( a , c )
12 False
13 # a r e m a i n s unchanged b e c a u s e a and c don t s h a r e t h i e r
memories
5.10 Masking
Arrays can be indexed using the method of masking. Masking is a way to
define the indexes as a separate object and then generating a new array from
original array using the mask as a rule. There are two ways using which
arrays can be masked: fancy indexing and indexing using boolean values.
It is important to note that masking methods generate copies instead of
views. The two methods are discussed in the following subsections.
64
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
13 array ( [ 0 , 1000 , 8000 , 27000 , 64000])
14 # a [ j ] i s t h e a r r a y o f c u b e s i n d e x e d with a r r a y j
15 >>>k = a r r a y ( [ [ 1 , 2 ] , [ 1 1 , 12 ] ] )
16 # k i s a two d i m e n s i o n a l a r r a y o f i n d e x e s 1 , 2 , 1 1 , 1 2
17 >>>a [ k ]
18 array ( [ [ 1, 8] ,
19 [1331 , 1728]])
20 # a [ k ] i s a r r a y made up o f e l e m e n t s p l a c e d a t i n d e x e s g i v e n by k
65
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
numpy.matrix. A numpy.matrixis a specialized 2-D array that retains its
2-D nature through operations. Certain special operators, such as * (matrix
multiplication) and ** (matrix power) are defined for them.
1 >>>a = a r a n g e ( 1 0 ) . r e s h a p e ( 2 , 5 )
2 # d e f i n i n g a 2 X 5 a r r a y made o f numbers from f i r s t t e n numbers
3 >>>a
4 array ( [ [ 0 , 1 , 2 , 3 , 4] ,
5 [5 , 6 , 7 , 8 , 9]])
6
7 >>>type ( a )
8 numpy . n darray
9 # a i s a numpy . ndarray
10
11 # Now we s h a l l c r e a t e a matrix u s i n g t h i s a r r a y
12 >>>b = matrix ( a )
13 >>>b
14
15 matrix ( [ [ 0 , 1 , 2 , 3 , 4 ] ,
16 [5 , 6 , 7 , 8 , 9]])
17
18 >>>type ( b )
19 numpy . m a t r i x l i b . d e f m a t r i x . matrix
20 # b i s a matrix u n l i k e a , which i s an a r r a y
21
22 # Even though a and b l o o k s s i m i l a r , they a r e two d i f f e r e n t
objects
66
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
16 [ 8 , 10 , 12 , 1 4 ] ,
17 [ 1 6 , 18 , 20 , 2 2 ] ] )
18 # sum o f two a r r a y s p r o d u c e s a new a r r a y where e l e m e n t w i s e
operation ( a d d i t i o n h e r e ) i s p er f or me d
19
20 >>>s c a l a r p r o d u c t = 3 a a r r a y
21 >>>s c a l a r p r o d u c t
22
23 array ( [ [ 0 , 3 , 6 , 9] ,
24 [ 1 2 , 15 , 18 , 2 1 ] ,
25 [ 2 4 , 27 , 30 , 3 3 ] ] )
26
27 # s c a l a r p r o d u c t o f a r r a y with a number i s s i m p l y e l e m e n t w i s e
multiplication
28
29 >>>a m a t r i x = matrix ( a a r r a y )
30 >>>a m a t r i x
31
32 matrix ( [ [ 0 , 1 , 2 , 3 ] ,
33 [ 4 , 5 , 6 , 7] ,
34 [ 8 , 9 , 10 , 1 1 ] ] )
35 # A matrix a m a t r i x i s c r e a t e d u s i n g an a r r a y a s c a l a r
36
37 >>>sum matrix = a m a t r i x + a m a t r i x
38 >>>sum matrix
39
40 matrix ( [ [ 0 , 2 , 4 , 6 ] ,
41 [ 8 , 10 , 12 , 1 4 ] ,
42 [ 1 6 , 18 , 20 , 2 2 ] ] )
43
44 >>>s c a l a r m u l m a t r i x = 3 a
45 >>>s c a l a r m u l m a t r i x = 3 a m a t r i x
46 >>>s c a l a r m u l m a t r i x
47
48 matrix ( [ [ 0 , 3 , 6 , 9 ] ,
49 [ 1 2 , 15 , 18 , 2 1 ] ,
50 [ 2 4 , 27 , 30 , 3 3 ] ] )
51
52 # Checking f o r t r a n s p o s e
53 >>>a a r r a y T = a a r r a y . T
54 >>>a a r r a y T
55
56 array ( [ [ 0 , 4 , 8] ,
57 [ 1 , 5 , 9] ,
58 [ 2 , 6 , 10] ,
59 [ 3, 7, 11]])
60
61 >>>a m a t r i x T = a m a t r i x . T
62 >>>a m a t r i x T
67
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
63
64 matrix ( [ [ 0 , 4 , 8] ,
65 [ 1, 5 , 9] ,
66 [ 2, 6 , 10] ,
67 [ 3, 7, 11]])
68
69 # c h e c k i n g f o r dot p r o d u c t o f a r a y s and m a t r i c e s
70
71 >>>d o t a r r a y = dot ( a a r r a y , a a r r a y T )
72 >>>d o t a r r a y
73
74 array ( [ [ 14 , 38 , 6 2 ] ,
75 [ 38 , 126 , 2 1 4 ] ,
76 [ 62 , 214 , 3 6 6 ] ] )
77
78 >>>d o t m a t r i x = dot ( a m a t r i x , a m a t r i x T )
79 >>>d o t m a t r i x
80
81 matrix ( [ [ 1 4 , 3 8 , 6 2 ] ,
82 [ 38 , 126 , 2 1 4 ] ,
83 [ 62 , 214 , 3 6 6 ] ] )
84
85
86 # Well t h e matrix behaves e x a c t l y same a s a r r a y u p t i l l now
68
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
The answer is quite complex. While array serves most of the general
purposes for matrix algebra, matrix is written to facilitate linear algebra
functionalities. Linear algebra is performed using a submodule of numpy
accessed as numpy.linalg. Issuing command help(numpy.linalg) gives
idea about the purpose of this module. Some of the useful function from
matrix algebra point of view are:
More information about matrix object can be found by issuing the com-
mand help(numpy.matrix) or visiting http://docs.scipy.org/doc/numpy/
reference/generated/numpy.matrix.html.
5.12.1 sum
sum() calculates the sum of all elements in the array.
1 >>>a = a r a n g e ( 2 5 )
2 # c r e a t e d an a r r a y a c o n s i s t i n g o f f i r s t 25 numbers
3 >>>a
4
5 array ( [ 0 , 1 , 2, 3, 4, 5, 6, 7, 8, 9 , 10 , 11 , 12 , 13 ,
14 , 15 , 16 ,
69
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
6 17 , 18 , 19 , 20 , 21 , 22 , 23 , 2 4 ] )
7
8 >>>sum ( a )
9 300
10
11 # r e s h a p i n g i t t o a 5 X 5 matrix d o e s not change t h e sum o f
elements
12
13 >>>b = a . r e s h a p e ( 5 , 5 )
14 >>>b
15
16 array ( [ [ 0, 1, 2, 3 , 4] ,
17 [ 5, 6, 7, 8, 9] ,
18 [10 , 11 , 12 , 13 , 14] ,
19 [15 , 16 , 17 , 18 , 19] ,
20 [20 , 21 , 22 , 23 , 24]])
21
22 >>>sum ( b )
23 300
24
25 # sum can be d e f i n e d f o r an a x i s
26
27 >>>sum ( b , a x i s =0)
28 array ( [ 5 0 , 55 , 60 , 65 , 7 0 ] )
29 # each e l e m e n t i f sum o f coloumn e l e m e n t s
30
31 >>>sum ( b , a x i s =1)
32 array ( [ 10 , 35 , 60 , 85 , 1 1 0 ] )
33 # each e l e m e n t i s sum o f row e l e m e n t s
70
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
12 array ( [ 5 , 6 , 7 , 8 , 9 ] )
13 # maximum i n each coloumn
14
15 >>>a . max( a x i s =1)
16 array ( [ 4 , 9 ] )
17 # maximum i n each row
5.12.4 sort
sort() sorts the array values from maximum to minimum.
1 >>>a = rand ( 3 , 4 )
2 >>>a
3
4 array ( [ [ 0.12623497 , 0.08767029 , 0.76615535 , 0.85825585] ,
5 [ 0.78531643 , 0.92799983 , 0.03808058 , 0.87323096] ,
6 [ 0.40734359 , 0.7030647 , 0.02290688 , 0.1080126 ] ] )
7
8 >>>s o r t ( a )
9
10 array ( [ [ 0.08767029 , 0.12623497 , 0.76615535 , 0.85825585] ,
11 [ 0.03808058 , 0.78531643 , 0.87323096 , 0.92799983] ,
12 [ 0.02290688 , 0.1080126 , 0.40734359 , 0.7030647 ] ] )
13 # s o r t s by coloumn by d e f a u l t
14
15 >>>s o r t ( a , a x i s =1)
16
71
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
17 array ( [ [ 0.08767029 , 0.12623497 , 0.76615535 , 0.85825585] ,
18 [ 0.03808058 , 0.78531643 , 0.87323096 , 0.92799983] ,
19 [ 0.02290688 , 0.1080126 , 0.40734359 , 0.7030647 ] ] )
20
21 >>>s o r t ( a , a x i s =0)
22
23 array ( [ [ 0.12623497 , 0.08767029 , 0.02290688 , 0.1080126 ] ,
24 [ 0.40734359 , 0.7030647 , 0.03808058 , 0.85825585] ,
25 [ 0.78531643 , 0.92799983 , 0.76615535 , 0.87323096]])
72
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
10 >>>around ( a ) . a s t y p e ( i n t )
11 array ( [ 0 , 0 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 0 ] )
5.14 Summary
Array based computing is used as a primary force to solve equations or
system of equations. Using slicing and indexing operations, it provides pow-
erful tools to manipulate data using a program. Since present book is an
interactive text in python, hence discussion about all functions for indexing
and slicing is out of the scope for the book. Users are requested to visit http:
//docs.scipy.org/doc/numpy/reference/routines.indexing.html
73
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
Pythons ability to flexible create variety of arrays and compute using
various mathematical functions makes it one of the most preferred language
in the field of computational physics and mathematics. One more fact which
makes it a preferable programming language among scientific community is
its ability to plot publication quality graphs with relative ease. This shall
be discussed in the next chapter.
74
Plotting
6
6.1 Introduction
Plotting the data is one of
the most essential part of nu-
merical computation. In per-
processing, during computation and
post-processing, plotting data in
term of variety of graphs becomes
essential. Visualization of data in
a convenient format lets one under-
stand the process better. Visual
clues generate a lot of information
about the process which generated
that particular data. One can look for error easily and derive simple as
well as complex interpretations. A good programming language must in-
corporate facilities to plot data easily. Plotting two dimensional (2D) and
three-dimensional (3D) graphs are essential qualities in a good visualization
product. Python users have a good number of choices in this regard.
plotting 2D or 3D
75
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
plotting large data quickly
plotting data with chosen resolution to keep a check about file size.
Various plotting libraries will be discussed in present chapter and they will
be judged based on above mentioned parameters. None of them if perfect.
They fulfill each others gaps. Hence a programmer is encouraged to learn
all of them and then choose to use them as and when required.
6.2 Matplotlib
John Hunter, the creator of matplotlib, rightly quoted that
Matplotlib tries to make easy things easy and hard things possible.
In some cases, with just one line of code, one can generate high qual-
ity publication ready graphics visualization of problem at hand. Before
python, gnuplot was used to plot the data passed by a python script. With
matplotlib at hand, this action has become very flexible now. matplotlib
was modeled after graphic capabilities of MATLAB
R
, which came as a
boon for programmer who were already well versed with MATLAB
R
. Some
of the major advantages of using matplotlib over other plotting libraries
are:
Its is open sourced, so one does not have to worry about license fees.
76
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
a variety of plotting styles like line, scatter, bar charts as well as histograms
and many more. It can also be used interactively with Ipython. numpy is
essential to work with matplotlib, hence it must be installed on the system
before one can work with matplotlib.
numpy >1.1
libpng >1.1
FreeType >1.4
77
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
6.3 Plotting basic plots
We shall first explore the working environment offered by matplotlib.pyplot.
Functional inside pyplot control a particular feature of the plot like putting
up a tile, mentioning labels on x-axis and y-axis, putting mathematical equa-
tions on the body of plot at a desired position, defining tick labels, defining
types of markers to plot a graph etc. pyplot is stateful i.e. it keeps updat-
ing the changes in the state of figure once defined. This makes it easier to
modify a graph until desired level before including the code in the program.
sqPlot0.py
The result can be seen in 6.1. A plot needs two axes which are usually
termed as x and y axis. When plot() command is supplied with a single
list or array, it assumes it to be the values for y-axis and automatically gen-
erates corresponding x-values taking cue from length of list. In or case, we
78
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
had 10 numbers, hence x-axis had 10 numbers from 0 to 9.
plot() can take both axes as input vectors to produce a plot a shown
in the code given by sqPlot1.py.
1 import numpy a s np
2 from m a t p l o t l i b import pylab a s p l
3 x = np . l i n s p a c e ( 0 , 1 0 0 )
4 y = x 2
5 pl . plot (x , y)
6 p l . show ( )
sqPlot1.py
The result can be seen in figure ??. A very basic plot could be plotted by
just few lines of code where one first imports relevant libraries (line 1 and
line 2), then define x and y axes, and then use the plot command which
is given the parameters about x and y axes. These commands can e issued
one by one at python command prompt, or it can be saved as a python file
(use a text-editor, write the code and save with sqPlot1.py).
The axes are defined as a numpy array. They can be generated by all
the methods available at hand like generating by hand, generated using a
formula (like in code sqPlot1.py, array named y is generated by element-
wise squaring of array x), generated by a data file, data taken live from a
remote/local server using internet or LAN etc. Subsequent chapters will
deal with file input output facilities. Hence for present chapter, only array
79
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
generated by self or using formulas, will be used.
sqPlot2.py
Title: Title gives a short introduction for the purpose of the graph
title() object sets the title of the current axes, positioned above
axes and in the center. It takes a string as an input.
80
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
xlabel() and ylabel() object sets the label of x and y axis re-
spectively. The text strong which it takes as input is positioned above
the axis in the center.
Ticks: Ticks on axis show the division of data points on an axes and
help judging information about a data point on graph.
xticks and yticks sets the ticking frequency and location. For
example
xticks( arange(5), (a, b, c, d, e) )
Markers: markers are the symbols drawn at each data point. The
size and type of markers can be differentiated for showing the data
points belonging to two or more different data sets.
81
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
Marker Abbreviation Marker Style
. Point
, Pixel
o Circle
v Triangle down
< Triangle left
> Triangle right
1 Tripod down
2 Tripod up
3 Tripod left
4 Tripod right
s square
p pentagon
star
h hexagon
H Rotated hexagon
+ plus
x cross
D Diamond
d Thin diamond
- Horizontal line
Line style: Line style defines the style of lines which connect the
markers. They can be set off when data points need not be connected.
linestyle = . sets the line style as a connecting dot between
two data points. Similarly a number of other line style also exist.
Style Abbreviation Style
- solid line
dashed line
-. dash dot line
: dotted line
Color: Color of markers can also be used for distinguishing data points
belonging to two or more different data sets, but this method cannot
be used where data needs to be published in Black and White color
82
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
scheme.
Above command sets the line style as --, markers as + in green color.
A shortcut command would have been
plot(range(10), --g+)
grid(True)
Legends: Legends are used to differentiate between different types of
data points from multiple graphs in a same figure by showing symbol
for data type and printing text for the same.
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.
legend
83
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
corner by default. It can be changed as per requirement by setting
loc= argument.
84
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
37 p l . ylim ( 1.2 , 1 . 2 )
38
39 # Set y t i c k s
40 p l . y t i c k s ( np . l i n s p a c e ( 1 , 1 , 5 , e n d p o i n t=True ) )
41
42 # S e t t h e t i l e a s S i n e waves
43 p l . t i t l e ( $ s i n ( x ) $ and $ s i n ( 2 x ) $ waves )
44
45 # S e t t i n g l a b e l on xa x i s and ya x i s
46
47 p l . y l a b e l ( $ s i n ( x ) $ and $ s i n ( 2 x ) $ )
48 p l . x l a b e l ( $x$ )
49
50 # S e t t i n g t h e g r i d t o be ON
51 p l . g r i d ( True )
52
53 # To show a l e g e n d a t one c o r n e r f o r d i f f e r e n t i a t i n g two c u r v e s
54 pl . legend ()
55
56 # Show r e s u l t on s c r e e n
57 p l . show ( )
sqPlot3.py
85
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
6.5 Histograms
Histograms use vertical bars to plot events occurring with a particular
range of frequency (called bins). The can be simply plotted using hist()
function as in code plottingHistogram.py
1 import m a t p l o t l i b . p y p l o t a s pt
2 import numpy a s np
3
4 a = np . random . rand ( 5 0 )
5 pt . h i s t ( a )
6 pt . show ( )
plotHistogram.py
86
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
2 import numpy a s np
3
4 a = np . random . rand ( 5 0 )
5 pt . h i s t ( a , 2 5 ) # s e t t i n g number o f b i n s t o 25
6 pt . show ( )
plotHistogramBins.py
87
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
1 import m a t p l o t l i b . p y p l o t a s p l
2 import numpy a s np
3 x = np . a r r a y ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ] )
4 y = np . a r r a y ( [ 1 , 4 , 2 , 3 , 4 , 5 , 7 , 6 , 8 , 7 ] )
5 p l . bar ( x , y )
6 p l . t i t l e ( V e r t i c a l Bar c h a r t )
7 p l . x l a b e l ( $x$ )
8 p l . y l a b e l ( $y$ )
9 p l . show ( )
bar.py
Bar charts and histograms look very similar. Difference lies in the way one
defines them. Whereas bar() requires both x-axis and y-axis arguments,
hist() requires only y-axis argument. barh() function plots horizontal
bars.
1 import m a t p l o t l i b . p y p l o t a s p l
2 import numpy a s np
3 x = np . a r r a y ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ] )
4 y = np . a r r a y ( [ 1 , 4 , 2 , 3 , 4 , 5 , 7 , 6 , 8 , 7 ] )
5 p l . barh ( x , y )
6 p l . t i t l e ( H o r i z o n t a l Bar c h a r t )
7 p l . x l a b e l ( $x$ )
8 p l . y l a b e l ( $y$ )
9 p l . show ( )
barh.py
88
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
The result in shown in figure 6.8.
1 import m a t p l o t l i b . p y p l o t a s p l
2 import numpy a s np
3 x = np . a r a n g e ( 0 , 4 , 0 . 2 ) # g e n e r a t e d data p o i n t from 0 t o 4 with
step of 0.2
4 y = x 2 # y = e (x )
5 e r r = np . a r r a y ( [
0 ,.1 ,.1 ,.2 ,.1 ,.5 ,.9 ,.2 ,.9 ,.2 ,.2 ,.2 ,.3 ,.2 ,.3 ,.1 ,.2 ,.2 ,.3 ,.4])
6 p l . e r r o r b a r ( x , y , y e r r=e r r , e c o l o r= r )
7 p l . t i t l e ( E r r o r bar c h a r t with s y m m e t r i c a l e r r o r )
8 p l . x l a b e l ( $x$ )
9 p l . y l a b e l ( $y$ )
89
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
10 p l . show ( )
ploterror.py
ecolor sets the color for error bars. Just as setting value for yerr key-
word, one can also set xerr to produce error bars on x-axis too.
6 e r r n e g a t i v e = np . a r r a y ( [
0.2 ,.4 ,.3 ,.1 ,.4 ,.3 ,.1 ,.9 ,.1 ,.3 ,.5 ,.0 ,.5 ,.1 ,.2 ,.6 ,.3 ,.4 ,.1 ,.1])
7 p l . e r r o r b a r ( x , y , y e r r =[ e r r p o s i t i v e , e r r n e g a t i v e ] , e c o l o r= r )
8 p l . t i t l e ( E r r o r bar c h a r t with Asyymetric e r r o r )
90
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
9 p l . x l a b e l ( $x$ )
10 p l . y l a b e l ( $y$ )
11 p l . show ( )
ploterror1.py
1 import m a t p l o t l i b . p y p l o t a s p l
2 import numpy a s np
3 x = np . random . rand ( 1 0 0 0 )
91
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
4 y = np . random . rand ( 1 0 0 0 )
5 pl . scatter (x , y)
6 p l . t i t l e ( S c a t t e r Chart )
7 p l . x l a b e l ( $x$ )
8 p l . y l a b e l ( $y$ )
9 p l . show ( )
scatter.py
92
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
7 p l . t i t l e ( P i e Chart )
8 p l . show ( )
pie.py
When explode label is not defined, one gets an unexploded pie chart.
autopct sets the show the percentage of weight for a particular weight which
can be set by format specifier. %2.2f%% sets the display of percentage weights
uptill 2 decimal places with 2 significant digits. shadow provides a shadow
below the wedge so that it looks like a real pie!
93
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
are obtained by plot() function.
1 import m a t p l o t l i b . p y p l o t a s p l
2 import numpy a s np
3
4 r = np . a r a n g e ( 0 , 1 0 . 0 , 0 . 1 )
5 t h e t a = 2 np . p i r
6
7 pl . polar ( theta , r , c o l o r = g )
8 p l . show ( )
polar.py
94
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
2 import numpy a s np
3 x = np . a r a n g e ( 0 , 2np . pi , . 0 1 )
4 y = np . s i n ( x )
5 pl . plot (x , y , color = r ) ;
6 p l . t e x t ( 0 . 1 , 0.04 , $ s i n ( 0 ) = 0 $ )
7 pl . t e x t ( 1 . 5 , 0 . 9 , $ s i n (90) = 1$ )
8 pl . te x t ( 2 . 0 , 0 , $ s i n (180) = 0$ )
9 p l . t e x t ( 4 . 5 , 0.9 , $ s i n ( 2 7 0 ) = 1$ )
10 pl . te x t ( 6 . 0 , 0 . 0 , $ s i n (360) = 1$ )
11 p l . a n n o t a t e ( $ s i n ( t h e t a )=0$ , xy =(3 , 0 . 1 ) , x y t e x t =(5 , 0 . 7 ) ,
a r r o w p r o p s=d i c t ( f a c e c o l o r= g r e e n , s h r i n k =0.05) )
12 p l . t i t l e ( I n s e r t i n g t e x t and a n n o t a t i o n i n p l o t s )
13 pl . xlabel ( $theta$ )
14 p l . y l a b e l ( $y = s i n ( t h e t a ) $ )
15 p l . show ( )
textPlot.py
95
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
present mouse coordinates are shown. help(matplotlib.pyplot.annotate)
gives useful inputs to use this function.
6.12 Subplots
Multiple plots can be plotted using subplot option where different plots
are considered to be a matrix of graphs. Just like a regular matrix, elements
are identified by index. As seen in code subplot1.py subplots are located
using indices like (222) which essentially mean that the matrix is a 2 2 and
one is accessing 2nd position to place the scatter() function based plot.
Similarly (221) uses a plot() function at 1st position, (223) at 3rd position
plots a histogram using hist() function and (224) is 4th plot using barh()
giving a horizontal bar graph.
1 import m a t p l o t l i b . p y p l o t a s p l
2 import numpy a s np
3
4 x = np . a r a n g e ( 1 0 )
5 y1 = np . random . rand ( 1 0 )
6 y2 = np . random . rand ( 1 0 )
7
8 f i g = pl . f i g u r e ()
9
10
11 ax1 = f i g . a d d s u b p l o t ( 2 2 1 )
12 ax1 . p l o t ( x , y1 )
13
14 ax2 = f i g . a d d s u b p l o t ( 2 2 2 )
15 ax2 . s c a t t e r ( x , y2 )
16
17 ax3 = f i g . a d d s u b p l o t ( 2 2 3 )
18 ax3 . h i s t ( y1 )
19
20 ax4 = f i g . a d d s u b p l o t ( 2 2 4 )
21 ax4 . barh ( x , y2 )
22
23 p l . show ( )
subplot1.py
96
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
sqPlot4.py
It is important to know the size of file and resolution of figure for the
purpose of publication at both off-line/print and on-line medium. When
97
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
none of the arguments for setting the resolution of figure and size of file is
set within the program, default values are used. These default values can
be known by following code:
1 >>>import m a t p l o t l i b a s ml
2 >>>ml . rcParams [ f i g u r e . f i g s i z e ]
3 [8.0 , 6.0]
4 # Default f i g u r e s i z e i s 8 X 6 inches
5 >>>ml . rcParams [ s a v e f i g . d p i ]
6 100.0
7 # D e f a u l t f i g u r e r e s o l u t i o n i s 100 d p i
Since a 8 6 inches figure is created with 100 dpi (dots/pixels per inch),
hence a 800 600 pixels image is saved using savefig() by default. When
this file is directed towards a computer graphic terminal for displaying,
length units are ignored and pixels are displayed. If file is directed towards
a printing media like a printer or plotter, lengths parameters and DPI de-
termine figure size and resolution.
98
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
different environments. It is important to note that matplotlib requires
graphic user interface requiring a X11 connection. Hence it is important to
turn on this faculty on a web application server before updating the plots
dynamically, generated by matplotlib. There are two aspects to plotting
graphs on computer in general. Coding using a set of commands to make
a script is known as frontend task which require a backend effort. Back-
end does all the hardwork of interacting with graphical capabilities of the
system to produce a graph in a desired plot. Plots can be plotted interac-
tively using backends like pygtk, wxpython, tkinter, qt4, macosx or they
can be plotted non-interactively (permanently saved as files on computer)
using backends like PNG, SVG, PS, PDF. The latter are also known as hardcopy
backends.
99
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
One way to save transparent figures as opposed to white colored by default
is to set transparent=TRUE. This is particularly important in the case when
figures needs to be embedded on a web-page with predefined-background
color/image.
Ipython notebook
While working with Ipython, if one wishes to work with plots to dynam-
ically change by issuing commands at Ipython command prompt, then one
simply issues a command at UNIX terminal:
1 $ i p y t h o n pylab
produces the graphs in-line i.e. within the body of the code in between
the command lines where plot is called, otherwise plots pop out in a separate
window. In-line mode is useful while designing teaching material. But before
sharing with concerned person, it should be ensured that encoded backends
and dependencies are installed on users computer.
100
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
to customize the same. The hierarchy of three basic objects used for the
purpose is as follows:
3. Axes: Axes are the rectangular areas to hold various plot features like
lines, ticks, curves, text etc.
objPlot.py
Most of the plots in present book will be plotted in objective mode hence-
forth.
101
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
102
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
20
21 ax3 = f i g . a d d s u b p l o t ( 2 2 3 )
22 y3 = np . s i n ( np . p i x / 3 . )
23 ax3 . l o g l o g ( x , y3 , basex =2) ;
24 ax3 . g r i d ( True )
25 ax3 . s e t y l a b e l ( both a x e s i n l o g ) ;
26
27 ax4 = f i g . a d d s u b p l o t ( 2 2 4 )
28 y4 = np . c o s ( 2 x )
29 ax4 . l o g l o g ( x , y3 , basex =10) ;
30 ax4 . g r i d ( True )
31
32 p l t . show ( )
log.py
Logarithmic plots find their use in a variety of fields like signal processing,
thermodynamics etc. Essentially, whenever data changes by an order of a
magnitude, its easier to observe it using logarithmic plot. Logarithmic scale
is a non-linear scale. The ability of change base of logarithmic function,
provides a powerful tool at the hands of developers to plot creatively to
derive most meaningful conclusion.
103
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
104
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
13 ax1 . s e t y l a b e l ( $x {2} $ ) ;
14 ax2 = ax1 . twinx ( ) # twinx ( ) f u n c t i o n i s used t o show twinned x
axes
15 ax2 . p l o t ( x , y2 , k+ ) ;
16 ax2 . s e t y l a b e l ( $ \ s q r t {x} $ ) ;
17 ax2 . s e t t i t l e ( Same x a x i s f o r both y v a l u e s ) ;
18 p l t . show ( )
twinx.py
It is worth noting that two different axes instances namely ax1 and ax2
are superimposed on each other where data from y1 being alloted to axes
instance ax1 and data from y2 being alloted to axes instance ax2. This also
illustrates the power of defining a plot in object mode. The corresponding
plot is given in 6.19
105
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
from top, one can define regions of similar height being shown with a closed
loop. Thus a mountain will be a series of loops. Similarly, a 2D map of
non-uniformly heated region can be viewed as contours depicting regions of
same temperature. A region of rainfall can be viewed as contour showing
regions of dis-similar size of droplets.
Hence contour lines are also called level lines or isolines. The term iso- is
attached to data points having constant value and the regions of these data
points are separated by contours.
For a contour plot, one needs x, y and z axis where z axis defines the
height. The data with same height is clubbed together within isolines.
1 import m a t p l o t l i b . p y p l o t a s p l t
2 import numpy a s np
3
4 # d e f i n i n g data f o r x , y , z a x e s
5 x = np . l i n s p a c e ( 0 , 1 , 1 0 0 )
6 y = np . l i n s p a c e ( 1 , 2 , 1 0 0 )
7 (X,Y) = np . meshgrid ( x , y )
8 z = np . s i n (X)np . s i n (Y)
9
10 # p l o t t i n g contour
11 fig = plt . figure ()
12
13 ax1 = f i g . a d d s u b p l o t ( 2 1 1 )
14 c1 = ax1 . c o n t o u r ( x , y , z )
15 l 1 = ax1 . c l a b e l ( c1 )
16 l x 1 = ax1 . s e t x l a b e l ( x )
17 l y 1 = ax1 . s e t y l a b e l ( y )
18
19
20 # p l o t t i n g f i l l e d contour
21 ax2 = f i g . a d d s u b p l o t ( 2 1 2 )
22 c2 = ax2 . c o n t o u r f ( x , y , z )
23 l 2 = ax2 . c l a b e l ( c2 )
24 l x 2 = ax2 . s e t x l a b e l ( x )
25 l y 2 = ax2 . s e t y l a b e l ( y )
26
27 p l t . show ( )
28
29 # p l o t t i n g f i l l e d contour
contour.py
106
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
107
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
3 import numpy a s np
4 import m a t p l o t l i b . p y p l o t a s p l t
5
6 fig = plt . figure ()
7 ax = f i g . gca ( p r o j e c t i o n= 3d )
8
9 x = np . l i n s p a c e ( 10(np . p i ) , 1 0 ( np . p i ) , 1 0 e4 )
10 y = np . s i n ( x )
11 z = np . c o s ( x )
12
13 ax . p l o t ( x , y , z , l a b e l= $y=s i n ( x ) $ and $z = c o s ( x ) $ )
14 ax . l e g e n d ( )
15 ax . s e t t i t l e ( 3D l i n e c u r v e )
16 ax . s e t x l a b e l ( $x$ )
17 ax . s e t y l a b e l ( $y = s i n ( x ) $ )
18 ax . s e t z l a b e l ( $z = c o s ( x ) $ )
19 p l t . show ( )
3Dline.py
1 import numpy a s np
2 from m p l t o o l k i t s . mplot3d import Axes3D
3 import m a t p l o t l i b . p y p l o t a s p l t
4
5 fig = plt . figure ()
6 ax = f i g . a d d s u b p l o t ( 1 1 1 , p r o j e c t i o n= 3d )
7
8 x = np . l i n s p a c e ( 5(np . p i ) , 5 ( np . p i ) , 2 0 0 )
9 y =np . s i n ( x )
10 z =np . c o s ( x )
11
12 ax . s c a t t e r ( x , y , z , marker= )
13
14 ax . set x l a b e l ( $x$ )
15 ax . set y l a b e l ( $y = s i n ( x ) $ )
16 ax . set z l a b e l ( $z = c o s ( x ) $ )
17 ax . set t i t l e ( S c a t t e r p l o t i n 3D )
18
19 p l t . show ( )
3Dscatter.py
Scatter plots are plotted in 3D in similar way as line plots. This is illus-
trated in the python scripy 3Dscatter.py and the corresponding graph is
shown in figure 6.21
108
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
is generated on top of this grid and plotted using wiremesh function. rstride
and cstride define the row and column step size.
109
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
3Dwiremesh.py
Surface plots are similar to wiremesh plots except the fact that its contin-
uously filled up. Hence instead of wiremesh() function, one uses surface()
function.
1 from m p l t o o l k i t s . mplot3d import axes3d
2 import m a t p l o t l i b . p y p l o t a s p l t
3 import numpy a s np
4
5 fig = plt . figure ()
6 ax = f i g . a d d s u b p l o t ( 1 1 1 , p r o j e c t i o n= 3d )
7
8 a = np . a r a n g e ( 5 , 5 , 0 . 2 5 )
9 b = np . a r a n g e ( 5 , 5 , 0 . 2 5 )
10 x , y = np . meshgrid ( a , b )
11 z = np . s q r t ( x 2 + y 2 )
12
13 ax . p l o t s u r f a c e ( x , y , z , r s t r i d e =2 , c s t r i d e =2)
14
15 ax . set xlabel ( x )
16 ax . set y l a b e l ( $y$ )
17 ax . set z l a b e l ( $z = \ s q r t {x{2}+y {2}} $ )
18 ax . set t i t l e ( S u r f a c e type o f 3D p l o t )
19
20 p l t . show ( )
110
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
3Dsurface.py
111
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
tions, one can plot unfilled and filled contour plots.
1 from m p l t o o l k i t s . mplot3d import axes3d
2 import m a t p l o t l i b . p y p l o t a s p l t
3 from m a t p l o t l i b import cm
4 import numpy a s np
5
6 fig = plt . figure ()
7 ax1 = f i g . a d d s u b p l o t ( 1 2 1 , p r o j e c t i o n= 3d )
8 x = np . l i n s p a c e ( 2 np . pi , 2( np . p i ) , 1 0 0 0 )
9 y = np . l i n s p a c e ( 2 np . pi , 2( np . p i ) , 1 0 0 0 )
10 X,Y = np . meshgrid ( x , y )
11 Z = np . s i n (X) + np . s i n (Y)
12
13 c o n t = ax1 . c o n t o u r (X, Y, Z )
14 ax1 . c l a b e l ( cont , f o n t s i z e =9, i n l i n e =1)
15 ax1 . s e t x l a b e l ( $x$ )
16 ax1 . s e t y l a b e l ( $y$ )
17 ax1 . s e t t i t l e ( Contour f o r $z=s i n ( x )+s i n ( y ) $ )
18
19 ax2 = f i g . a d d s u b p l o t ( 1 2 2 , p r o j e c t i o n= 3d )
20 Z = np . s i n (X) + np . s i n (Y)
21 c o n t = ax2 . c o n t o u r f (X, Y, Z )
22 ax2 . c l a b e l ( cont , f o n t s i z e =9, i n l i n e =1)
23 ax2 . s e t x l a b e l ( $x$ )
24 ax2 . s e t y l a b e l ( $y$ )
25 ax2 . s e t t i t l e ( F i l l e d Contour f o r $z=s i n ( x )+s i n ( y ) $ )
26
27
28
29 p l t . show ( )
3Dcontour.py
112
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
Figure 6.23: Contour plots using contour and contourf functions plot
11 #p = p l t . q u i v e r k e y ( q , 1 , 1 6 . 5 , 5 0 , 5 0 m/ s , c o o r d i n a t e s = data , c o l o r
= r )
12 x l = p l t . x l a b e l ( x (km) )
13 y l = p l t . y l a b e l ( y (km) )
14 p l t . show ( )
3Dquiver.py
113
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
gnuplot to name a few. A brief discussing follows about using plotly, which
interestingly, plots the data on the web. This is particularly interesting for
those engineering applications where the sensor data needs to be plotted on
web in real time.
6.20.1 Plotly
Plotly is an on-line analytics and data visualization tool. Apart from
python, plotly can also plot data used in Perl, Julia, Arduino, R and
MATLAB
. R Weblink https://plot.ly/ gives pretty good idea about ca-
pabilities of plotly. Essentially plotly allows plotting and publishing the
graph on-line for allowing collaboration. Hence one must be connected to
internet before working with plotly library.
First, one needs to make a user account at plotly website. Account will
114
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
provide a username and API key which needs to be used in the program.
Then one can write a code either as one command at a time on python
terminal or as a python script.
1 import numpy a s np
2 import p l o t l y . p l o t l y a s py
3 from p l o t l y . g r a p h o b j s import
4 py . s i g n i n ( username , APIkey )
5 data = Data ( [ S c a t t e r ( x=np . a r a n g e ( 1 0 0 ) , y=np . random . randn ( 1 0 0 ) ,
name= t r a c e 0 ) ] )
6 f i g = F i g u r e ( data=data )
7 p l o t u r l = py . p l o t ( f i g )
plotly.py
User needs to provide the username and API key in the above script.
The above script results in generating a scatter plot on-line at workspace
in the user account. These graphs can be plotted quite interactively using
the functions provided by plotly. When data is steamed from a device con-
nected to a live sensor, live data is plotted in real time. This can further be
embedded on a website.
6.21 Summary
The present chapter discussed various plotting options available while
working with python. The ease of plotting data is one of the most attrac-
tive feature from python. Just a few lines of code visualize the data in
a variety of ways. Visualization is the back bone of data presentation and
analysis since understanding of data becomes more clearer. Apart from sim-
ple visualization, matplotlib allows rich feature to decorate the graph with
useful information in a desired manner.
115
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
116
File I/O
7
7.1 Introduction
Handling files is an essential
part of the process of computation.
Python provides many features to
perform this act. Files come in
a variety of format and hence any
programming language enjoying the
capabilities of handling files must
provides the functionalities for han-
dling variety of file formats and
opening, making, editing and delet-
ing them as desired, with ease.
117
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
reads one-line and returns it as a string. on the other hand, input function,
treats the input as a valid python expression nd returns the result.
1 S t r i n g f r o m r a w i n p u t = r a w i n p u t ( Enter t h e i n p u t f o r r a w i n p u t
function : )
2 print raw input y i e l d s = , String from raw input
3
4 S t r i n g f r o m i n p u t = i n p u t ( Enter t h e i n p u t f o r i n p u t f u n c t i o n : )
5 print raw input y i e l d s = , String from input
input.py
The result is shown below:
1 Enter t h e i n p u t f o r r a w i n p u t f u n c t i o n : Hi
2 r a w i n p u t y i e l d s = Hi
3
4 Enter t h e i n p u t f o r i n p u t f u n c t i o n : [ x f o r x i n r a n g e ( 1 0 ) ]
5 raw input y i e l d s = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9]
118
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
Here file1 variable will store the file object containing the contents of
the file named a.txt. This file is opened in read only mode as specified by
second parameter given by the string "r". A number of modes exist for files
to be opened, as shown in table 7.2
r read only
rb read only in binary format
r+ read and write
rb+ read and write in binary format
w write only
w+ read and write
wb+ read and write in binary format
a append
a+ append and read
ab+ append and reading in binary format
119
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
1 >>>a1 = open ( a , w )
This command will make a new file named a in working directory and
open it in write mode. This file object is stored in a variable named a1.
1 >>>type ( a1 )
2 file
3
4 >>>a1 . name
5 a
6
7 >>>a1 . c l o s e d
8 False
9
10 >>>a1 . mode
11 w
12
13 >>>a1 . c l o s e ( )
14 >>>a1 . c l o s e d
15 True
16
17 >>>a1 . mode
18 w
19
20 >>>a1 . name
21 a
Line number 1 inquired about the type of object stored in the variable
name a1 which is returned as a file type. Line 4 inquires about name of
the file which is returned as a. Line 7 inquires if the file is closed. Since
the present set of commands have not closed the file so a boolean data type
False is returned. Line number 10 inquires about the mode of file, for
which write is returned. When the file is closed using instructions at Line
13 then the inquiry about its closing status at Line number 14 is returned
as a boolean object True. All attributes can still be inquired after closing
as seen in Line 17 and 20.
120
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
as input argument.
read() method reads a string from a file. Python strings can have binary
data apart from text data.
7.6 Buffering
This feature is incorporated when physical writes are performed on storage
devices. For small files, it is not needed. When very big files are dealt with
and data speed is slower then buffering (holding the data before recording)
is needed and a space in memory is required to hold the data before passing
it on to programs for reading and writing. When the value 0 is used to force
unbuffered operation, all file write operations are performed immediately.
When the value 1 is used, line buffering is enforced i.e. output lines are
written whenever a line terminator like \n is written. Any other positive
value for this argument will make a buffer size of that particular value,
available for the file operation.
121
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
7.7 Summary
File I/O is central part of a python programmer in all domains of studies.
Hence present chapter introduced the fundamentals of file I/O operations
using python. Various modes of file operations were illustrated and impor-
tance of creating, opening and closing files in a particular mode of operations
was illustrated. These actions are critical parts of real-world data analysis
where input data is usually in terms of files.
122
Functions and Loops
8
8.1 Introduction
A function is a part of a computer
program where a number of pro-
gramming statements are clubbed
as a block. It can be called as and
when desired. his enables modular
approach to programming tasks and
has become most popular amongst
programmers now-a-days as mod-
ules can be edited with ease instead
of finding edition in one big pro-
gram. Functions receive input pa-
rameters and returns output parameters. Which using a function, func-
tion name is called along with values for input parameters and after exe-
cution, a set of output parameters are returned. Python functions can be
defined at any place in the program, irrespective of the place from where
they are being called. They can even be defined as a separate file individu-
ally or in a combined manner. Also, they can be called any number of times
or may not be called too, as per requirements.
123
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
1. Header: Begins with a keyword (def) and ends with a colon (:)
124
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
execute. When a statement is written without indentation, function is ex-
ited.
Rules of defining a function name are same that for defining variable
names. It is wise to name them with functionally relevant names so that
they are easier to be recalled when in need. When return_parameetrs are
omitted, the functions returns a null object.
For example fn-hello.py as shown below, does not take any input param-
eters and simply executes the statements of printing the string Hello world
1 def greet () :
2 p r i n t H e l l o world
3
4 greet ()
fnhello.py
fnsq.py
125
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
Result of running the program is given below:
1 >>>0 0
2 1 1
3 2 4
4 3 9
5 4 16
6 5 25
7 6 36
8 7 49
9 8 64
10 9 81
Here the function square is called inside a for loop which increments
value of variable i from 1 to 10 (generated by the in-built function range()).
defmulti.py
The execution results in:
1 >>>(100, 1 0 2 , 2 0 2 )
126
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
loop is associated with a repetitive calculations because one defined variable
names and repetitively shuffles the variable values in a specified sequence
generated by a condition. For example, one might like to find square root of
first 10 integers. To perform this calculations, one need to run the function
sqrt() on a list of first 10 integers (which can be generated by range())
function. The list of integers can be stored in an integer and this variable
can be put into a loop to perform the operation of finding out square root
of each member of the list one-by-one.
ListMembers.py
127
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
list/array is formed as per the defined condition, loop can then be run of
list/array members. In this way, overall computation time can be reduced.
a2 + b2 = c2
This can be accomplished using for loop as given in the python code
pytha.py. In this python code, user inputs a number denoting the maximum
number for which this calculation will be performed. This ensures that
calculation has a proper end condition (without explicit definition).
1 # Program t o g e n e r a t e even Pythagorean numbers
2 # Pythagorean numbers a r e t h o s e numbers f o r which p y t h a g o r u s
equation stands true
3
4 from numpy import s q r t
5 n = r a w i n p u t ( P l e a s e i n p u t a maximum number : ) # Asks u s e r t o
i n p u t a number
6 n = i n t ( n )+1 # c o n v e r t s t h e v a l u e s s t o r e d i n v a r i a b l e n t o
i n t e g e r data type and adds 1 s o t h a t computation can be done
i f user feeds 0
7
8 # Two l o o p s t o d e f i n e a r r a y s f o r a and b f o r which c s h a l l be
computed
9 f o r a in range (1 , n) :
10 f o r b in range ( a , n) :
11 c s q u a r e = a 2 + b 2
12 c = i n t ( s q r t ( c s q u a r e ) ) # c i s c o n v e r t e d t o an i n t e g e r
13 i f ( ( c s q u a r e c 2 ) == 0 ) : # i f s q u a r e o f a and s q u a r e
o f b i s e q u a l t o s q u a r e o f c then t h e r e s u l t w i l l be z e r o
14 i f ( c%2 ==0) : # c h e c k i n g i f c i s an even number
15 print a , b , c
pytha.py
1 >>>Maximal Number? 20
2 6 8 10
3 12 16 20
As an exercise, one can write few more lines of code to check if there are
any prime number triplets as Pythagorean numbers.
128
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
8.7 if-else loop
In the example python code pytha.py, if statement has already been
used. It simply checks a condition, runs the loop if condition results True
boolean data and exits the loop if condition results False boolean data.
When multiple conditions needs to be checked in a sequence, if-else loops
are employed where if condition results True boolean data, the statement
is executed otherwise next condition is checked and similar operation is
performed recursively. This action is performed unless all conditions result
in returning the False boolean data.
1 # Program t o c a l c u l a t e s h i p p i n g c o s t based on money s p e n t and
location
2
3 t o t a l = i n t ( r a w i n p u t ( What i s t h e t o t a l amount f o r your o n l i n e
s h o p p i n g ?\ n ) )
4 a r e a = r a w i n p u t ( Type I i f you a r e s h o p p i n g w i t h i n I n d i a . . .
5 and O i f you a r e s h o p p i n g o u t s i d e I n d i a \n )
6
7 i f a r e a == I :
8 i f t o t a l <= 5 0 0 :
9 p r i n t S h i p p i n g C o s t s INR 2 0 . 0 0
10 e l i f t o t a l <= 1 0 0 0 :
11 p r i n t S h i p p i n g C o s t s INR 1 0 0 . 0 0
12 e l i f t o t a l <= 1 5 0 0 :
13 p r i n t S h i p p i n g C o s t s INR 2 5 0 . 0 0
14 else :
15 p r i n t FREE
16
17 i f a r e a == O :
18 i f t o t a l <= 5 0 0 :
19 p r i n t S h i p p i n g C o s t s INR 7 5 . 0 0
20 e l i f t o t a l <= 1 0 0 0 :
21 p r i n t S h i p p i n g C o s t s INR 2 0 0 . 0 0
22 e l i f t o t a l <= 1 5 0 0 :
23 p r i n t S h i p p i n g C o s t s INR 5 0 0 . 0 0
24 else :
25 p r i n t FREE
ifelif.py
129
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
7 FREE
8
9 >>>What i s t h e t o t a l amount f o r your o n l i n e s h o p p i n g ?
10 300
11
12 Type I i f you a r e s h o p p i n g w i t h i n I n d i a . . .
13 and O i f you a r e s h o p p i n g o u t s i d e I n d i a
14 I
15 S h i p p i n g C o s t s INR 2 0 . 0 0
Note that the statement()s) are indented for grouping. The statement(s)
can be single or multiple actions. The condition is a logical expression. The
loop iterated until the values of logical expression is True. As soon as it be-
comes False, the program control is passed to the next line. while loop
plays an important role in cases where looping must be skipped if condition
is not satisfied since none of satement is executed, if logical expression has
False value.
130
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
7
8 i = 0 # i n i t i a l i z i n g the counter
9 w h i l e ( i <10) : # c o u n t e r c o n d i t i o n
10 s t a r t = time . c l o c k ( ) # d e f i n i n g t h e v a r i a b l e which s t o r e s
time . c l o c k ( v a l u e )
11 p r i n t Square r o o t o f %d = %3.2 f : %( i , np . s q r t ( i ) ) #
p r i n t i n g t h e number and i t s s q u a r e r o o t
12 i=i +1 # i n c r e m e n t i n g t h e c o u n t e r
13 t i m i n g = time . c l o c k ( ) s t a r t # p r i n t s time taken t o e x e c u t e
two l i n e s o f code above
14 p r i n t Time taken f o r e x e c u t i o n = %e s e c o n d s \n % t i m i n g
15
16 p r i n t The end # s i g n i f i e s e x i t i n g t h e l o o p a f t e r c o n d i t i o n i s
satisfied
while.py
131
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
30
31 The end
Note that the output time might be different for each execution even on
same computer since time taken to process a line of code is functional of the
state of CPU at that particular moment of time.
infiniteloop.py
Since the condition remains true always so program will never quite dis-
playing the value of i, which is 1. It will never print the last line of code.
On a Linux machine, one needs to press CTRL+C to interrupt the executing
and come back to command line.
8.10 while-else
Within a while loop, the statements are executed if condition produces a
boolean value True. Using else statement within this structure allows the
user the route the flow of program if condition returns the boolean value
False.
1 i =0
2
3 w h i l e i <5:
4 print i
5 i=i +1
6 else :
7 p r i n t the value execeeds 5
132
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
whileelse.py
8.11 Summary
Functions enable modular structure of programs. Also, controlling the
flow of information as well as iterations have become the very basis of com-
putational work in most applications. These two actions are performed by
loops. Together, they make python a powerful tool for various applications.
Modular structure makes it easier to test and debug. Mastering both skills
of writing functions as well as choosing proper loop structure have become
key indicators for ranking a programmers performance to solve problems
using python codes. Hence the present chapter becomes one of the most
important ones for programmers.
133
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
134
Numerical Computing formalism
9
9.1 Introduction
135
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
In the present chapter, we shall try to understand some of the important
steps one must consider to solve a physical problem using numerical com-
putation. Defining a problem in proper term is just the first step. Making
the right model and then using the right method to solve (solver) enables
to distinguish between a naive and experienced scientist/engineer.
3x + 4y + 9z = 10
This is an equation having a term 3x + 4y + 9z on left hand side (LHS)
136
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
and a term 10 on right hand side (RHS). Please note that whereas LHS is
an algebraic term, RHS is a number.
In real life we can have situations which can be mixture of these scenarios.
Like an object can oscillate and move in curved fashion at the same time.
In that case we write an expression using mixture of functions or find new
functions which could explain the behavior of object. Verifying the func-
tions is done by finding solutions to equations describing the behavior and
matching it with observations taken on object. If they match perfectly, we
obtain perfect solutions. In most cases, an exact solutions might be difficult
to obtain. In these cases, we get an approximate solution. If the errors in-
volved while obtaining an approximate solution are within toleration limits,
the models can be acceptable.
x + 3y = 4 (9.1)
2x 4y = 3 (9.2)
137
Dr. Sandeep Nagar
www.bookmuft.com [email protected]
Then this system of two equations can be represented by a matrix equa-
tion as follows:
1 3 x 4
+ =
2 4 y 3
Now using matrix algebra, values of variables x and y can be found such
that they satisfy the equations. Those values are called roots of these equa-
tions. These roots are the point in 2-D space (because we had 2 dependent
variables) where the system will find stability for that physical problem. In
this way, we can predict the behavior of system without actually doing an
experiment.
For example, Newtons second law of motion says that rate of change
of velocity of an object is directly proportional to the force applied on it.
Mathematically:
dy
F (9.3)
dx
The proportionality is turned into equality by substituting for a constant
of multiplication m such that:
dy
F =m (9.4)
dx
138
Dr. Sandeep Nagar
[email protected] www.bookmuft.com
There are many methods to digitize and numerically solve a given func-
tion. Programs to implement a particular method to solve a function nu-
merically, is called a solver. A lot of solvers exist to solve a function. Choice
of solver is critical to successfully obtain a solution. For example, equation
9.4 is a differential equation. It is a first order ordinary differential equation.
A number of solvers exist to solve it like Euler, Runge-Kutta etc. Choice
of particular solver depends on accuracy of its solution, time taken for ob-
taining a solution and amount of memory used during the process. The
latter is important where memory is not an freely expendable commodity
like micro-computers with limited memory storage.
9.5 Summary
Almost all branch of science and engineering requires one to perform nu-
merical computation. Python is one of the alternative to do so. Python
has a library of optimized functions for general computation. Also it has a
variety of packages are present to perform a specialized job. This makes it
an ideal choice for prototyping a numerical computation problem efficiently.
139