NumPy Tutorial
NumPy is a core Python library for numerical computing, built for handling
large arrays and matrices efficiently. It is significantly faster than Python's
built-in lists because it uses optimized C language style storage where
actual values are stored at contiguous locations (not object reference). It
also supports vectorized computations.
• ndarray object: Stores homogeneous data in n-dimensional
arrays for fast processing.
• Vectorized operations: Perform element-wise calculations
without explicit loops.
• Broadcasting: Apply operations across arrays of different shapes.
• Linear algebra functions: Matrix multiplication, inversion,
eigenvalues, etc.
• Statistical tools: Mean, median, standard deviation, and more.
• Fourier transforms: Fast computation for signal and image
processing.
• Integration with other libraries: Works seamlessly with Pandas,
SciPy, and scikit-learn
What is NumPy Used for?
With NumPy, you can perform a wide range of numerical operations,
including:
• Creating and manipulating arrays.
• Performing element-wise and matrix operations.
• Generating random numbers and statistical calculations.
• Conducting linear algebra operations.
• Working with Fourier transformations.
• Handling missing values efficiently in datasets.
Why Learn NumPy?
• NumPy speeds up math operations like addition and
multiplication on large groups of numbers compared to regular
Python..
• It’s good for handling large lists of numbers (arrays), so you don’t
have to write complicated loops.
• It gives ready-to-use functions for statistics, algebra and random
numbers.
• Libraries like Pandas, SciPy, TensorFlow and many others are
built on top of NumPy.
• NumPy uses less memory and stores data more efficiently, which
matters when working with lots of data.
How to Install Numpy on Windows?
On Windows, it can be installed easily using either pip or conda,
You can install NumPy using one of the following package managers:
• pip (Python Package Installer)
• conda (Anaconda / Miniconda)
1. Installing Numpy For PIP Users
Ensure that Python and pip are already installed and added to PATH.
Then run the following command in Command Prompt or PowerShell:
pip install numpy
You will get a similar message once the installation is complete:
Create a NumPy ndarray Object
NumPy is used to work with arrays. The array object in NumPy is called ndarray.
We can create a NumPy ndarray object by using the array() function.
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
[1 2 3 4 5]
<class '[Link]'>
Example
Use a tuple to create a NumPy array:
import numpy as np
arr = [Link]((1, 2, 3, 4, 5))
print(arr)
[1 2 3 4 5]
NumPy Array Indexing
Access Array Elements
Array indexing is the same as accessing an array element.
You can access an array element by referring to its index number.
The indexes in NumPy arrays start with 0, meaning that the first element has index 0,
and the second has index 1 etc.
Example
Get the first element from the following array:
import numpy as np
arr = [Link]([1, 2, 3, 4])
print(arr[0])
1
Example
Get the second element from the following array.
import numpy as np
arr = [Link]([1, 2, 3, 4])
print(arr[1])
2
Example
Get third and fourth elements from the following array and add them.
import numpy as np
arr = [Link]([1, 2, 3, 4])
print(arr[2] + arr[3])
7
NumPy Joining Array
Joining NumPy Arrays
Joining means putting contents of two or more arrays in a single array.
In SQL we join tables based on a key, whereas in NumPy we join arrays by axes.
We pass a sequence of arrays that we want to join to the concatenate() function
Example
Join two arrays
import numpy as np
arr1 = [Link]([1, 2, 3])
arr2 = [Link]([4, 5, 6])
arr = [Link]((arr1, arr2))
print(arr)
[1 2 3 4 5 6]
NumPy Searching Arrays
Searching Arrays
You can search an array for a certain value, and return the indexes that get a match.
To search an array, use the where() method.
Example
Find the indexes where the value is 4:
import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 4, 4])
x = [Link](arr == 4)
print(x)
(array([3, 5, 6]),)
NumPy Sorting Arrays
Sorting Arrays
Sorting means putting elements in an ordered sequence.
Ordered sequence is any sequence that has an order corresponding to elements, like
numeric or alphabetical, ascending or descending.
The NumPy ndarray object has a function called sort(), that will sort a specified
array.
Example
Sort the array:
import numpy as np
arr = [Link]([3, 2, 0, 1])
print([Link](arr))
[0 1 2 3]