What is NumPy
NumPy (short for numerical Python) is the library package in Python for high perdormance scientific computation in Python. It is a powerful Python library for computing numbers and analyzing data.
NumPy is widely used for scientific computation in Physics, Chemistry, Biology, Astronomy, data analysis, machine learning, Natural Language Processing, and many more disciplines.
Why Use NumPy for Scientific Computation
Python provides lists that are good, general-purpose containers of data. Python lists can be used to build arrays; they can contain heterogeneous data (elements of different data types). Python lists are quite fast when used to perform individual operations on a handful of elements. However, these programs are slow for large amount of data.
Vectorization
NumPy implements vectorization, which produces programs that are free of any explicit looping, indexing, and iterations. These things are taking place “behind the scenes” in optimized, pre-compiled C-code.
Vectorization applies the operations on the whole array rather using loops and indexing in the code to cycle through the entire array. This technique makes the numerical computations that are more efficient, easier to write, and understand.
Broadcasting
Broadcasting is a term that is used to describe the implicit element-by-element behavior of operations. In NumPy, element-by-element operations are the “default mode”.
Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python. It does this without making needless copies of data and usually leads to efficient algorithm implementations.
NumPy operations are usually done on pairs of arrays on an element-by-element basis. In the simplest case of the two arrays with exactly the same shape shown in the following example:
import numpy as np a = np.array([1.0, 2.0, 3.0]) b = np.array([2.0, 2.0, 2.0]) a * b array([2., 4., 6.])
Notice the elment-by-element operation.
The broadcasting example below shows a case when an array and a scalar value are combined in an operation:
import numpy as np
a = np.array([1.0, 2.0, 3.0])
b = 2.0
a * b
array([2., 4., 6,])
You can see the scalar “b”multiplied each element of array “a”.
See the below link for more insight into broadcasting operartion.
https://numpy.org/doc/stable/user/basics.broadcasting.html
Vectorization and Broadcasting techniques make the NumPy programs that are:
- Easy to read and understand code: The programs are shorter and the operations are expressed in mathematical terms.
- High in performance: The programs are significantly faster than code using explicit loops
- Efficient in memory usage: NumPy programs are generally more memory usage efficient.
Let us look at the program example below to clarify these concepts.
As a simple example, consider the case of multiplying each element in a 1-D sequence by the corresponding element in another 1-D sequence of the same length. The data are stored in two Python lists, named “a” and “b”. The Python program multiplies the two lists by iterating over each element of the two lists.
The program also does the multiplication of two 1-D arrays using NumPy.
PROGRAM EXAMPLE: Why Use NumPy
# Program Name : why_use_numpy.py
# Python List multiplcation
a = [2, 3, 4, 7]
b = [4, 3, 2, 8]
# Multiply list1 and list2
c = [] # Empty list
for i in range(len(a)):
c.append(a[i] * b[i])
print(" roduct of a and b lists using Python is: \n", c)
print()
#
# Now Use NumPy for the same multiplication
import numpy as np
a = np.array([2, 3, 4, 7])
b = np.array([4, 3, 2, 8])
c_1 = a * b
print("Product of a and b arrays using NumPy is : \n", c_1)
The following is the program’s output.
Product of a and b lists using Python is:
[8, 9, 8, 56]
Product of a and b arrays using NumPy is :
[ 8 9 8 56]
Note that the NumPy program is simpler and intuitive because its uses of vectorization and broadcasting.
ndarrays
NumPy incorporates an array object, called ndarray, with lots of supporting functions that make the programs much faster and easier to use, especially in scientific computation. NumPy programs using ndarrays are efficient in that they are fast and they consume less memory. When there are large quantities of “homogeneous” (same type) data) to be processed, NumPy is an excellent choice for scientific computation.
For reference, click on the list below.
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray
NumPy Import
All NumPy programs must start by importing NumPy using the following statement.
>>> import numpy as np.
The following pages review the basics of NumPy package. We start with the introduction to Arrays.
Copyright © 2026 softwareprogramming4kids.com
All Rights Reserved