×

Python SciPy Library

Introduction

SciPy, a logical library for Python is an open-source, BSD-authorized library for arithmetic, science, and design. The SciPy library relies upon NumPy, which gives advantageous and quick N-dimensional exhibit control. The fundamental justification for assembling the SciPy library is that it should work with NumPy exhibits. It gives numerous easy-to-understand and effective mathematical practices like routines for mathematical joining and improvement. This is a basic instructional exercise, which covers the essentials of SciPy and depicts how to manage its different modules.

SciPy vs NumPy

SciPy:

  1. SciPy is a logical calculation library that utilizes NumPy beneath. 
  2. SciPy represents Scientific Python. 
  3. It gives greater utility capacities to streamlining, details, and sign handling. 
  4. SciPy is open source so we can utilize it unreservedly. 
  5. SciPy was made by NumPy's maker Travis Oliphant.

Numpy:

  • Numpy is written in C and use for mathematical or numeric calculation.
  • It is faster than other Python Libraries
  • Numpy is the most useful library for Data Science to perform basic calculations.
  • Numpy contains nothing but array data type which performs the most basic operation like sorting, shaping, indexing, etc.
  • Numpy is used for numerical or mathematic computations.
  • Numpy is the most valuable library for Data Science for performing the essential operations. 
  • It is quicker and nimble than other Python Libraries 
  • Numpy contains only cluster information type which plays out the most essential activity like arranging, forming, ordering, and so on.

Installation of SciPy

If our system already has Python and PIP then the installation process of SciPy is very simple.

Installing SciPy in Windows via pip.

pip install scipy 

Before we begin learning SciPy Python, we need to realize fundamental usefulness just as various kinds of an array of NumPy.

After the installation of SciPy, import the SciPy module(s) which you want to apply in your applications and we can do this with 

from scipy import module statement:

The usual way to import SciPy modules and Numpy is:

from scipy import special   
import numpy as np

Example:

from scipy.special import cbrt
#Find cubic root of 27 & 64 using cbrt() function
cb = cbrt([27, 64])
#print value of cb
print(cb)

Output: 

array([3., 4.])

Checking version of SciPy

The __version__ attribute stores the string of the version.

Example:

import scipy


print(scipy.__version__)

SciPy Constants

The scipy.constants bundle gives different constants. We need to import the necessary consistency and use them according to the prerequisite. Allow us to perceive how these consistent factors are imported and utilized.

Example:

constant value of PI:


from scipy import constants


print(constants.pi)

Output:

3.141592653589793

Constant Units

A record of all units under the constants module can be seen by utilizing the dir() work. The dir() work returns the record of all units under the constant module.

Example

List all constants:

from scipy import constants


print(dir(constants))

Output:

It returns a list of constant units such as 

'Avogadro', 'Boltzmann', 'Btu', 'Btu_IT', 'Btu_th', 'C2F', 'C2K', 'ConstantWarning', 'F2C', 'F2K', 'G', 'Julian_year', 'K2C', 'K2F', 'N_A', 'Planck', 'R', 'Rydberg', 'Stefan_Boltzmann'

Categories of units

The units are located under certain categories:

  • Binary
  • Mass
  • Metric
  • Volume
  • Pressure
  • Energy
  • Speed
  • Power
  • Angle
  • Time
  • Length
  • Temperature
  • Force

Binary:

Specified Units returned in bytes

from scipy import constants


print(constants.kibi)   
print(constants.mebi)  
print(constants.gibi)  
print(constants.tebi)    
print(constants.pebi)    
print(constants.exbi)    
print(constants.zebi)   
print(constants.yobi)  

Output:

1024
1048576
1073741824
1099511627776
1125899906842624
1152921504606846976
1180591620717411303424
1208925819614629174706176

Mass:

The specified unit returned in kg

from scipy import constants
print(constants.gram)
print(constants.metric_ton)
print(constants.grain)
print(constants.lb)
print(constants.pound)
print(constants.oz)
print(constants.ounce)
print(constants.stone)
print(constants.long_ton)
print(constants.short_ton)
print(constants.troy_ounce)
print(constants.troy_pound)
print(constants.carat)
print(constants.atomic_mass)
print(constants.m_u)
print(constants.u)

Output:

0.001
1000.0
6.479891e-05
0.45359236999999997
0.45359236999999997
0.028349523124999998
0.028349523124999998
6.3502931799999995
1016.0469088
907.1847399999999
0.031103476799999998
0.37324172159999996
0.0002
1.66053904e-27
1.66053904e-27
1.66053904e-27

Metric (SI) :

The specified unit is returned in meter.

from scipy import constants

print(constants.yotta)
print(constants.zetta)
print(constants.exa)
print(constants.peta)
print(constants.tera)
print(constants.giga)
print(constants.mega)
print(constants.kilo)
print(constants.hecto)
print(constants.deka)
print(constants.deci)
print(constants.centi)
print(constants.milli)
print(constants.micro)
print(constants.nano)
print(constants.pico)
print(constants.femto)
print(constants.atto)
print(constants.zepto)

Output:

1e+24
1e+21
1e+18
1000000000000000.0
1000000000000.0
1000000000.0
1000000.0
1000.0
100.0
10.0
0.1
0.01
0.001
1e-06
1e-09
1e-12
1e-15
1e-18
1e-21

Volume:

The specified unit is returned in cubic meters.

from scipy import constants


print(constants.liter)
print(constants.litre)
print(constants.gallon)
print(constants.gallon_US)
print(constants.gallon_imp)
print(constants.fluid_ounce)
print(constants.fluid_ounce_US)
print(constants.fluid_ounce_imp)
print(constants.barrel)
print(constants.bbl)

Output:

0.001
0.001
0.0037854117839999997
0.0037854117839999997
0.00454609
2.9573529562499998e-05
2.9573529562499998e-05
2.84130625e-05
0.15898729492799998
0.15898729492799998

Pressure:

The specified unit is returned in pascals.

from scipy import constants


print(constants.atm)
print(constants.atmosphere)
print(constants.bar)
print(constants.torr)
print(constants.mmHg)
print(constants.psi)

Output:

101325.0
101325.0
100000.0
133.32236842105263
133.32236842105263
6894.757293168361
101325.0
101325.0
100000.0
133.32236842105263
133.32236842105263
6894.757293168361

Energy:

The specified unit is returned in joules.

from scipy import constants
print(constants.eV)
print(constants.electron_volt)
print(constants.calorie)
print(constants.calorie_th)
print(constants.calorie_IT)
print(constants.erg)
print(constants.Btu)
print(constants.Btu_IT)
print(constants.Btu_th)
print(constants.ton_TNT)

Output:

1.6021766208e-19
1.6021766208e-19
4.184
4.184
4.1868
1e-07
1055.05585262
1055.05585262
1054.3502644888888
4184000000.0

Speed:

The specified unit is returned in meters per second.

from scipy import constants


print(constants.kmh)
print(constants.mph)
print(constants.mach)
print(constants.speed_of_sound)
print(constants.knot)

Output:

0.2777777777777778
0.44703999999999994
340.5
340.5
0.5144444444444445

Power:

The specified unit is returned in watts.

from scipy import constants


print(constants.hp)
print(constants.horsepower)

Output:

745.6998715822701
745.6998715822701

Angle:

The specified unit returned in radians.

from scipy import constants


print(constants.degree)
print(constants.arcmin)
print(constants.arcminute)
print(constants.arcsec)
print(constants.arcsecond)

Output:

0.017453292519943295
0.0002908882086657216
0.0002908882086657216
4.84813681109536e-06
4.84813681109536e-06

Time:

The specified unit is returned in seconds.

from scipy import constants


print(constants.minute)
print(constants.hour)
print(constants.day)
print(constants.week)
print(constants.year)
print(constants.Julian_year)

Output:

60.0
3600.0
86400.0
604800.0
31536000.0
31557600.0

Length:

The specified unit is returned in meters.

from scipy import constants


print(constants.inch)
print(constants.foot)
print(constants.yard)
print(constants.mile)
print(constants.mil)
print(constants.pt)
print(constants.point)
print(constants.survey_foot)
print(constants.survey_mile)
print(constants.nautical_mile)
print(constants.fermi)
print(constants.angstrom)
print(constants.micron)
print(constants.au)
print(constants.astronomical_unit)
print(constants.light_year)
print(constants.parsec)

Output:

0.0254
0.30479999999999996
0.9143999999999999
1609.3439999999998
2.5399999999999997e-05
0.00035277777777777776
0.00035277777777777776
0.3048006096012192
1609.3472186944373
1852.0
1e-15
1e-10
1e-06
149597870691.0
149597870691.0
9460730472580800.0
3.0856775813057292e+16

Temperature:

The specified unit is returned in Kelvin.

from scipy import constants


print(constants.zero_Celsius)    
print(constants.degree_Fahrenheit) 

Output:

273.15
0.5555555555555556

Force:

The specified unit is returned in newton.

from scipy import constants


print(constants.dyn)
print(constants.dyne)
print(constants.lbf)
print(constants.pound_force)
print(constants.kgf)
print(constants.kilogram_force)

Output:

1e-05
1e-05
4.4482216152605
4.4482216152605
9.80665
9.80665

Image Processing with SciPy 

scipy.ndimage is a submodule of SciPy which is for the most part utilized for operating out an image-related activity ."n" dimensional image leads to ndimage.

With the help of SciPy Image Processing, we can do Geometrics transformation(pivot, crop, flip), filtering of images(sharp and de nosing), show image, image division, arrangement, and highlights extraction, and the MISC Package in SciPy contains prebuilt images which can be utilized to perform image control task.

Sub packages in SciPy:

SciPy includes various sub-packages for different scientific operations which are shown below:

NameDescription
ClusterClustering algorithms
ConstantsPhysical and mathematical constants
FftpackFast Fourier Transform routines
IntegrateIntegration and ordinary differential equation solvers
InterpolateInterpolation and smoothing splines
IoInput and Output
LinalgLinear algebra
NdimageN-dimensional image processing
OdrOrthogonal distance regression
OptimizeOptimization and root-finding routines
SignalSignal processing
SparseSparse matrices and associated routines
SpatialSpatial data structures and algorithms
SpecialSpecial functions
StatsStatistical distributions and functions

Conclusion

In this article, we have learned about the SciPy constant package and learned about categories of units in Constant sub-packages.


Related Topics

AX Contour in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python e-book free download

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. There are many sources to learn python. In this...

3 minutes read.

Python sklearn train_test_split

Sklearn: One of the most beneficial open-source libraries for learning algorithms in Python is, without a doubt, Sklearn or Scikit-Learn. The most effective tools for statistical modeling and machine learning are all included in...

6 minutes read.

Python And Operator

Python's and operator takes two operands that can be either object, Boolean expressions, or both. And operator creates more complex expressions using those operands. Conditions are the common name for...

8 minutes read.

Python program to check whether a given number is prime or not

Python program to check whether a given number is prime or not A positive integer greater than 1 is called a prime number if it is divisible by one and number...

1 minute read.

Python Set clear() Method

Python Set clear() Method The set.clear() method removes all the elements from the set. Syntax set.clear() Parameter NA Return None Example 1 # Python program explaining # the set.clear() method # initializing the set fruits = {"watermelon", "banana", "apple"} # printing the set...

2 minutes read.

Python Encapsulation

What is meant by Encapsulation? The process of restricting access to the methods and variables so that accidental modification of data can be prevented is known as Encapsulation. The motive of Encapsulation:...

3 minutes read.

Python if statement

Python if statement Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as: If statementIf-else statementelif statement if statement The if statement is...

2 minutes read.

Python String capitalize() method

Python String capitalize() method The string.capitalize() method in Python returns a copy of the string with only its first character capitalized. Syntax string.capitalize() Parameter NA Return This function returns a string where the first character is upper...

1 minute read.

Python Examples

In this tutorial, we will see some examples related to python. This will include some basic example questions and their code in Python. Write a program to print “Hello Python”. print(‘Hello Python’) Output Hello...

5 minutes read.

Static Variables in Python

What is a Static Variable? The variable that remains with a constant value throughout the program or throughout the class is known as a " Static Variable ". Static variables are...

3 minutes read.

GUI to extract lyrics from a song Using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

4 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

GUI to Shut down, Restart and Logout from the PC using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

4 minutes read.

Python String expandtabs() method

Python String expandtabs() method The string.expandtabs() method in Python returns a copy of the string where all tab characters are expanded using spaces. Syntax String.expandtabs([tabsize]) Parameter tabsize: This parameter specifies the number of characters to...

1 minute read.

Tuple in Python

Tuples Tuples are the same as the list but as we know that lists are mutable means we can change the data from it. In the tuple we cannot change the...

6 minutes read.

Augmented reality in python

In this article, we shall learn about augmented reality in python. Augmented reality is a technology that captures the world around you and adds virtual content or objects on top of...

3 minutes read.

Polymorphism in Python

What is polymorphism and why is it important? Polymorphism's literal definition is the state of occurring in diverse shapes or forms. When it comes to programming, the idea of polymorphism is crucial....

3 minutes read.

Python PPTX

Python-pptx is a python library that enables the user to create and update PowerPoint (.pptx) presentations. It is used to create modified PowerPoint presentations from the db content that can...

10 minutes read.

Python Tricks: The Book

A Buffet of Awesome Python Features Dan Bader is the author of the book called Python Tricks . he is the owner and editor of Real Python and one of the...

3 minutes read.