Numpy arrays covering below sections:
From Python List — 1D and 2D arrays
Built-in Creators — zeros, ones, full, eye
Range-based — arange, linspace
Random — rand, randn, randint
Attributes — shape, ndim, size, dtype
Data Types — int32, float64, complex128
Reshaping — reshape to any shape
Indexing/Slicing — row, col, block access
Operations — element-wise math, broadcasting
Aggregates — sum, min, max, mean, std
import numpy as np
# ── 1. From Python List ───────────────────────────────────────────────────────
a = np.array([1, 2, 3]) # 1D array from list
print(a) # [1 2 3]
print(a.dtype) # int64
print(a.shape) # (3,)
b = np.array([[1, 2, 3], [4, 5, 6]]) # 2D array from nested list
print(b) # [[1 2 3]
# [4 5 6]]
print(b.shape) # (2, 3) → 2 rows, 3 cols
print(b.ndim) # 2 → number of dimensions
# ── 2. Built-in Array Creators ────────────────────────────────────────────────
print(np.zeros((3, 3))) # 3×3 matrix of 0.0
print(np.ones((2, 4))) # 2×4 matrix of 1.0
print(np.full((2, 2), 7)) # 2×2 matrix filled with 7
print(np.eye(3)) # 3×3 identity matrix (1s on diagonal)
# [[1. 0. 0.]
# [0. 1. 0.]
# [0. 0. 1.]]
# ── 3. Range-based Arrays ─────────────────────────────────────────────────────
print(np.arange(0, 10)) # [0 1 2 3 4 5 6 7 8 9]
print(np.arange(0, 10, 2)) # [0 2 4 6 8] step=2
print(np.linspace(0, 1, 5)) # [0. 0.25 0.5 0.75 1. ] 5 evenly spaced
# ── 4. Random Arrays ──────────────────────────────────────────────────────────
np.random.seed(42) # seed for reproducibility
print(np.random.rand(2, 3)) # 2×3 uniform random (0.0 to 1.0)
# [[0.37 0.95 0.73]
# [0.59 0.15 0.05]]
print(np.random.randn(2, 3)) # 2×3 standard normal (mean=0, std=1)
# [[ 0.64 1.52 0.31]
# [-0.23 0.18 -0.23]]
print(np.random.randint(1, 10, (2, 3))) # 2×3 random integers between 1–9
# [[6 1 4]
# [4 8 9]]
# ── 5. Array Attributes ───────────────────────────────────────────────────────
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape) # (2, 3) → rows, cols
print(arr.ndim) # 2 → dimensions
print(arr.size) # 6 → total elements
print(arr.dtype) # int64 → data type
# ── 6. Data Types ─────────────────────────────────────────────────────────────
i = np.array([1, 2, 3], dtype=np.int32) # 32-bit integer
f = np.array([1, 2, 3], dtype=np.float64) # 64-bit float
s = np.array([1, 2, 3], dtype=np.complex128) # complex number
print(i.dtype) # int32
print(f.dtype) # float64
print(s.dtype) # complex128
# ── 7. Reshaping Arrays ───────────────────────────────────────────────────────
x = np.arange(1, 10) # [1 2 3 4 5 6 7 8 9]
print(x.reshape(3, 3)) # reshape to 3×3
# [[1 2 3]
# [4 5 6]
# [7 8 9]]
print(x.reshape(1, 9)) # 1 row, 9 cols → [[1 2 3 4 5 6 7 8 9]]
print(x.reshape(9, 1)) # 9 rows, 1 col → [[1],[2],...,[9]]
# ── 8. Indexing and Slicing ───────────────────────────────────────────────────
arr = np.array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
print(arr[0, 0]) # 10 → row 0, col 0
print(arr[1, 2]) # 60 → row 1, col 2
print(arr[2, :]) # [70 80 90] → entire row 2
print(arr[:, 1]) # [20 50 80] → entire col 1
print(arr[0:2, 0:2]) # [[10 20] → top-left 2×2 block
# [40 50]]
# ── 9. Array Operations ───────────────────────────────────────────────────────
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # [5 7 9] element-wise add
print(a * b) # [4 10 18] element-wise multiply
print(a ** 2) # [1 4 9] element-wise power
print(a + 10) # [11 12 13] broadcasting scalar
# ── 10. Aggregate Functions ───────────────────────────────────────────────────
arr = np.array([[1, 2, 3],
[4, 5, 6]])
print(arr.sum()) # 21 → total sum
print(arr.sum(axis=0)) # [5 7 9] → col-wise sum
print(arr.sum(axis=1)) # [6 15] → row-wise sum
print(arr.min()) # 1
print(arr.max()) # 6
print(arr.mean()) # 3.5
print(arr.std()) # 1.707...
No comments:
Post a Comment
Please comment below to feedback or ask questions.