Chapter-4
NumPy Arrays and Vectored Computation
Contents
•NumPy arrays
•Array creation
•Indexing and slicing
•Fancy indexing
•Numerical operations on arrays
• Array functions
•Data processing using arrays
• Loading and saving data
•Saving an array
•Loading an array
• Linear algebra with NumPy
•NumPy random number
NumPy
• NumPy stands for Numerical Python, is an open-source Python library
that provides support for large, multi-dimensional arrays and
matrices.
• It also have a collection of high-level mathematical functions to
operate on arrays.
How to import NumPy
After installing NumPy, it may be imported into Python code like:
import numpy as np
NumPy
Why use NumPy?
• Python lists are excellent, general-purpose containers. They can be
“heterogeneous”, meaning that they can contain elements of a variety of
types, and they are quite fast when used to perform individual operations
on a handful of elements.
• Depending on the characteristics of the data and the types of operations
that need to be performed, other containers may be more appropriate; by
exploiting these characteristics, we can improve speed, reduce memory
consumption, and offer a high-level syntax for performing a variety of
common processing tasks.
• NumPy shines when there are large quantities of “homogeneous”
(same-type) data to be processed on the CPU.
Arrays in NumPy
• NumPy’s main object is the homogeneous multidimensional array.s
• It is a table of elements (usually numbers), all of the same type,
indexed by a tuple of positive integers.
• In NumPy, dimensions are called axes. The number of axes is rank.
• NumPy’s array class is called ndarray. It is also known by the alias
array.
NOTE
As with built-in Python sequences, NumPy arrays are “0-indexed”: the
first element of the array is accessed using index 0, not 1.
Arrays in NumPy
Array attributes
Example
• we are creating a two-dimensional array that has the rank of 2 as it has 2 axes.
• The first axis(dimension) is of length 2, i.e., the number of rows, and the second
axis(dimension) is of length 3, i.e., the number of columns. The overall shape of
the array can be represented as (2, 3)
Adding, removing, and sorting elements
In order to remove elements from an
array, it’s simple to use indexing to select
the elements that you want to keep.
How do you know the shape and size of an
array?
This structure has 3 groups, each containing 2
rows of 4 elements.
NumPy Array Creation
Create NumPy Array with List and Tuple
• You can create an array from a regular Python list or tuple using the
array() function.
NumPy Array Creation
Create Array of Fixed Size
• Often, the element is of an array is originally unknown, but its size is known. Hence, NumPy offers
several functions to create arrays with initial placeholder content.
• This minimize the necessity of growing arrays, an expensive operation. For example: [Link],
[Link], [Link], [Link], etc.
• To create sequences of numbers, NumPy provides a function analogous to the range that returns
arrays instead of lists.
NumPy Array Indexing
NumPy in Python offers many ways to do array indexing.
• Slicing: Just like lists in Python, NumPy arrays can be sliced. As arrays
can be multidimensional, you need to specify a slice for each
dimension of the array.
• Integer array indexing: In this method, lists are passed for indexing
for each dimension. One-to-one mapping of corresponding elements
is done to construct a new arbitrary array.
• Boolean array indexing: This method is used when we want to pick
elements from the array which satisfy some condition.
NumPy Array Indexing
Here, temp = arr[[0, 1, 2, 3], [3, 2, 1, 0]] uses two
lists of indices to access specific elements in the
array
Fancy Indexing
Fancy indexing in NumPy refers to accessing multiple array elements using arrays of
indices, rather than simple slicing. This allows for complex indexing patterns that
aren’t possible with regular slicing, and it’s often used to extract specific elements
from an array based on a list of index positions.
Numerical Operations on Arrays
Array Functions
• NumPy provides a wide range of functions to perform operations on
arrays, from simple calculations to complex transformations.
Here are some of the most commonly used array functions in NumPy:
Array Functions
Data Processing using arrays
• Data processing with NumPy arrays involves using various functions and operations to clean,
transform, analyze, and manipulate data. NumPy provides efficient tools for handling large
datasets, which makes it widely used in data science, machine learning, and scientific
computing.
Here are some common data processing techniques using NumPy arrays:
• Data Cleaning
• Data Transformation
• Aggregations
• Sorting and Searching
• Filtering and Conditional Selection
• Vectorized Operations
• Handling Multidimensional Data
• Concatenation and Stacking
• Applying Custom Functions
• Saving and Loading Data
Data Cleaning
Data Transformation
Aggregations
Sorting and Searching
Filtering and Conditional Selection &
Vectorized Operations
Handling Multidimensional Data &
Concatenation and Stacking
Applying Custom Functions & Saving and
Loading Data
Linear algebra with NumPy
NumPy random numbers
• NumPy offers a variety of functions for generating random numbers,
which are useful for simulations, data sampling, and random
experiments. These functions are available in the [Link]
module.
NumPy random numbers
NumPy random numbers
NumPy random numbers
NumPy random numbers
NumPy random numbers