Pages

Numpy operations

Numpy covers below:
Arithmetic — +, -, *, /, //, %, ** Scalar / Broadcasting — apply single value to all elements ufuncs — sqrt, abs, exp, log Trigonometry — sin, cos, tan Aggregates — sum, min, max, mean, std, var with axis Comparison — ==, !=, >, <, array_equal Dot Product & Matrix Multiply — np.dot, @ Sorting — sort, argsort with axis Rounding — round, floor, ceil, trunc Cumulative — cumsum, cumprod, diff

import numpy as np

# ── 1. Arithmetic Operations ──────────────────────────────────────────────────
# Element-wise math between arrays of same shape

a = np.array([10, 20, 30, 40])
b = np.array([2, 4, 5, 8])

print(a + b) # [12 24 35 48] → element-wise add
print(a - b) # [ 8 16 25 32] → element-wise subtract
print(a * b) # [ 20 80 150 320] → element-wise multiply
print(a / b) # [5. 5. 6. 5.] → element-wise divide
print(a // b) # [5 5 6 5] → floor division
print(a % b) # [0 0 0 0] → modulus
print(a ** 2) # [ 100 400 900 1600] → power

# ── 2. Scalar Operations (Broadcasting) ──────────────────────────────────────
# Apply a single value to every element in the array

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

print(a + 10) # [11 12 13 14 15]
print(a * 3) # [ 3 6 9 12 15]
print(a / 2) # [0.5 1. 1.5 2. 2.5]
print(a ** 2) # [ 1 4 9 16 25]

# ── 3. Universal Functions (ufuncs) ───────────────────────────────────────────
# Built-in NumPy functions applied element-wise

a = np.array([1, 4, 9, 16, 25])

print(np.sqrt(a)) # [1. 2. 3. 4. 5.] → square root
print(np.abs(np.array([-1, -2, 3, -4]))) # [1 2 3 4] → absolute value
print(np.exp(np.array([0, 1, 2]))) # [1. 2.718 7.389] → e^x
print(np.log(np.array([1, np.e, np.e**2]))) # [0. 1. 2.] → natural log
print(np.log10(np.array([1, 10, 100]))) # [0. 1. 2.] → log base 10

# ── 4. Trigonometric Functions ────────────────────────────────────────────────

angles = np.array([0, np.pi/6, np.pi/4, np.pi/2, np.pi])

print(np.sin(angles).round(2)) # [0. 0.5 0.71 1. 0. ]
print(np.cos(angles).round(2)) # [ 1. 0.87 0.71 0. -1. ]
print(np.tan(angles).round(2)) # [ 0. 0.58 1. 1.63e+16 -0. ]

# ── 5. Aggregate / Statistical Operations ────────────────────────────────────
# Summarize entire array or along an axis

a = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

print(a.sum()) # 45 → total sum
print(a.sum(axis=0)) # [12 15 18] → column-wise sum
print(a.sum(axis=1)) # [ 6 15 24] → row-wise sum

print(a.min()) # 1 → global min
print(a.max()) # 9 → global max
print(a.mean()) # 5.0 → global mean
print(a.std()) # 2.581 → standard deviation
print(a.var()) # 6.666 → variance

print(a.min(axis=0)) # [1 2 3] → min of each column
print(a.max(axis=1)) # [3 6 9] → max of each row

# ── 6. Comparison Operations ──────────────────────────────────────────────────
# Returns boolean array

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

print(a == b) # [False False True False False]
print(a != b) # [ True True False True True]
print(a > b) # [False False False True True]
print(a < b) # [ True True False False False]
print(np.array_equal(a, b)) # False → True only if all elements match

# ── 7. Dot Product & Matrix Multiplication ────────────────────────────────────

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

print(np.dot(a, b)) # 32 → 1×4 + 2×5 + 3×6

A = np.array([[1, 2],
[3, 4]])
B = np.array([[5, 6],
[7, 8]])

print(A @ B) # [[19 22] → matrix multiplication
# [43 50]]
print(np.dot(A, B)) # [[19 22] → same as @
# [43 50]]

# ── 8. Sorting ────────────────────────────────────────────────────────────────

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

print(np.sort(a)) # [1 1 2 3 4 5 6 9] → sorted copy
print(np.argsort(a)) # [1 3 6 0 2 4 7 5] → indices that would sort

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

print(np.sort(b, axis=1)) # [[1 2 3] → sort each row
# [4 5 6]]
print(np.sort(b, axis=0)) # [[3 1 2] → sort each column
# [6 4 5]]

# ── 9. Rounding ───────────────────────────────────────────────────────────────

a = np.array([1.234, 2.567, 3.891])

print(np.round(a, 1)) # [1.2 2.6 3.9] → 1 decimal place
print(np.floor(a)) # [1. 2. 3.] → round down
print(np.ceil(a)) # [2. 3. 4.] → round up
print(np.trunc(a)) # [1. 2. 3.] → remove decimal part

# ── 10. Cumulative Operations ─────────────────────────────────────────────────

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

print(np.cumsum(a)) # [ 1 3 6 10 15] → running total
print(np.cumprod(a)) # [ 1 2 6 24 120] → running product
print(np.diff(a)) # [1 1 1 1] → difference between consecutive

 

No comments:

Post a Comment

Please comment below to feedback or ask questions.