NumPy for AI Beginners: AI ની Mathematics — Arrays અને Matrix સરળ ગુજરાતીમાં
Python શીખ્યા. Jupyter ખૂલ્યું. હવે AI Code લખો — પ્રથમ Line:
import numpy as np
NumPy — AI Developer ની સૌ પ્રથમ Library. ઇ વગર AI ન ચાલે.
NumPy એટલે શું?
NumPy = Numerical Python — Fast, Powerful, Mathematical Arrays.
Python List vs NumPy Array:
# Python List — Slow
my_list = [1, 2, 3, 4, 5]
# NumPy Array — Fast (10x–100x)
import numpy as np
my_array = np.array([1, 2, 3, 4, 5])
Speed ફરક:
import time
# Python List — 1 Million Numbers
py_list = list(range(1_000_000))
start = time.time()
result = [x * 2 for x in py_list]
print(f"List: {time.time() - start:.3f}s") # ~0.100s
# NumPy Array
np_array = np.arange(1_000_000)
start = time.time()
result = np_array * 2
print(f"NumPy: {time.time() - start:.3f}s") # ~0.002s
⚡ NumPy 50x ઝડપ — AI Training Millions of Numbers — NumPy વગર ઘણો Slow!
Array બનાવો — 5 Ways
import numpy as np
# 1. List માંથી
a = np.array([10, 20, 30, 40])
print(a) # [10 20 30 40]
# 2. Range
b = np.arange(0, 10, 2)
print(b) # [0 2 4 6 8]
# 3. Zeros
c = np.zeros(5)
print(c) # [0. 0. 0. 0. 0.]
# 4. Ones
d = np.ones((3, 3))
print(d)
# [[1. 1. 1.]
# [1. 1. 1.]
# [1. 1. 1.]]
# 5. Random
e = np.random.rand(3, 3)
print(e) # 0–1 ના Random Numbers
Array Shape — "Dimensions સમજો"
# 1D Array (Vector)
v = np.array([1, 2, 3, 4])
print(v.shape) # (4,)
# 2D Array (Matrix)
m = np.array([[1, 2, 3],
[4, 5, 6]])
print(m.shape) # (2, 3) — 2 Rows, 3 Columns
# 3D Array (Tensor — Images!)
t = np.zeros((100, 28, 28))
print(t.shape) # (100, 28, 28) — 100 Images, 28×28 Pixels
💡 AI માં Image = 3D Array: Height × Width × Channels (RGB = 3)
(224, 224, 3)= 224px × 224px × 3 Colors
Array Operations — "Math ઝટ"
a = np.array([1, 2, 3, 4])
b = np.array([10, 20, 30, 40])
# Basic Math
print(a + b) # [11 22 33 44]
print(a * b) # [10 40 90 160]
print(b / a) # [10. 10. 10. 10.]
print(a ** 2) # [ 1 4 9 16]
# Scalar Operation (Broadcast)
print(a * 5) # [ 5 10 15 20]
print(a + 100) # [101 102 103 104]
Broadcasting — NumPy ની Magic:
# 2D + 1D — Automatic Broadcast
matrix = np.array([[1, 2, 3],
[4, 5, 6]])
row = np.array([10, 20, 30])
print(matrix + row)
# [[11 22 33]
# [14 25 36]]
Indexing & Slicing — "Data ઉઠાવો"
arr = np.array([10, 20, 30, 40, 50])
# Single Element
print(arr[0]) # 10
print(arr[-1]) # 50
# Slice
print(arr[1:4]) # [20 30 40]
print(arr[:3]) # [10 20 30]
print(arr[::2]) # [10 30 50] — Every 2nd
# 2D Indexing
m = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(m[1, 2]) # 6 (Row 1, Col 2)
print(m[:, 1]) # [2 5 8] — Column 1 બધી Rows
print(m[0, :]) # [1 2 3] — Row 0 બધી Columns
Statistical Functions — "Data Summary"
data = np.array([85, 92, 78, 95, 88, 72, 91, 87])
print(np.mean(data)) # 86.0 — Average
print(np.median(data)) # 87.5 — Middle Value
print(np.std(data)) # 7.19 — Standard Deviation
print(np.min(data)) # 72 — Minimum
print(np.max(data)) # 95 — Maximum
print(np.sum(data)) # 688 — Total
Matrix Operations — AI ની Heart
A = np.array([[1, 2],
[3, 4]])
B = np.array([[5, 6],
[7, 8]])
# Matrix Multiply — AI ની સૌ Important Operation!
C = np.dot(A, B)
print(C)
# [[19 22]
# [43 50]]
# Modern Syntax
C = A @ B
print(C) # Same Result
# Transpose
print(A.T)
# [[1 3]
# [2 4]]
# Inverse
print(np.linalg.inv(A))
💡 Neural Network = ઘણા Matrix Multiply! Input × Weights = Output — NumPy
@Operator = AI Core Math.
Reshape — "Shape બદલો"
# 1D → 2D
a = np.arange(12)
print(a) # [ 0 1 2 3 4 5 6 7 8 9 10 11]
b = a.reshape(3, 4)
print(b)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
# -1 = Auto Calculate
c = a.reshape(2, -1) # 2 Rows, Auto Columns
print(c.shape) # (2, 6)
# Image Flatten (CNN Input)
image = np.zeros((28, 28)) # 28×28 Image
flat = image.flatten()
print(flat.shape) # (784,)
Boolean Indexing — "Filter Data"
scores = np.array([45, 78, 92, 55, 88, 61, 95, 42])
# 60 ઉ Pass
passed = scores[scores >= 60]
print(passed) # [78 92 88 61 95]
# 90+ Toppers
toppers = scores[scores >= 90]
print(toppers) # [92 95]
# Condition Replace
scores[scores < 60] = 0 # Fail = 0
print(scores) # [ 0 78 92 0 88 61 95 0]
AI Project માં NumPy — Real Use
import numpy as np
# Dataset Normalize (0–1 Range)
data = np.array([200, 400, 150, 600, 350])
normalized = (data - data.min()) / (data.max() - data.min())
print(normalized)
# [0.11 0.56 0. 1. 0.44]
# Train/Test Split
X = np.random.rand(100, 5) # 100 Samples, 5 Features
y = np.random.randint(0, 2, 100)
split = 80
X_train, X_test = X[:split], X[split:]
y_train, y_test = y[:split], y[split:]
print(X_train.shape) # (80, 5)
print(X_test.shape) # (20, 5)
# One-Hot Encoding
labels = np.array([0, 1, 2, 1, 0])
one_hot = np.eye(3)[labels]
print(one_hot)
# [[1. 0. 0.]
# [0. 1. 0.]
# [0. 0. 1.]
# [0. 1. 0.]
# [1. 0. 0.]]
NumPy Cheat Sheet
np.array([...]) # Array બનાવો
np.zeros((m, n)) # Zero Matrix
np.ones((m, n)) # Ones Matrix
np.arange(start, stop) # Range Array
np.random.rand(m, n) # Random Array
arr.shape # Dimensions
arr.reshape(m, n) # Shape Change
arr.flatten() # 1D બનાવો
arr[i, j] # Element
arr[:, col] # Column
np.dot(A, B) / A @ B # Matrix Multiply
np.mean/std/min/max(arr) # Statistics
arr[arr > value] # Filter
નિષ્કર્ષ
NumPy = AI ની Mathematics ની Language. Array, Matrix, Broadcast, Math — NumPy વગર PyTorch, TensorFlow, scikit-learn — કોઈ ન ચાલે.
import numpy as np — AI Developer ની સૌ પ્રથમ Line — હવે ખ્યાલ આવ્યો? 🚀