0% found this document useful (0 votes)
12 views38 pages

09 20241101 NumPy

The document provides an overview of NumPy, a core library in Python for numerical and scientific computing, emphasizing its efficient handling of multidimensional arrays and vectorized computations. It covers key topics such as array creation, indexing, slicing, vectorized operations, data processing, loading and saving data, linear algebra operations, and random number generation. The content is aimed at enhancing understanding and application of NumPy in data analysis and machine learning.

Uploaded by

scs623170
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views38 pages

09 20241101 NumPy

The document provides an overview of NumPy, a core library in Python for numerical and scientific computing, emphasizing its efficient handling of multidimensional arrays and vectorized computations. It covers key topics such as array creation, indexing, slicing, vectorized operations, data processing, loading and saving data, linear algebra operations, and random number generation. The content is aimed at enhancing understanding and application of NumPy in data analysis and machine learning.

Uploaded by

scs623170
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

NumPy Arrays and Vectorized

Computations

Prof. Murali Krishna Gurram


Dept. of Geo-Engineering & RDT
Centre for Remote Sensing, AUCE
Andhra University, Visakhapatnam – 530 003
Dt. 01/11/2024
Basic Elements of Python

NumPy arrays and Vectorized Computations


a. NumPy arrays
b. Array creation
c. Indexing and slicing
d. Fancy Indexing
e. Numerical operations on arrays
f. Array functions
g. Data processing using arrays
h. Loading and saving data
i. Saving an array
j. Loading an array
k. Linear algebra with NumPy
l. NumPy random numbers
NumPy Overview
Object-Oriented Programming (OOP) Principles in Python
 NumPy (short for Numerical Python) is a core library in Python for
numerical and scientific computing.

 NumPy is particularly designed for high-performance


multidimensional array objects and tools to manipulate these arrays.

 Unlike traditional Python lists, NumPy arrays are stored in contiguous


memory blocks, making it more memory-efficient and faster to access
and manipulate.

 This efficiency in both storage and computation is what allows NumPy


to serve as the backbone for numerous data analysis, machine
learning, and scientific computing applications in Python.
1. NumPy Arrays in Python
NumPy Arrays in Python
What is an Array in NumPy?
 An array in NumPy is a grid of values that are all of the same data
type, indexed by a tuple of nonnegative integers.

 In other words, arrays are collections of data elements, just like lists in
Python.

 Arrays in NumPy are designed for homogeneous data (same data type
across all elements) and are much more efficient for handling large
volumes of data.
NumPy Arrays in Python
What is an Array in NumPy?
NumPy arrays are n-dimensional.
Arrays can be…
 Single-dimensional (1D), like a vector,
 Two-dimensional (2D), like a matrix, and
 Multi-dimensional (3D and beyond)

 This flexibility is important for operations on data in fields such as


machine learning and image processing, where data is often
organized in high-dimensional structures.
NumPy Arrays in Python
Types of Array Creation
Creating arrays efficiently is essential in NumPy. There are
several ways to create arrays:
1. From Lists:
– Any Python list or nested list can be converted into a NumPy
array using np.array().
– np.array() method is often the simplest and most intuitive
way to create a small array.

Example
import numpy as np
array_from_list = np.array([1, 2, 3, 4, 5])
NumPy Arrays in Python
Types of Array Creation
2. Predefined Arrays:
– These are methods to initialize arrays with predefined values
for faster array creation.
– Zeros and Ones: Use np.zeros() or np.ones() to initialize
arrays with zeros or ones, useful for placeholders or setting
default states.
– Empty Arrays: Created with np.empty(); these arrays are
allocated without initialization, leaving values uninitialized,
which is sometimes useful when performance is critical.

Example
zeros_array = np.zeros((3, 3))
ones_array = np.ones((2, 4))
empty_array = np.empty((2, 3))
NumPy Arrays in Python
Types of Array Creation
3. Arrays with Ranges:
– Often used in mathematical and scientific contexts to quickly
create sequences.

– np.arange(start, stop, step): Similar to Python’s built-in


range(), but returns a NumPy array.

– np.linspace(start, stop, num): Divides a specified interval


evenly into num parts, useful for mathematical plotting.
Example
range_array = np.arange(0, 10, 2)
linspace_array = np.linspace(0, 1, 5)
NumPy Arrays in Python
Types of Array Creation
4. Randomized Arrays:
– Random numbers are essential for simulations, data
augmentation, and initializing model weights in machine
learning.
– np.random.rand(d0, d1, …, dn): Generates an array of the
given shape with values sampled from a uniform distribution
over [0, 1).
– np.random.randn(d0, d1, …, dn): Generates samples from a
standard normal (Gaussian) distribution.
Example
random_uniform = np.random.rand(2, 3)
random_normal = np.random.randn(2, 3)
2. Indexing and Slicing
2. Indexing and Slicing
Why is Array Indexing Important?
– Indexing is essential in NumPy because it allows efficient
retrieval and modification of data within an array, which is
critical for fast data processing.

– In NumPy, slicing (retrieving a subset of an array) doesn’t


create a new copy of the data but rather a view, meaning it
refers to the original data.

– A View is a memory-efficient approach for working with


subsets of large datasets.
2. Indexing and Slicing
Indexing and Slicing Techniques
1. 1D Array Indexing and Slicing:
• Use regular indexing similar to lists. Negative indexing
is supported, allowing access from the end of the array.

Example
arr = np.array([10, 20, 30, 40, 50])
print(arr[1]) # Output: 20
print(arr[-1]) # Output: 50
print(arr[1:4]) # Slice from index 1 to 3, Output: [20 30 40]
2. Indexing and Slicing
Indexing and Slicing Techniques
2. 2D Array (Matrix) Indexing::
• 2D arrays can be accessed using a pair of indices [row,
column].

Example
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix[1, 2]) # Output: 6
print(matrix[1:, :2]) # Rows 1 and onwards, first two columns
2. Indexing and Slicing
Indexing and Slicing Techniques
3. Boolean Indexing:
• This technique uses a Boolean array (of the same
shape) to filter elements.

Example
data = np.array([10, 20, 30, 40, 50])
filtered_data = data[data > 25] # Select elements greater than 25
2. Indexing and Slicing
Indexing and Slicing Techniques
4. Fancy Indexing:
• Fancy indexing allows you to select multiple elements
from an array at once by specifying a list or array of
indices.

Example
arr = np.array([0, 10, 20, 30, 40, 50])
indices = [1, 3, 4]
selected_elements = arr[indices] # Select elements at indices 1, 3, and 4

print("Selected elements:", selected_elements)

# Output: [10 30 40]


2. Indexing and Slicing
Indexing and Slicing Techniques
4b. Fancy Indexing in Multi-dimensional Arrays
• Fancy indexing also works in multi-dimensional arrays.

Example
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
row_indices = np.array([0, 1, 2])
col_indices = np.array([2, 1, 0])
selected_elements = matrix[row_indices, col_indices] # Select elements (0,2),
(1,1), (2,0)
print("Selected elements from matrix:", selected_elements)
3. Vectorized Operations
3. Vectorized Operations
What is Vectorization?
• Vectorization is a process that allows operations to be
performed on entire arrays or subarrays without explicit loops,
making computation faster and more concise.

• It leverages low-level optimizations, such as SIMD (Single


Instruction, Multiple Data) operations, meaning one instruction
can operate on multiple data points at once.
3. Vectorized Operations
Benefits of Vectorized Computations:

• Speed: Operations are performed directly on arrays, avoiding


Python loops.

• Conciseness: Code is shorter and more expressive.

• Memory Efficiency: Vectorized operations often avoid


temporary arrays, saving memory.
Examples of Vectorized Operations
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print("Addition:", a + b) # Output: [5 7 9]
print("Multiplication:", a * b) # Output: [4 10 18]
print("Square:", a ** 2) # Output: [1 4 9]
3. Vectorized Operations
Numerical Operations on Arrays:

• NumPy arrays support a wide range of arithmetic and


mathematical operations.
Basic Arithmetic Operations
• Arithmetic operations such as addition, subtraction,
multiplication, and division can be performed on arrays in an
element-wise manner.
• .Example
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print("Addition:", a + b)
print("Multiplication:", a * b)
print("Square each element:", a ** 2)
3. Vectorized Operations
Array Functions
• NumPy provides numerous functions for operations on arrays,
including trigonometric, exponential, and logarithmic functions.

• These mathematical functions that operate element-wise,


enabling us to apply functions like sqrt, exp, sin, and others to
arrays directly.
Example
print("Square root:", np.sqrt(a))
print("Exponential:", np.exp(a))
print("Logarithm:", np.log(b))
print("Sine:", np.sin(a))
4. Data Processing Using Arrays
4. Data Processing Using Arrays
Aggregation and Data Filtering
• NumPy arrays are useful for filtering data and performing
aggregate operations, which is often required in data analysis.

Boolean Indexing
• Boolean indexing is a way to filter elements in an array based on
conditions.

Example
data = np.array([10, 20, 30, 40, 50])
filtered_data = data[data > 25] # Select elements greater than 25
print("Filtered data:", filtered_data)
4. Data Processing Using Arrays
Aggregation and Data Filtering
• Aggregation (like sum, mean, min, max) is essential for
analyzing data.

• NumPy provides optimized aggregation functions that operate


over entire arrays or specified axes, making data summarization
quick and memory-efficient.
Example
data = np.array([10, 20, 30, 40, 50])
print("Sum:", np.sum(data))
print("Mean:", np.mean(data))
print("Min:", np.min(data))
print("Max:", np.max(data))
print("Standard Deviation:", np.std(data))
5. Loading and Saving Data
5. Loading and Saving Data
 NumPy allows saving and loading data, both in binary (NumPy’s
.npy format) and text formats.

Saving Data
• Binary Format: Saves arrays in a highly efficient binary format
that preserves data types.
• Text Format: Saves arrays in text format, suitable for human-
readable files.
Example
np.save("data_array.npy", data) # Save in binary format
np.savetxt("data_array.txt", data) # Save in text format
5. Loading and Saving Data
Loading Data
• To load data back into an array, use np.load for binary files or
np.loadtxt for text files.

Example
loaded_data = np.load("data_array.npy")
loaded_text_data = np.loadtxt("data_array.txt")
print("Loaded binary data:", loaded_data)
print("Loaded text data:", loaded_text_data)
6. Linear Algebra with NumPy
6. Linear Algebra with NumPy
Matrix and Linear Algebra Operations
• NumPy has extensive linear algebra support, including matrix
multiplication, determinants, transposes and solving linear
systems.
Matrix Multiplication
• For matrix multiplication, use np.dot or the @ operator.
Example
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print("Matrix Multiplication:\n", np.dot(A, B))
6. Linear Algebra with NumPy
Matrix and Linear Algebra Operations
Transpose and Determinant
• Transpose: Flips rows and columns.
• Determinant: Calculated using np.linalg.det.

Example
print("Transpose of A:\n", A.T)
print("Determinant of A:", np.linalg.det(A))
6. Linear Algebra with NumPy
Matrix and Linear Algebra Operations
Solving Linear Equations
• For equations of the form Ax = B, for x using np.linalg.solve.

Example
B = np.array([1, 0])
x = np.linalg.solve(A, B)
print("Solution x:", x)
6. Linear Algebra with NumPy
Matrix and Linear Algebra Operations
• Linear algebra is foundational in areas like machine learning and
engineering.
• NumPy provides matrix manipulation operations and functions
for solving linear equations, which form the basis for many
machine learning algorithms.
Example
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print("Matrix Multiplication:\n", np.dot(A, B))
print("Transpose of A:\n", A.T)
print("Determinant of A:", np.linalg.det(A))

# Solving a linear equation


B = np.array([1, 0])
x = np.linalg.solve(A, B)
print("Solution x:", x)
7. NumPy Random Numbers
7. NumPy Random Numbers
NumPy's random module provides functions to generate random
numbers for simulations, sampling, and other purposes.

Random Number Generation


• Uniform Distribution: Generates random numbers between 0
and 1.

• Normal Distribution: Generates numbers from a normal


(Gaussian) distribution.
7. NumPy Random Numbers
Random Number Generation

Example
# Uniformly distributed random values between 0 and 1
rand_array = np.random.rand(2, 3)
print("Uniform random values:\n", rand_array)

# Normally distributed random values (mean 0, variance 1)


normal_array = np.random.randn(2, 3)
print("Normal random values:\n", normal_array)
7. NumPy Random Numbers
Random Integers and Permutations

Example
# Random integers in a specified range
random_integers = np.random.randint(1, 10, (2, 3))
print("Random integers:\n", random_integers)

# Random permutation of a sequence


seq = np.array([1, 2, 3, 4, 5])
shuffled_seq = np.random.permutation(seq)
print("Shuffled sequence:", shuffled_seq)
Q&A

You might also like