Introduction

0

In this guide, you will learn about the core library used to perform operations related to the field of mathematics, science, and engineering.

By the end of this guide you'll have a strong understanding of the following:

- A basic understanding of SciPy
- Basic operations in SciPy
- Frequently used sub-packages in SciPy

Throughout this guide, we will be using the following libraries:

`1 2`

`import numpy as np import scipy as sp`

python

SciPy, or Scientific Python, is a python library which extends the functionality of the NumPy library. It adds significant power to the interactive Python session by providing the user with high-level commands and classes for manipulating and visualizing data. With SciPy an interactive Python session becomes a data-processing and system-prototyping environment; rivaling systems such as MATLAB, IDL, Octave, R-Lab, and SciLab.

The additional benefit of basing SciPy on Python is that this also makes a powerful programming language available for use in developing sophisticated programs and specialized applications. Scientific applications using SciPy benefit from the development of additional modules in numerous niches of the software landscape by developers across the world. Everything from parallel programming to web and data-based subroutines and classes have been made available to the Python programmer. All of this power is available, in addition to the mathematical libraries in SciPy.

The structure of SciPy is defined based on the various sub-packages where each sub-package covers different scientific computing domains. Here's the list of various sub-packages along with a brief description.

Subpackage | Description |
---|---|

cluster | Clustering algorithms |

constants | Physical and mathematical constants |

fftpack | Fast Fourier Transform routines |

integrate | Integration and ordinary differential equation solvers |

interpolate | Interpolation and smoothing splines |

io | Input and Output |

linalg | Linear algebra |

ndimage | N-dimensional image processing |

odr | Orthogonal distance regression |

optimize | Optimization and root-finding routines |

signal | Signal processing |

sparse | Sparse matrices and associated routines |

spatial | Spatial data structures and algorithms |

special | Special functions |

stats | Statistical distributions and functions |

To know more about each of the above sub-packages, you can refer to the following webpage.

Above, we observed what SciPy is and what it constitutes. Now, let us learn to install it using two ways:

- Using pip
- Using Linux package manager

Usually, it is advised to go for Anaconda installation, as it already has SciPy and other important libraries pre-installed for you. However, you can still install SciPy using the given methods.

To install SciPy using pip, you first need to ensure that you have python as well as pip installed in your system. Later, you will need to run the following command in the command prompt or Anaconda prompt (for Anaconda users).

`1`

`python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy`

This will install not only SciPy but also NumPy, matplotlib, ipython, jupyter, pandas, and sympy. Using`--user`

while installing makes the current installation available only for the current local user and doesn't write to the system directories.

If you're using an Ubuntu/Debian Linux distribution, then installing SciPy can be done using the terminal with the following command:

`1`

`sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy`

This command again installs SciPy along with other important libraries that are similar to the libraries installed when installing SciPy using Pip.

To install SciPy for Fedora, Mac, or using other ways, you can refer to the following link:

As we have mentioned preivously, SciPy extends the functionality of the NumPy module. Most of the basic operations are performed using NumPy methods. Let us discuss some of the important methods used in the following topics:

- Indexing
- Polynomials
- Vectorizing
- Others

We are well familiar with the common python indexing, which is used in subsetting a NumPy array, using the `:`

operator. Let us learn a few other methods to perform indexing based on `np.r_`

, `np.c_`

, `np.mgrid`

, and `np.ogrid`

.

The `np.r_`

and `np.c_`

methods help to concatenate row-wise and column-wise. The functionality for both methods is also different in the case of 1-D data. Let us learn it practically.

`1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31`

`# Onedimensional data arr = [5, 9, 8] # np.r_ print(np.r_[arr].shape) # Output: (3,) # np.c_ print(np.c_[arr].shape) # Output: (3,1) # Two-dimensional data mat = [[1, 2], [3, 4]], [[5, 6], [7, 8]] # np.r_ print(np.r_[mat]) # Output: # [[1 2] # [3 4] # [5 6] # [7 8]] # np.c_ print(np.c_[mat]) # Output: # [[1 2 5 6] # [3 4 7 8]]`

python

The next two methods, `mgrid`

and `ogrid`

, help to create a mesh and can be treated as a better alternative to `arange`

as shown:

`1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17`

`np.mgrid[0:3,0:2] # Output: # array([[[0, 0], # [1, 1], # [2, 2]], # [[0, 1], # [0, 1], # [0, 1]]]) np.ogrid[0:3,0:2] # Output: # [array([[0], # [1], # [2]]), array([[0, 1]])]`

python

The polynomials are handled using the `poly1d`

class from the NumPy module. You can create a polynomial, perform basic mathematical operations like add, subtract, etc., as well as do complex operations including differentiation and integration as shown here:

`1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19`

`# Creating a base polynomial of the form x^2 + 2x + 3 p = np.poly1d([1, 2, 3]) # Subtracting p with a new poly of the form x^2 -2x + 3 p - np.poly1d([1, -2, 3]) # This gives us a polynomial of the form 4x + 0 # as represented by the output poly1d([4, 0]) # Performing first differentiation of polynomial p p.deriv() # This results in the polynomial of the form 2x + 2 # Performing integration of the polynomial p p.integ() # This results in the polynomial of the form # 0.333x^3 + x^2 + 3x + 0`

python

NumPy also provides an important method named as `vectorize`

which converts a python scalar function to a vectorized format with the same broadcasting rules as other NumPy functions (i.e. the Universal functions, or ufuncs). Let us take a look at an example to understand it:

`1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22`

`# Defining a common python function def dosomething(arg1, arg2): if arg1 > 0: return arg1 * arg2 else: return arg1 - arg2 # Normally if we pass a python scalar to the above # function, it returns a scalar as shown print(dosomething(5, 10)) # Output: 50 # However, we can use the same function with the # ability of vectorization as shown vectorized_func = np.vectorize(dosomething) vectorized_func([2, -1, 3], [6, 9, 1]) # which results in a vector array([ 12, -10, 3])`

python

There are many other important functions available inside NumPy. We shall end with another main method termed as `select`

. You may have wondered how to establish an `if-elif-else`

ladder while performing vectorization. The `select`

method does this for you. It takes an element, checks it against the first condition, and, if the first condition is failed, it goes on to the next condition and so on. Whenever a condition is passed the corresponding output is returned. The syntax and code clarify it further:

**Syntax**

- Vector
- Multiple conditions: if, elif, elif, ..., else
- Corresponding output: Out1, Out2, Out3, ..., OutN

`1 2 3 4 5 6 7 8 9 10 11 12 13 14`

`# Initializing input vector x = np.arange(10) # Multiple conditions ladder multiple_cond = [x<3, x == 4, x>5] # Corresponding output corresponding_out = [x, 3*x, 2*x] # select method np.select(multiple_cond, corresponding_out) # Output: # array([ 0, 1, 2, 0, 12, 0, 12, 14, 16, 18])`

python

We have referenced all the sub-packages by which SciPy module is organized. However, to learn specifically about a few of the most important packages, you can refer to the below guides in this series:

In this guide, you have learned about the definition, organization, and basic operations of SciPy.

0

Test your skills. Learn something new. Get help. Repeat.

Start a FREE 10-day trial