
Learning SciPy for Numerical and Scientific Computing Second Edition
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
Book DescriptionThis book targets programmers and scientists who have basic Python knowledge and who are keen to perform scientific and numerical computations with SciPy.What you will learn
Get to know the benefits of using the combination of Python, NumPy, SciPy, and matplotlib as a programming environment for scientific purposes
Create and manipulate an object array used by SciPy
Use SciPy with large matrices to compute eigenvalues and eigenvectors
Focus on construction, acquisition, quality improvement, compression, and feature extraction of signals
Make use of SciPy to collect, organize, analyze, and interpret data, with examples taken from statistics and clustering
Acquire the skill of constructing a triangulation of points, convex hulls, Voronoi diagrams, and many similar applications
Find out ways that SciPy can be used with other languages such as C/C++, Fortran, and MATLAB/Octave
Who this book is forThis book targets programmers and scientists who have basic Python knowledge and who are keen to perform scientific and numerical computations with SciPy.
All prices
More details
Other editions
Additional editions

Person
But the most special thanks go without a doubt to my wife and daughter. Grace's love and smiles alone provided all the motivation, enthusiasm and skills to overcome any difficulties encountered during the pursuit of this book, and everything life threw at me ever since she was born.
Content
Introduction to SciPy
Top-level SciPy
SciPy for Linear Algebra
SciPy for Numerical Analysis
SciPy for Signal Processing
SciPy for Data Mining-
SciPy for Computational Geometry
Interaction with Other Languages
Array routines
In this section, we will deal with most operations on arrays. We will classify them into four main categories:
- Routines to create new arrays
- Routines to manipulate a single array
- Routines to combine two or more arrays
- Routines to extract information from arrays
The reader will surely realize that some operations of this kind can be carried out by methods, which once again shows the flexibility of Python and NumPy.
Routines to create arrays
We have previously seen the command to create an array and store it to a variable A. Let's take a look at it again:
The complete syntax, however, writes as follows:
array(object,dtype=None,copy=True,order=None, subok=False,ndim=0)Let's go over the options: object is simply the data we use to initialize the array. In the previous example, the object is a 2 x 2 square matrix; we may impose a datatype with the dtype option. The result is stored in the variable A. If copy is True, the returned object will be a copy of the array, if False, the returned object will only be a copy, if dtype is different from the datatype of object. The arrays are stored following a C-style ordering of rows and columns. If the user prefers to store the array following the memory style of FORTRAN, the order='Fortran' option should be used. The subok option is very subtle; if True, the array may be passed as a subclass of the object, if False, then only ndarray arrays are passed. And finally, the ndmin option indicates the smallest dimension returned by the array. If not offered, this is computed from object.
A set of special arrays can be obtained with commands such as zeros, ones, empty, identity, and eye. The names of these commands are quite informative:
zeroscreates an array filled with zeros.onescreates an array filled with ones.emptyreturns an array of required shape without initializing its entries.identitycreates a square matrix with dimensions indicated by a single positive integer n. The entries are filled with zeros, except the diagonal, which is filled with ones.
The eye command is very similar to identity. It also constructs diagonal arrays, but unlike identity, eye allows specifying diagonals offset the traditional centered, as it can operate on rectangular arrays as well. In the following lines of code, we use zeros, ones, and identity commands:
In the first two cases, we indicated the shape of the array (as a Python tuple of positive integers) and the optional datatype imposition.
The syntax for eye is as follows:
The integers, N and M indicate the shape of the array, and the integer k indicates the index of the diagonal to populate.
An index k=0 (the default) points to the traditional diagonal; a positive index refers to upper diagonals and negative to lower diagonals. To illustrate this point, the following example shows how to create a 4 x 4 sparse matrix with nonzero elements on the first upper and subdiagonals:
The output is shown as follows:
[[ 0. 1. 0. 0.] [ 1. 0. 1. 0.] [ 0. 1. 0. 1.] [ 0. 0. 1. 0.]]Using the previous four commands together with basic slicing, it is possible to create even more complex arrays very simply. We propose the following challenge.
Use exclusively, the previous definitions of U and I together with an eye array. How would the reader create a 5 x 5 array A of values, type float with fives at the four entries (0, 0), (0, 1), (1, 0), and (1, 1); sixes along the remaining entries of the diagonal; and threes in the two other corners ? The solution to this question can be addressed by issuing the following set of commands:
The output is shown as follows:
[[ 5. 5. 0. 0. 3.] [ 5. 5. 0. 0. 0.] [ 0. 0. 6. 0. 0.] [ 0. 0. 0. 6. 0.] [ 3. 0. 0. 0. 6.]]The flexibility of creating an array in NumPy is even more clear using the fromfunction command. For instance, if we require a 4 x 4 array where each entry reflects the product of its indices, we may use the lambda function (lambda i,j: i*j) in the fromfunction command, as follows:
The output is shown as follows:
[[0 0 0 0] [0 1 2 3] [0 2 4 6] [0 3 6 9]]A very important tool dealing with arrays is the concept of masking. Masking is based on the idea of selecting or masking those indices for which their corresponding entries satisfy a given condition. For example, in the array B shown in the previous example, we can mask all zero-valued entries with the B==0 command, as follows:
The output is shown as follows:
[[ True True True True] [ True False False False] [ True False False False] [ True False False False]]Now, how would the reader update B so that all zero's would be replaced by the sum of the squares of their corresponding indices?
Multiplying a mask by a second array of the same shape offers a new array in which each entry is either zero (if the corresponding entry in the mask is False), or the entry of the second array (if the corresponding entry in the mask is True):
The output is shown as follows:
[[0 1 4 9] [1 1 2 3] [4 2 4 6] [9 3 6 9]]Note that we have created a new array filled with Boolean values as the size of the original array and in each step. This isn't a big deal in these toy examples, but when handling large datasets, allocating too much memory could seriously slow down our computations and exhaust the memory of our system. Among the commands to create arrays, there are two in particular putmask and where, which facilitate the management of resources internally, thus speeding up the process.
Note, for example, when we look for all odd-valued entries in B, the resulting mask has size of 16, although the interesting entries are only eight:
The output is shown as follows:
[[False True False True] [ True True False True] [False False False False] [ True True False True]]The numpy.where() command helps us gather those entries more efficiently. Let's take a look at the following command:
The output is shown as follows:
(array([0, 0, 1, 1, 1, 3, 3, 3], dtype=int32),array([1, 3, 0, 1, 3, 0, 1, 3], dtype=int32))If we desire to change those entries (all odd), to, say they are squares plus one, we can use the numpy.putmask() command instead, and better manage the memory at the same time. The following is a sample code for the numpy.putmask() command:
The output is shown as follows:
[[ 0 2 4 82] [ 2 2 2...System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: PDF
Copy-Protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (only limited: Kindle).
The file format PDF always displays a book page identically on any hardware. This makes PDF suitable for complex layouts such as those used in textbooks and reference books (images, tables, columns, footnotes). Unfortunately, on the small screens of e-readers or smartphones, PDFs are rather annoying, requiring too much scrolling.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our eBook Help page.