0% found this document useful (0 votes)
13 views52 pages

Understanding NumPy for Python Computing

Uploaded by

bukyarajpal
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)
13 views52 pages

Understanding NumPy for Python Computing

Uploaded by

bukyarajpal
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

Numpy

Numerical Computing in Python

Compiled by
Dr. P. Venkateswara Rao
Associate Professor
1
What is Numpy?
• Numpy, Scipy, and Matplotlib provide MATLAB-like functionality in
python.
• Numpy Features:
• Typed multidimentional arrays (matrices)
• Fast numerical computations (matrix math)
• High-level math functions

Source: Jake VanderPlas’s Python Data Science Handbook

2
NumPy
• Stands for Numerical Python
• It is a fundamental package required for high performance computing
and data analysis
• NumPy is so important for numerical computations in Python is because
it is designed for efficiency on large arrays of data.
• It provides
• ndarray for creating multiple dimensional arrays
• Internally stores data in a contiguous block of memory, independent of other
built-in Python objects, use much less memory than built-in Python sequences.
• Standard math functions for fast operations on entire arrays of data without
having to write loops
• NumPy Arrays are important because they enable you to express batch
operations on data without writing any for loops. We call this vectorization.
NumPy ndarray vs list
• One of the key features of NumPy is its N-dimensional array
object, or ndarray, which is a fast, flexible container for large
datasets in Python.
• Whenever you see “array,” “NumPy array,” or “ndarray” in the
text, with few exceptions they all refer to the same thing: the
ndarray object.
• NumPy-based algorithms are generally 10 to 100 times faster (or
more) than their pure Python counterparts and use significantly
less memory.
Why do we need NumPy
import time

n_nums = 10000000
# Using a Python list
lst = list(range(n_nums)) Python list computation time: 2.64 seconds
NumPy array computation time: 0.05 seconds
start = [Link]()
lst_squared = [x**2 for x in lst]
end = [Link]()
print(f"Python list computation time: {end - start: .2f} seconds")

# Using a NumPy array


arr = [Link](n_nums)
start = [Link]()
arr_squared = arr ** 2
end = [Link]()
print(f"NumPy array computation time: {end - start: .2f} seconds")

5
Numpy Array Vs Python List

6
Import & Version Check
import numpy as np
print("Using NumPy version:", np.__version__)

Using NumPy version: 1.26.4

from numpy import pi, sin

from numpy import *

7
NumPy Overview
1. Arrays
2. Shaping and transposition
3. Mathematical Operations
4. Indexing and slicing
5. Broadcasting

8
Arrays
Structured lists of numbers.
• Vectors
• Matrices
• Images
• Tensors
• ConvNets

9
Arrays
Structured lists of numbers.
𝑝
• Vectors
𝑝
• Matrices
𝑝
• Images
• Tensors 𝑎 ⋯ 𝑎
• ConvNets ⋮ ⋱ ⋮
𝑎 ⋯ 𝑎

10
Arrays
Structured lists of numbers.
• Vectors
• Matrices
• Images
• Tensors
• ConvNets

11
Arrays
Structured lists of numbers.
• Vectors
• Matrices
• Images
• Tensors
• ConvNets

12
Arrays
Structured lists of numbers.
• Vectors
• Matrices
• Images
• Tensors
• ConvNets

13
Matrix
• Represented as: [Link] with 2 dimensions (rows × columns).
• python
• matrix = [Link]([[1, 2], [3, 4]])

• Concept:
• A matrix is a 2D grid of numbers (a special case of a tensor).
• Common in linear algebra.

• Applications:
• Solving systems of equations.
• Linear transformations.
• Used in fully connected (dense) layers of neural networks.
• Representing adjacency in graphs.

14
Images
• Grayscale Image: 2D array (height × width).

• Color Image (RGB): 3D array (height × width × 3).

• python
• # Grayscale: shape (H, W)
• # RGB: shape (H, W, 3)
• Concept:
• Digital image as a grid of pixel values.
• Each pixel can be a single intensity (grayscale) or a tuple of RGB values.

• Applications:
• Computer vision tasks: classification, object detection, segmentation.
• Input to CNNs.
• Image preprocessing and augmentation.

15
Tensors
• A generalization of scalars (0D), vectors (1D), matrices (2D), to N-D arrays.

• tensor = [Link](10, 3, 28, 28) # e.g., batch of 10 RGB images


• Concept:
• A tensor is a container for data in multiple dimensions.
• Think of it as a general-purpose multidimensional array.

• Applications:
• Used throughout machine learning and deep learning.

• Represent:
• Input data (e.g., images, text).
• Weights in neural networks.
• Intermediate features.

16
ConvNets (Convolutional Neural Networks)
• You can manually implement convolutions using NumPy:

• import numpy as np
• from [Link] import convolve2d
• output = convolve2d(image, kernel, mode='valid’)

• But ConvNets themselves are usually built using libraries like PyTorch or TensorFlow.

• Concept:
• A neural network architecture designed to automatically learn spatial hierarchies in data, particularly images.
• Uses convolutional layers, pooling, and non-linearities to extract features.

• Applications:
• Image classification (e.g., CIFAR-10, ImageNet).
• Object detection (YOLO, SSD).
• Image segmentation (U-Net, DeepLab).
• Medical imaging, facial recognition, autonomous driving.

17
Creating arrays : array() function
• The general form of [Link]() in NumPy is:
• [Link](object, dtype=None)
• Parameters:
• object: (array_like)
• The input data (e.g., list, tuple, or another array) to be converted into a NumPy array.
• dtype: (data-type, optional)
• Desired data type of the array (e.g., np.int32, np.float64, etc.).

# From a Python list


py_list = [1, 2, 3, 4]
print("List from py_list : ",py_list)
Output:
arr_from_list = [Link](py_list) List from py_list : [1, 2, 3, 4]
print("Array from list:", arr_from_list) Array from list: [1 2 3 4]
18
Array Attributes
1. Arrays can have any number of dimensions, including zero (a scalar).
2. Arrays are typed: np.uint8, np.int64, np.float32, np.float64
3. Arrays are dense. Each element of the array exists and has the same type.
4. shape, size, ndim, and dtype are particularly important.

import numpy as np
Array:
random_arr = [Link](1, 10, size=(3,4)) [[1 1 2 8]
[8 4 5 2]
print("Array:\n", random_arr) [3 2 9 8]]
print("Shape:", random_arr.shape) Shape: (3, 4)
print("Size:", random_arr.size) Size: 12
print("Dimensions:", random_arr.ndim) Dimensions: 2
print("Data Type:", random_arr.dtype) Data Type: int64
19
Taking help
• ? and . tab completion are useful for exploring the API.
• [Link]?
• help([Link])

20
Arrays, creation
Using built-in functions
• [Link], [Link]
• [Link]
• [Link]
• [Link]
• [Link]
• np.zeros_like, np.ones_like
• [Link]

21
Arrays, creation : [Link], [Link]

22
Arrays, creation import numpy as np

• [Link]() # Basic usage


[Link](5) # Output: [0 1 2 3 4]
[Link]([start,] stop[, step], dtype=None)
# With start and stop
Parameter Description [Link](2, 8) # Output: [2 3 4 5 6 7]
start (optional) The starting value of the sequence.
Default is 0 if omitted. # With step
[Link](2, 10, 2) # Output: [2 4 6 8]
stop (required) The end value (exclusive) of the sequence.
# With negative step
The sequence stops before this value. [Link](10, 2, -2) # Output: [10 8 6 4]

step (optional) The spacing between values. # With dtype


Default is 1. Can be negative. [Link](1, 5, dtype=float) # Output: [1. 2. 3. 4.]

dtype (optional) The data type of the output array.


If not specified, it is inferred from the
input.
23
[Link]()
• For non-integer steps (e.g., 0.1), consider using [Link] for better precision.
• [Link](start, stop, num=50, endpoint=True, retstep=False, dtype=None,
axis=0)
Parameter Description
start The starting value of the sequence.
stop The end value of the sequence.
num Number of evenly spaced samples to generate.
Default is 50.
endpoint If True (default), stop is the last value in the sequence.
If False, the sequence ends before stop.
retstep If True, returns a tuple (samples, step)
where step is the spacing between values.
dtype Data type of the output array.
If not given, it is inferred.
axis The axis in the result along which the samples are stored.
Default is 0. (Useful in multi-dimensional linspace)
24
Example on linspace()
import numpy as np

# Basic usage
[Link](0, 1, 5)
# Output: array([0. , 0.25, 0.5 , 0.75, 1. ])

# Without endpoint
[Link](0, 1, 5, endpoint=False)
# Output: array([0. , 0.2, 0.4, 0.6, 0.8])

# Return step value


x, step = [Link](0, 1, 5, retstep=True)
print(x) # [0. 0.25 0.5 0.75 1. ]
print(step) # 0.25

# Using dtype
[Link](1, 10, 10, dtype=int)
# Output: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

25
Key Differences arange() & linspace()
Feature [Link]() [Link]()
Inputs Start, Stop, Step Start, Stop, Number of Samples
Precision Can have floating-point issues More precise with floating-point
Includes Stop? Never includes stop Includes stop by default (unless endpoint=False)
Best for Known spacing Known number of points

26
arange() & linspace()

27
Arrays, creation : [Link]
[Link], [Link], and [Link] can
help combine arrays.
arrA = [Link]([1, 2, 3]) Concatenate: [1 2 3 4 5 6]
arrB = [Link]([4, 5, 6])
VStack:
print("Concatenate:",
[[1 2]
[Link]([arrA, arrB])) [3 4]
[5 6]
gridA = [Link]([[1,2],[3,4]]) [7 8]]
gridB = [Link]([[5,6],[7,8]])
print("\nVStack:\n", [Link]([gridA, gridB])) HStack:
print("\nHStack:\n", [Link]([gridA, gridB])) [[1 2 5 6]
[3 4 7 8]]

28
Arrays, creation [Link], [Link]

29
Arrays, creation

30
Shaping
• Use reshape to change the shape without altering
data.
a = [Link]([1,2,3,4,5,6])
a = [Link](3,2)
a = [Link](2,-1)
a = [Link]()
1. Total number of elements cannot change.
2. Use -1 to infer axis shape (automatic dimension calculation)
3. Row-major by default (MATLAB is column-major)

31
Transposition
a = [Link](10).reshape(5,2)
a = a.T
a = [Link]((1,0))

[Link] permutes axes.


a.T transposes the first two axes.

32
Mathematical operators
• Arithmetic operations are element-wise
• Logical operator return a bool array
• In place operations modify the array

33
Array operations : Universal Functions
Ufuncs are vectorized, element-by-element functions that allow fast operations on entire arrays without
explicit Python loops. Each arithmetic operator (+, -, *, /, etc.) in NumPy is backed by a ufunc, and there
are many more specialized ufuncs for math, stats, etc.

34
Aggregations
• Aggregations summarize array values into a single numeric result (or one result per axis). Common
examples include minimum, maximum, sum, mean, median, standard deviation, etc.

data = [Link](1, 100, size=10)


print("data:", data) data: [38 61 9 74 1 5 60 77 71 94]
Sum: 490
# Basic aggregations Min: 1
print("Sum:", [Link](data)) Max: 94
print("Min:", [Link](data)) Mean: 49.0
print("Max:", [Link](data)) Standard Deviation: 31.849646779831012
print("Mean:", [Link](data)) matrix:
print("Standard Deviation:", [Link](data)) [[7 1 2 5]
[7 3 5 5]
matrix = [Link](0, 10, size=(3,4)) [9 6 1 8]]
print("matrix:\n", matrix) Min of each column: [7 1 1 5]
print("Min of each column:", [Link](matrix, axis=0)) Max of each row: [7 7 9]
print("Max of each row:", [Link](matrix, axis=1))
35
Indexing of 1D array

36
Two-dimensional (2D) array

37
Two-dimensional (2D) array

38
Mathematical operators
• Arithmetic operations are element-wise
• Logical operator return a bool array
• In place operations modify the array

39
Mathematical operators
• Arithmetic operations are element-wise
• Logical operator return a bool array
• In place operations modify the array

40
Math, upcasting
Just as in Python and Java, the result of a math operator is cast to the
more general or precise datatype.
uint64 + uint16 => uint64
float32 / int32 => float32

Warning: upcasting does not prevent overflow/underflow. You must


manually cast first.
Use case: images often stored as uint8. You should convert to float32 or
float64 before doing math.

41
Math, universal functions
Also called ufuncs
Element-wise
Examples:
• [Link]
• [Link]
• [Link]
• [Link]
• [Link]

42
Indexing
x[0,0] # top-left element
x[0,-1] # first row, last column
x[0,:] # first row (many entries)
x[:,0] # first column (many entries)
Notes:
• Zero-indexing
• Multi-dimensional indices are comma-separated (i.e., a tuple)

43
Indexing, slices and arrays
I[1:-1,1:-1] # select all but one-pixel
border
I = I[:,:,::-1] # swap channel order
I[I<10] = 0 # set dark pixels to black
I[[1,3], :] # select 2nd and 4th row

1. Slices are views. Writing to a slice overwrites the original array.


2. Can also index by a list or boolean array.

44
Python Slicing
Syntax: start:stop:step
a = list(range(10))
a[:3] # indices 0, 1, 2
a[-3:] # indices 7, 8, 9
a[3:8:2] # indices 3, 5, 7
a[4:1:-1] # indices 4, 3, 2 (this one is tricky)

45
Axes
[Link]() # sum all entries
[Link](axis=0) # sum over rows
[Link](axis=1) # sum over columns
[Link](axis=1, keepdims=True)
1. Use the axis parameter to control which axis NumPy operates on
2. Typically, the axis specified will disappear, keepdims keeps all
dimensions

46
Numpy vectorized operations

47
Matrix Power

48
Broadcasting
• Allows operations on arrays of different shapes
by stretching dimensions when possible. Reference:
[Link]
a = a + 1 # add one to every element er/[Link]

When operating on multiple arrays, broadcasting rules are used.


Each dimension must match, from right-to-left
1. Dimensions of size 1 will broadcast (as if the value was repeated).
2. Otherwise, the dimension must have the same shape.
3. Extra dimensions of size 1 are added to the left as needed.
49
Broadcasting example
a = [Link]([[ 0.0, 0.0, 0.0], [[ 0. 0. 0.]
[10.0, 10.0, 10.0], [10. 10. 10.]
[20.0, 20.0, 20.0], [20. 20. 20.]
[30.0, 30.0, 30.0]]) [30. 30. 30.]]
b = [Link]([1.0, 2.0, 3.0]) [1. 2. 3.]
print(a) a + b:
print(b) [[ 1. 2. 3.]
[11. 12. 13.]
[21. 22. 23.]
# Broadcasting
[31. 32. 33.]]
print("a + b:\n", a + b)

50
Broadcasting failures
If [Link] is 100, 200, 3 but [Link] is 4 then a + b will fail. The trailing
dimensions must have the same shape (or be 1)

51
Sorting & Partitioning
• [Link](arr) returns a sorted copy.
• [Link]() sorts in-place.
• [Link] returns the indices.
unsorted_arr = [Link]([2,1,4,3,5])
print("Sorted copy:",
[Link](unsorted_arr)) Sorted copy: [1 2 3 4 5]
print("Original:", unsorted_arr) Original: [2 1 4 3 5]
In-place sort: [1 2 3 4 5]
unsorted_arr.sort()
print("In-place sort:", unsorted_arr)

52

You might also like