DEV Community

Dr. Azad Rasul
Dr. Azad Rasul

Posted on

13- Using SciPy (Scientific Python) library in Python

SciPy denotes Scientific Python and it is valuable for scientific analysis.

SciPy installation:

pip install SciPy
Enter fullscreen mode Exit fullscreen mode

Examples of the functions in SciPy:

  • Integration: (scipy.integrate)

  • Optimization/Fitting: (scipy.optimize)

  • Interpolation: (scipy.interpolate)

  • Signal Processing: (scipy.signal)

  • Spatial data structures and algorithms: (scipy.spatial)

  • Statistics: (scipy.stats)

  • Multi-dimensional image processing: (scipy.ndimage)

Importing from SciPy

from scipy import optimize
from scipy import spatial
# first form
from scipy import stats
# second form
from scipy.stats import distributions
Enter fullscreen mode Exit fullscreen mode

T-Test

from scipy.stats import ttest_ind
x = ([1, 3, 5, 7, 11])
y = ([2, 4, 6, 8, 4])
res = ttest_ind(x, y)
print(res)
Enter fullscreen mode Exit fullscreen mode

Statistical Description of Data

import numpy as np
from scipy.stats import describe

x = np.random.normal(size=50)
res = describe(x)

print(res)
# DescribeResult(nobs=50, minmax=(-2.5511668761037507, 1.7772593602939395), mean=0.02937722230632724, variance=0.9754504451804601, skewness=-0.12226936632001595, kurtosis=-0.39363575297869824)
Enter fullscreen mode Exit fullscreen mode

Interpolation

import numpy as np
x = np.array([0., 1.,5., 8., 10.])
y = np.array([0., 4., 1., 6., 8.])
f = interp1d(x, y)
f(3)
# array(2.5)
Enter fullscreen mode Exit fullscreen mode

Integration

import scipy.integrate
f= lambda x:np.exp(x**1)
# print results
i = scipy.integrate.quad(f, 1, 2) # quad -- General purpose integration.
print(i)
# (4.670774270471606, 5.1856011379043454e-14)
Enter fullscreen mode Exit fullscreen mode

Input and Output

Scipy.io package provides multiple methods to handle inputs and outputs of multiple formats such as:

  • Matlab

  • Netcdf

  • IDL

  • Arff

  • Matrix Market

  • Wave

import scipy.io as syio

# Save the mat file
n = 14031977
syio.savemat('test.mat', {'test': n})

# Load the mat File
matf_contents = syio.loadmat('test.mat')
print(matf_contents)

# printing the contents of mat file.
matf_contents = syio.whosmat('test.mat')
print(matf_contents)

#{'__header__': b'MATLAB 5.0 MAT-file Platform: nt, Created on: Mon Jun 28 16:15:59 2021', '__version__': '1.0', '__globals__': [], 'test': array([[14031977]])}
[('test', (1, 1), 'int32')]
Enter fullscreen mode Exit fullscreen mode

If you like the content, please SUBSCRIBE to my channel for the future content.

To get full video tutorial and certificate, please, enroll in the course through this link: https://www.udemy.com/course/python-for-researchers/?referralCode=886CCF5C552567F1C4E7

Top comments (0)