What are the five common functions in SciPy?

Introduction

SciPy is an open-source Python-based library that helps in mathematics, engineering, technical, and scientific computing. SciPy packages are generally needed for scientific computing. The primary reason for using Scipy is its optimization for compute-intensive operations. It can work with NumPy arrays, as SciPy is built on top of NumPy. To install the package, you can run the command mentioned below:

pip install scipy
Installing scipy python library

Common functions

We will explore the five most commonly used functions from the SciPy package:

  • cbrt(): The cbrt() function gets the cube root of a number. It accepts one parameter, i.e., the number whose cube root needs to be determined. The parameter could be a single real number or a list of real numbers. It returns the cube root of the number.
  • exp10(): The exp10() function calculates the value of the expressions 10x, where x is the input parameter to the given function. The value of x could be any real number or a list containing the real numbers.
  • comb(): The comb() function calculates the value of the expression xCy. This is the calculation of the number of ways we can select the objects with no effect on the order of the objects. Mathematically, it is termed as combinations. This function accepts two parameters— the value of x and y— and returns the result of the combination.
  • perm(): The perm() function calculates xPy’s value. This is the calculation of the number of ways we can select y objects out of x. Here, the order of the objects matters. Mathematically, it is termed as permutations. This function accepts two parameters— the value of x and y— and returns the result of the permutation.
  • round(): The round() function returns the nearest possible integer from the given floating number. For example, if the input given is 1.9, the nearest integer would be 2. This function accepts a real number or a list containing the real numbers.

Code

Let’s explore how this works in the code:

import numpy
from scipy import special
#Create a numpy array
x = numpy.array([27,45])
#Get cube root
print(special.cbrt(x))
#Exponential function
print(special.exp10(x))
#Permutation and combination
print(special.comb(3,2)) #Equivalent to 3C2
print(special.perm(3,2)) #Equivalent to 3P2
#round function
print(special.round([1.9, -2.4, 1.2]))

Explanation

  • Lines 1 and 2: We import the required package.
  • Line 3: We create a numpy array.
  • Line 8: We call the cbrt() function to get the cube root of the numbers.
  • Line 11: We call the exp10() function to get the exponential value.
  • Lines 14 and 15: We call the comb() and the perm() function to calculate the combination and permutation values, respectively.
  • Line 18: We call the round() function to round off the values to the nearest integer.

Free Resources