Sling Academy
Home/NumPy/How to Integrate NumPy with SciPy for Scientific Computing

How to Integrate NumPy with SciPy for Scientific Computing

Last updated: January 23, 2024

Introduction

When it comes to scientific computing in Python, two of the mainstays in the field are NumPy and SciPy. NumPy is the fundamental package for scientific computing with Python, offering powerful N-dimensional arrays and various derived objects such as masked arrays and matrices. SciPy builds on NumPy by offering a collection of mathematical algorithms and convenience functions. Together, they form a powerful toolkit for numerical computations.

This tutorial will guide you through integrating NumPy with SciPy for scientific computing, taking you from the basics to more advanced applications.

Setting Up Your Environment

Before diving in, let’s make sure you have both NumPy and SciPy installed in your Python environment. You can install them using pip:

pip install numpy scipy

After installation, you can import them as follows:

import numpy as np
import scipy

Basic Operations with NumPy

Let’s start by creating some basic NumPy arrays and performing elementary operations:

arr = np.array([1, 2, 3, 4, 5])
print(arr)
# Output: [1 2 3 4 5]

NumPy arrays are versatile and can be manipulated easily. Below is an example of element-wise addition:

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = arr1 + arr2
print(result)
# Output: [5 7 9]

Advanced Array Operations

Moving to more advanced array operations, we can do matrix multiplication using NumPy:

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
product = np.dot(a, b)
print(product)
# Output: [[19 22]
#          [43 50]]

NumPy also offers functions for more complex operations like eigenvalue decomposition:

values, vectors = np.linalg.eig(a)
print(values)
print(vectors)
# Output for values: [-0.37228132  5.37228132]
# Output for vectors:
# [[-0.82456484 -0.41597356]
#  [ 0.56576746 -0.90937671]]

Integrating NumPy with SciPy

Now, let’s bring SciPy into play. Suppose we wish to integrate a mathematical function; SciPy’s integrate sub-module offers several integration techniques:

from scipy import integrate
result, error = integrate.quad(lambda x: x ** 2, 0, 1)
print(result)
# Output: 0.33333333333333337

SciPy can also handle differential equations. Here is an example of solving an ordinary differential equation (ODE) using SciPy’s odeint function:

from scipy.integrate import odeint
def model(y, t):
    k = 0.3
    dydt = -k * y
    return dydt
y0 = 5
t = np.linspace(0, 20, 100)
result = odeint(model, y0, t)
print(result[:5])
# Output: [[5.        ]
#          [4.49058581]
#          [4.02351012]
#          [3.59480666]
#          [3.20105785]]

Now, combining NumPy with SciPy, we can unpack results and perform further array operations. For instance, we could integrate a signal acquired as NumPy array:

signal = np.cos(np.arange(100))
cumulative = integrate.cumtrapz(signal, initial=0)
print(cumulative[:5])
# Output: [ 0.          1.          1.54030231  1.25758427  0.34922325]

Conclusion

In this tutorial, we explored the synergy between NumPy and SciPy. Starting with NumPy’s fundamental array operations, advancing to sophisticated matrix computations, we saw how SciPy supplements these with higher-level mathematical functions. These tools, together, create a powerful combination for scientific computing in Python.

Next Article: How to Use NumPy with Jupyter Notebook for Interactive Analysis

Previous Article: How to Use NumPy for Geospatial Data Analysis

Series: NumPy Intermediate & Advanced Tutorials

NumPy

You May Also Like

  • SciPy linalg.solve_triangular() function (3 examples)
  • SciPy linalg.solve_circulant() function (4 examples)
  • SciPy – Using linalg.solveh_banded() function
  • SciPy: Using linalg.solve_banded() function (3 examples)
  • SciPy linalg.solve() function (4 examples)
  • SciPy – Using linalg.inv() function (4 examples)
  • SciPy io.arff.loadarff() function (4 examples)
  • SciPy io.wavfile.read() function (4 examples)
  • SciPy io.hb_write() function (4 examples)
  • Using SciPy’s io.hb_read() function (3 examples)
  • SciPy io.mmwrite() function (4 examples)
  • SciPy – Exploring io.mmread() function (4 examples)
  • Using io.mminfo() function in SciPy (3 examples)
  • SciPy – Working with io.readsav() function (4 examples)
  • Understanding io.whosmat() function in SciPy (3 examples)
  • SciPy: Using io.savemat() function (4 examples)
  • Using io.loadmat() function in SciPy (4 examples)
  • SciPy: Using interpolate.bisplev() function (3 examples)
  • SciPy: Using interpolate.insert() function (4 examples)