Pages

Numpy Array Indexing

Numpy Array indexing covers below:

1D Indexing — positive and negative index
1D Slicing — start/stop/step, reverse
2D Indexing — [row, col] access
2D Slicing — rows, cols, blocks
Fancy Indexing — list of indices
Boolean Indexing — filter by condition
Modifying — assign via index or condition
np.where — conditional value selection

import numpy as np

# ── 1. 1D Array Indexing ──────────────────────────────────────────────────────
# Access elements by position (0-based index)

a = np.array([10, 20, 30, 40, 50])

print(a[0]) # 10 → first element
print(a[2]) # 30 → third element
print(a[-1]) # 50 → last element
print(a[-2]) # 40 → second from last

# ── 2. 1D Array Slicing ───────────────────────────────────────────────────────
# Syntax: array[start:stop:step] (stop is exclusive)

print(a[1:4]) # [20 30 40] → index 1 to 3
print(a[:3]) # [10 20 30] → from start to index 2
print(a[2:]) # [30 40 50] → from index 2 to end
print(a[::2]) # [10 30 50] → every 2nd element
print(a[::-1]) # [50 40 30 20 10] → reversed

# ── 3. 2D Array Indexing ──────────────────────────────────────────────────────
# Syntax: array[row, col]

b = np.array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])

print(b[0, 0]) # 10 → row 0, col 0
print(b[1, 2]) # 60 → row 1, col 2
print(b[2, 1]) # 80 → row 2, col 1
print(b[-1, -1]) # 90 → last row, last col

# ── 4. 2D Array Slicing ───────────────────────────────────────────────────────
# Syntax: array[row_start:row_stop, col_start:col_stop]

print(b[0, :]) # [10 20 30] → entire row 0
print(b[:, 1]) # [20 50 80] → entire col 1
print(b[0:2, 0:2]) # [[10 20] → top-left 2×2 block
# [40 50]]
print(b[1:, 1:]) # [[50 60] → bottom-right 2×2 block
# [80 90]]
print(b[:, 0]) # [10 40 70] → entire first column

# ── 5. Fancy Indexing ─────────────────────────────────────────────────────────
# Pass a list of indices to select multiple specific elements

a = np.array([10, 20, 30, 40, 50])

print(a[[0, 2, 4]]) # [10 30 50] → elements at index 0,2,4
print(a[[1, 3]]) # [20 40] → elements at index 1,3

b = np.array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])

print(b[[0, 2]]) # [[10 20 30] → rows 0 and 2
# [70 80 90]]

# ── 6. Boolean Indexing ───────────────────────────────────────────────────────
# Filter elements using a condition (returns elements where condition is True)

a = np.array([10, 20, 30, 40, 50])

print(a > 25) # [False False True True True]
print(a[a > 25]) # [30 40 50] → elements greater than 25
print(a < 25) # [ True True False False False]
print(a[a < 25]) # [10 20] → elements less than 25
print(a[a % 20 == 0]) # [20 40] → elements divisible by 20

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

print(b[b > 5]) # [6 7 8 9] → flattened result

# ── 7. Modifying with Indexing ────────────────────────────────────────────────
# Assign new values using index or condition

a = np.array([10, 20, 30, 40, 50])

a[0] = 99 # change single element
print(a) # [99 20 30 40 50]

a[1:3] = 0 # change a slice
print(a) # [99 0 0 40 50]

a[a > 40] = -1 # change elements matching condition
print(a) # [-1 0 0 40 -1]

a[:] = 0 # change ALL elements at once
print(a) # [0 0 0 0 0]

# ── 8. np.where ───────────────────────────────────────────────────────────────
# Conditional selection: np.where(condition, value_if_true, value_if_false)

a = np.array([10, 20, 30, 40, 50])

result = np.where(a > 25, a, 0) # keep value if > 25 else 0
print(result) # [ 0 0 30 40 50]

result = np.where(a % 20 == 0, "even20", "other")
print(result) # ['other' 'even20' 'other' 'even20' 'other']

# ── 9. copy() vs View ─────────────────────────────────────────────────────────
# Slicing returns a VIEW — modifying it changes the original
# copy() creates an independent array — changes do NOT affect original

a = np.array([10, 20, 30, 40, 50])

# View (no copy) — shares memory with original
view = a[1:4]
view[0] = 99
print(a) # [10 99 30 40 50] ← original changed!

# copy() — independent array
a = np.array([10, 20, 30, 40, 50])
copy = a[1:4].copy()
copy[0] = 99
print(copy) # [99 30 40] ← copy changed
print(a) # [10 20 30 40 50] ← original unchanged

# Full array copy
b = a.copy()
b[0] = 0
print(b) # [ 0 20 30 40 50]
print(a) # [10 20 30 40 50] ← original safe

No comments:

Post a Comment

Please comment below to feedback or ask questions.