0% found this document useful (0 votes)
31 views4 pages

Numpy Arithmetic Broadcasting

The document provides an overview of NumPy's arithmetic functions, array shape manipulation, and broadcasting rules. It explains how to find minimum and maximum values, reshape arrays, and perform arithmetic operations on arrays of different shapes. Key examples illustrate the use of these functions and concepts in practical scenarios.

Uploaded by

Ashish Chinche
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)
31 views4 pages

Numpy Arithmetic Broadcasting

The document provides an overview of NumPy's arithmetic functions, array shape manipulation, and broadcasting rules. It explains how to find minimum and maximum values, reshape arrays, and perform arithmetic operations on arrays of different shapes. Key examples illustrate the use of these functions and concepts in practical scenarios.

Uploaded by

Ashish Chinche
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/ 4

Title: NumPy Arithmetic Functions & Broadcasting Notes

1. Using Arithmetic Functions in NumPy


Import NumPy:

import numpy as np

Minimum and Maximum values:

• np.min(array) → minimum value


• np.max(array) → maximum value

Example:

arr = np.array([1, 2, 3, 4, 5])


print(np.min(arr)) # 1
print(np.max(arr)) # 5

Index positions of Min and Max:

• np.argmin(array) → index of minimum


• np.argmax(array) → index of maximum

Multi-dimensional arrays:

• Use axis to get min/max along rows or columns

arr = np.array([[1, 2, 3], [4, 5, 6]])


print(np.min(arr, axis=0)) # column-wise min → [1, 2, 3]
print(np.min(arr, axis=1)) # row-wise min → [1, 4]

Square root:

• np.sqrt(array) → element-wise square root

Trigonometric functions:

• np.sin(array) → sine
• np.cos(array) → cosine

1
x = np.array([0, np.pi/2, np.pi])
print(np.sin(x)) # [0, 1, 0]
print(np.cos(x)) # [1, 0, -1]

Cumulative sum:

• np.cumsum(array) → cumulative sum

arr = np.array([1, 2, 3, 4])


print(np.cumsum(arr)) # [1, 3, 6, 10]

Real-life uses: - Min/Max → find extremes - ArgMin/ArgMax → find positions (e.g., max sales day) -
Cumulative sum → statistics, time series, progress tracking - Trig & sqrt → scientific computations

2. Array Shape in NumPy


Checking shape:

arr = np.array([[1, 2], [3, 4]])


print(arr.shape) # (2, 2) → 2 rows, 2 columns

Converting 1D → 2D → 3D:

arr = np.array([1,2,3,4,5,6])
print(arr.shape) # (6,)

arr2d = arr.reshape(2,3)
print(arr2d.shape) # (2,3)

arr3d = arr.reshape(2,1,3)
print(arr3d.shape) # (2,1,3)

Checking dimensions:

print(arr.ndim) # 1
print(arr2d.ndim) # 2
print(arr3d.ndim) # 3

2
Flatten back to 1D:

flat = arr3d.reshape(-1)
print(flat) # [1 2 3 4 5 6]
print(flat.shape) # (6,)

Rules:

• .shape → gives rows, columns, structure


• .ndim → number of dimensions
• .reshape() → convert arrays (element count must remain same)
• .reshape(-1) → flatten to 1D

3. Broadcasting in NumPy
Definition: - Allows arithmetic operations on arrays of different shapes. - Smaller array is automatically
stretched to match shape of larger array (without copying data).

Broadcasting Rules:

1. Compare dimensions from right to left.


2. Dimensions must be either same or 1.
3. Result shape = maximum size along each dimension.
4. If rules don’t match → ❌ error

Example 1: Simple Addition

import numpy as np
a = np.array([1, 2, 3, 4]) # shape (4,)
b = np.array([10]) # shape (1,)
print(a + b) # [11 12 13 14]

Example 2: 2D + 1D

a = np.array([[1,2,3],[4,5,6]]) # (2,3)
b = np.array([10,20,30]) # (3,)
print(a + b)
# [[11 22 33]
# [14 25 36]]

3
Example 3: Mismatched shapes

a = np.array([[1,2,3],[4,5,6]]) # (2,3)
b = np.array([1,2]) # (2,)
print(a + b) # ❌ Error

Summary: - Broadcasting enables operations on arrays of different shapes. - Compare dimensions right →
left. - Dimensions must be same or 1. - Result shape = maximum along each dimension. - Mismatch → error.

You might also like