SciPy

SciPy

Welcome, curious minds and science enthusiasts, to an exciting expedition through the boundless realm of scientific computing! In this blog, we embark on a thrilling exploration of one of the most powerful tools at the forefront of cutting-edge research and data analysis: SciPy.


Imagine a treasure trove of mathematical functions, statistical tools, optimization techniques, and signal processing algorithms all neatly packed within a single Python library. This is the essence of SciPy - an open-source library that empowers scientists, engineers, researchers, and data analysts to unlock the true potential of their data and make groundbreaking discoveries.


Whether you're an ardent Pythonista or just beginning to dip your toes into the world of programming, SciPy promises an awe-inspiring experience, taking you on an extraordinary journey where complex challenges find elegant solutions.


Throughout this blog, we will unravel the mysteries of SciPy, understanding its underlying components, delving into its impressive capabilities, and witnessing its real-world applications. From harnessing the power of numerical methods to unraveling intricate data patterns, SciPy will be your steadfast companion on this exhilarating quest.

Are you ready to dive in? Let's take the plunge together and witness the magic of SciPy unfold before our eyes!

SciPy library in Python (Scientific Python) is an open-source library released in 2001 and used for scientific computing. It is predominantly used in Python programming. SciPy in Python is the core package for scientific routines. It is used for solving various scientific problems and machine-learning tasks.

It is built on NumPy, another library used to perform mathematical operations on arrays and matrices. NumPy and SciPy in Python work hand in hand. 


SciPy is an incredibly powerful library that can be used to tackle a wide range of complex problems in various scientific and engineering domains. Let's explore some examples of how SciPy can be utilized in different scenarios:

Optimization Problems: SciPy provides a collection of optimization algorithms that can be used to find the minimum or maximum of a function. Whether you're optimizing a business process, fitting a complex model to data, or fine-tuning parameters in a machine learning model, SciPy's optimization capabilities can be a game-changer.

Signal Processing: For tasks like filtering, Fourier transforms, and signal analysis, SciPy's signal processing module comes in handy. It allows you to process and manipulate signals efficiently, making it invaluable in various domains such as audio and image processing.

Statistics and Probability: SciPy's statistical functions and distributions are useful for hypothesis testing, descriptive statistics, probability density functions, and much more. Researchers and data scientists often turn to SciPy for their statistical analyses.

Partial Differential Equations (PDEs): In engineering and physics, solving PDEs can be a challenging task. SciPy offers tools for numerical integration and solvers that make it feasible to simulate and analyze complex systems described by PDEs.


Numerical Integration: When it comes to integrating functions or solving differential equations, SciPy's numerical integration capabilities shine. It includes adaptive integration, quadrature methods, and various ode solvers to handle different types of problems.

Image Processing: From basic image manipulation to more advanced tasks like edge detection, denoising, and image segmentation, SciPy's image processing module is a powerful tool in computer vision and related fields.


Machine Learning: While SciPy itself is not a machine learning library, it can be used in conjunction with other ML libraries like scikit-learn. SciPy's numerical routines often underpin many algorithms in machine learning models.

Spatial Data Analysis: For geospatial data analysis, SciPy's spatial module provides functionality for distance calculations, clustering, interpolation, and more.

Sparse Matrices: When dealing with large and sparse datasets, SciPy's sparse matrix support can significantly reduce memory usage and speed up computations.

Interpolation and Extrapolation: SciPy offers various methods for interpolation and extrapolation, useful for tasks like function approximation or filling missing data points.

No alt text provided for this image

SciPy can be installed in Spyder/Jupyter directly or it can also be installed through Anaconda Prompt.

  1. Via Jupyter / Spyder

No alt text provided for this image
No alt text provided for this image

2. Via Anaconda Prompt 

No alt text provided for this image

How to upgrade to the new version?

SciPy module in Python gets upgraded with new releases. As of Jun 28, 2023, the release is the 1.11.1 version. 

To upgrade to the latest version, the below code is used. 

No alt text provided for this image

Understand more about SciPy through Python:

Python has inbuilt functions that retrieve information/guidelines about the packages & functions and the parameters concerning them. 

Below are two must-know functions used to quickly refer to any package.

  • dir will provide us with a list of methods available on the object of interest
  • help will inspect a specific method we would like to use

No alt text provided for this image
No alt text provided for this image

Sub-Packages:

SciPy has multiple sub-packages with various functionalities. It can be imported as shown below: 

No alt text provided for this image

Example:

No alt text provided for this image
No alt text provided for this image

Import Packages: 

No alt text provided for this image

Or 

No alt text provided for this image

SciPy Python Library: Packages and its uses

No alt text provided for this image

Commonly used packages:

No alt text provided for this image

A quick reference to various functions in SciPy:

The SciPy program in Python has many functions and sub-functions. Here, listed are a few commonly used functions:

  • Business Moments 
  • Boxcox
  • IQR 
  • Constants 
  • Datasets
  • Access Files
  • Normal Distribution 


  1. Business Moments: 

First Business Moment: Mean, Median and Mode 

The formula for mean, median and mode is:

No alt text provided for this image

Import packages:

No alt text provided for this image

Or

No alt text provided for this image

Mean:

mean() 

No alt text provided for this image
No alt text provided for this image

Median: 

median() 

No alt text provided for this image
No alt text provided for this image

Mode:

mode() 

No alt text provided for this image
No alt text provided for this image

Second Business Moment: Variance, Standard Deviation 

The formula for variance and standard deviation is:

No alt text provided for this image

Variance: 

var()

No alt text provided for this image
No alt text provided for this image

Standard deviation: 

Std()

No alt text provided for this image
No alt text provided for this image

Third Business Moment: Skewness 

The formula for skewness is:

No alt text provided for this image
No alt text provided for this image

skew()

No alt text provided for this image
No alt text provided for this image

Fourth Business Moment: Kurtosis

The formula for Kurtosis is:

No alt text provided for this image

kurt()

No alt text provided for this image


2. Boxcox:

SciPy package in Python has Boxcox function which is used to transform non-normal data into normal data. 


Required packages are imported. 

A random non-normal data (exponential distribution) set is created. 

Boxcox() function is used to transform the data to normal distribution. 

The lambda value used for transformation is found. 

No alt text provided for this image

The non-normal and normal data are shown below: 

No alt text provided for this image

The Lambda value used to normalize the data is 0.238934.



3. IQR: Interquartile Range 

The formula for the Interquartile range is:

Interquartile range = Upper Quartile – Lower Quartile = Q3 – Q1

No alt text provided for this image

4. Constants

SciPy provides various physics and mathematical constants which are used in scientific calculations. 

Below is the list of constants:

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Constant Values

The code to find the constant value is shown in the below example. 

No alt text provided for this image

Search / Find

The below finds out all the constants containing the 'proton' word in it and returns a list of constants.

No alt text provided for this image
No alt text provided for this image

Value with Unit and Degree of Uncertainty 

Along with the magnitude of the constant, we can also access the unit and degree of uncertainty 

No alt text provided for this image

Example of how to use constant: 

Area of circle 

No alt text provided for this image

5. Datasets:

Get a color image of a raccoon face

No alt text provided for this image

6. Access File

Here are some ways to load and read a file using scipy.io. 

The scipy.io module is part of the SciPy library and provides functions for working with various file formats used in scientific computing, such as MATLAB files, NetCDF files, and WAV files.

No alt text provided for this image

7. Normal Distribution 

Python SciPy library helps generate all kinds of normal distribution. The normal distribution is the symmetrical distribution of data around its mean value. It is also called the Bell Curve. 


The object norm() in scipy.stats function is used to generate normal distributions. The parameters are data, size, moments loc and scale.

The normal distribution depends on the mean and standard deviation. Hence the loc is for the mean and the scale is for the standard deviation.  

No alt text provided for this image

Probability Distribution Function:  

Method 1: 

The mean and standard deviation values are entered manually. 

No alt text provided for this image

Method 2: 

The mean and standard deviation values are calculated using Numpy and then used to plot.

No alt text provided for this image

Cumulative Distribution Function:

Method 1: 

The mean and standard deviation values are entered manually. 

No alt text provided for this image

Method 2: 

The mean and standard deviation values are calculated using Numpy and then used to plot.

No alt text provided for this image

Random Variates:

Random Variate in norm object returns the specified number of random numbers in the normal distribution. 

No alt text provided for this image

Probability Point Function: 

No alt text provided for this image
No alt text provided for this image

Before we conclude, let's have a little interactive.


Imagine you are a scientist working on a fascinating project, and you need to utilize SciPy to solve a critical problem. In this scenario, you're investigating the spread of a deadly virus in a densely populated city. You have data on the virus's transmission rates, population density, and other factors. Your task is to use SciPy to create a predictive model and find the best strategy to control the virus's spread.


In the comments section below, let's brainstorm and interactively build the steps for this critical task using SciPy. Feel free to contribute ideas, ask questions, or suggest different approaches.




To view or add a comment, sign in

More articles by 360DigiTMG

Others also viewed

Explore content categories