# SciPy

SciPy is an open-source Python package used for scientific and engineering computing. It contains several user-friendly and efficient modules or sub-packages for linear algebra, optimization, interpolation, fast Fourier transform, Ordinary Differential Equations (ODE) solvers, and image processing.

It is built on top of NumPy, which provides the base for the library’s array objects and numerical computations. NumPy is also a widely used library in scientific computing and is often used alongside SciPy.

In addition, this library also integrates well with other scientific Python packages, such as Matplotlib for data visualization and Pandas for data manipulation. Users are encouraged to use SciPy when working with scientific engineering calculations.

SciPy is actively maintained and updated, adding new features and improvements regularly. It has a large and active community of users and contributors, and, as a result, many resources are available online.

## Components and Modules

SciPy contains several modules designed to provide scientific computing capabilities. Some of the key modules include:

#### Cluster

It provides functions for clustering and related tasks, which are commonly used in machine learning, data mining, and pattern recognition. Clustering consists of dividing data into groups or clusters based on their similarity. Cluster functions include hierarchical, vector quantization, and k-means algorithms.

#### Fftpack

It provides functions for computing fast Fourier transforms (FFT). FFT is an essential mathematical method for signal processing, image processing, and data analysis. It also supports functions for computing discrete Fourier transforms.

#### Integrate

It provides functions for numerical integration and differentiation. These numerical operations are relevant in many areas of science and engineering, for example, to compute rates, areas, volume, and other physical systems.

#### Interpolate

It provides functions to perform interpolation on tensors. In the mathematical field, interpolation estimates a value between two or more known values. Its accuracy will depend on the method and data quantity used to estimate it.

#### Optimize

It is a SciPy module that provides functions for optimization, root-finding, and minimization of scalar functions. Some of the commonly used functions in the “**optimize**” module are “**minimize**,” “**root**,” “**curve_fit**,” and “**linprog**.”

## SciPy vs. Python Packages

#### NumPy

NumPy is an open-source Python package specializing in large, multi-dimensional arrays, matrices, and high-level mathematical functions. It provides efficient array computations by allowing array elements to be stored together in memory, which makes them easily and quickly accessible. This feature improves performance significantly, especially when dealing with large datasets.

#### Pandas

Pandas is a Python package that assists in data analysis and manipulation. It is built on top of NumPy and provides fast and efficient operations on arrays, data frames, and series. For example, Pandas allows users to concatenate and merge arrays and easily view and manipulate data.

#### Scikit-learn

Scikit-learn is an open-source Python package built upon Numpy and SciPy. It is a collection of modules dedicated to machine learning tasks, including classification, regression, random forests, gradient boosting, and k-means. Scikit-learn integrates many other Python libraries to provide a complete data analysis and visualization toolkit. The most famous libraries used with Scikit-learn are Matplotlib, Plotly, Seaborn, and Pandas.

## Quick Installation Guide

SciPy is already included in the Anaconda distribution by default. To verify that SciPy is in your system, use the following command in the Anaconda prompt:

conda list

It will print a list and you will be able to see the “scipy” package. In case your system does not contain SciPy, you can install it by executing:

conda install -c anaconda scipy

It will download and install the latest SciPy version and any required dependencies. Alternatively, if you prefer to use pip:

pip install scipy

Once the installation is complete, you can import the package into your code by specifying the subpackage you want. In other words, users must import modules individually.

from scipy import integrate from scipy.optimize import minimize

## SciPy Fundamentals

### Fourier Transforms

Fourier analysis is a transform method that helps to decompose a temporal function or signal into a sum of periodic components. Use the “**.fft()**” function to perform this operation.

import numpy as np from scipy.fft import fft # Generate a test signal t = np.linspace(0, 2*np.pi, 1000) signal = np.sin(5*t) + 0.5*np.sin(10*t) # Compute the FFT of the signal fft_signal = fft(signal) # Compute the power spectrum of the FFT power_spectrum = np.abs(fft_signal)**2 # Plot the power spectrum import matplotlib.pyplot as plt freqs = np.fft.fftfreq(signal.size, t[1]-t[0]) idx = np.argsort(freqs) plt.plot(freqs[idx], power_spectrum[idx]) plt.xlabel('Frequency') plt.ylabel('Power Spectrum') plt.show()

In the example, the signal is the sum of two sine waves with frequencies of 5 and 10 Hz. The “**.fft()**” function computes the FFT of the signal, giving the amplitudes of its frequency components. The code also computes the power spectrum by squaring the FFT. As a result, we get the power distribution vs. the signal’s frequency components.

### Integration Functions

You can use the “**integrate.quad()**” function to integrate a function and declare the lower and upper bounds.

from scipy import integrate import numpy as np # Define the function to integrate def f(x): return np.sin(x) # Integrate the function from 0 to pi result, error = integrate.quad(f, 0, np.pi) # Print the result print("The integral of sin(x) from 0 to pi is: ", result) print("Error: ", error)

## Result The integral of sin(x) from 0 to pi is: 2.0 Error: 2.220446049250313e-14

In the example, we integrate the “**sin(x)**” function from 0 to pi. The function returns the value of the integral and estimates the absolute error of the result.

If you need to integrate a function of multiple variables, you can use the “**nquad()**” function instead.

### Optimization

For optimization operations, SciPy provides various modules. For example, you can use the “**minimize()**” function to discover the minimum value of a scalar function.

import numpy as np from scipy.optimize import minimize # Define the scalar function def f(x): return x**2 + 2*x + 1 # Use the Nelder-Mead algorithm to minimize the function result = minimize(f, x0=0, method='nelder-mead') # Print the result print(result)

## Result final_simplex: (array([[-1. ], [-0.9999375]]), array([0.00000000e+00, 3.90624999e-09])) fun: 0.0 message: 'Optimization terminated successfully.' nfev: 50 nit: 25 status: 0 success: True x: array([-1.])

The example uses a scalar function f(x) = x^2 + 2x + 1, and applies the Nelder-Mead numerical method to find the minimum value of the objective function. Scientists can use it for nonlinear optimization problems, particularly when derivatives are unknown.

## Highlights

### Project Background

- Project: SciPy
- Author: Travis Oliphant, Pearu Peterson, Eric Jones
- Initial Release: 2001
- Type: Technical Computing
- License: New BSD Licence
- Contains: luster, constant, integrate, interpolate, optimize, signal, park, etc
- Language: Python, Fortran, C, C++
- GitHub: scipy/scipy
- Runs On: Windows, Linux, MacOS
- Twitter: –

### Main Features

- It provides additional tools for array computing.
- Users can compute complex math operations like FFT and conduct optimization problems.
- SciPy provides functions for linear algebra, interpolation, ODE solvers, image processing, and many more, making it a useful tool for scientists and engineers.

### Prior Knowledge Requirements

- Understanding basic programming concepts such as data structures, loops, functions, and methods.
- Familiarity with the Python language itself and its syntax.
- Deep knowledge of Fourier transforms, optimization problems, and imaging processing.

### Community Benchmarks

- 10,900 Stars
- 4,600 Forks
- 1,270+ Code contributors
- 75+ releases
- Source: GitHub

### Releases

- SciPy 1.10.1 (2-20-2023): Update and Fixes: e.g., interpolate.interpn fails with read only input.
- SciPy 1.10.0 (1-3-2023): Update and Fixes. This release requires Python 3.8+ and NumPy 1.19.5 or greater.
- SciPy 1.9.3 (10-19-2022): Update and Fixes: e.g., dtype not preserved with operations on sparse arrays.
- SciPy 1.9.0 (7-29-2022): Update and Fixes: e.g., Added scipy.optimize.milp, new function for mixed-integer linear programming.
- SciPy 1.8.0 (2-5-2022): Users should expect minor API refinements over the next few releases.