Introduction to NumPy for Numerical Computing
Programming languages are tools that help us manipulate and analyze data. The Python programming language has become increasingly popular in different sectors such as physics, engineering, computer science, etc., and is extensively used for numerical computation, statistics, and data analysis. One reason for its strong position in these fields resides in the third-party package, NumPy (Numerical Python).

What is NumPy?
NumPy is at the center of Python’s scientific stack. It’s one of the fundamental packages that you need to know when working with data in Python. NumPy provides an n-dimensional array object and a set of array-processing functions that offer a fast operation on arrays, including mathematical, logical, shape manipulation, sorting, etc. However, before you move on to learn about this powerful tool, let’s review why you would use arrays instead of lists in Python.
Lists vs Arrays
Python lists are very flexible, they can contain any arbitrary objects, and they can be extended and reduced dynamically. However, that flexibility comes at the expense of the speed of operations. When you are going to perform mathematical operations on large arrays, Python lists fall short of providing a satisfactory speed and efficiency.
On the other hand, NumPy arrays, which are densely packed arrays of a homogeneous type, perform complex mathematical operations much faster than Python lists. This is due to effective cache utilization in NumPy arrays as the elements of the array are close to each other in memory.
Now that we know why we’d want to use NumPy arrays, it’s time to dive in deeper.
Working With NumPy
To use the NumPy package, you first need to install it. The fastest way to install NumPy is using pip
:
pip install numpy
Once installed, you can import it into your Python scripts with:
import numpy as np
The np
alias is a convention followed by the Python community to save typing numpy
every time you want to use the package.
Creating NumPy Arrays
There are a few ways to create NumPy arrays.
import numpy as np
# One-dimensional array
a = np.array([1, 2, 3])
print(a)
#Two-dimensional array
b = np.array([(1.5, 2, 3), (4, 5, 6)])
print(b)
In the above code, np.array
function is used to create an array. This function takes a list or a tuple of elements and converts that into an array.
Database Operations with NumPy
A key feature of the NumPy module is to perform database-type operations on datasets, distinctively comparable to what you’d do on SQL.
import numpy as np
# Selecting data
a = np.array([1, 2, 3])
print(a[a < 2])
# Data aggregation
b = np.array([(1.5, 2, 3), (4, 5, 6)])
print(np.mean(b))
# Joining datasets
c = np.array([4, 5, 6])
print(np.concatenate((a, c)))
Mathematical Operations in NumPy
NumPy offers a great way to perform mathematical operations on arrays.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# Addition
print(a + b)
# Subtraction
print(a - b)
# Multiplication
print(a * b)
# Division
print(a / b)
Why NumPy is Faster Than Lists?
NumPy arrays are densely packed arrays with a homogeneous type, while Python lists can contain elements of different data types. Hence, you get the simplicity of programming with the speed of C. How?
- You get a fast operation on an entire array without writing a loop in Python which can be slow.
- The operations in NumPy are implemented in C, providing a speed that is faster than if you were working with Python directly.
- Under the hood, it stores the fixed-type items laid out in contiguous memory storage. So, you have less space to travel in memory, which results in better runtime.
Conclusion
With this, you should have a good starting point to begin your journey with NumPy. Take the time to practice the examples and experiment with your code. Jupyter notebook offers a great way to work with NumPy as you can create different cells to run your code. Good luck, and enjoy your coding journey with Python and NumPy!