NumPy Array Operations
NumPy allows you to perform mathematical operations on entire arrays without writing loops. This is called vectorization. It is much faster and more concise.
Element‑wise Operations
Arithmetic operations apply element by element.
a = np.array([1,2,3])
b = np.array([4,5,6])
print(a + b) # [5,7,9]
print(a * b) # [4,10,18]
print(a ** 2) # [1,4,9]Universal Functions (ufuncs)
NumPy provides fast vectorized functions for mathematical operations.
x = np.array([0, np.pi/2, np.pi])
print(np.sin(x)) # [0, 1, 0]
print(np.exp(x)) # exponential
print(np.log(x+1)) # natural logAggregation Functions
Compute statistics across arrays.
data = np.array([[1,2],[3,4]])
print(np.sum(data)) # 10
print(np.mean(data)) # 2.5
print(np.max(data)) # 4
print(np.sum(data, axis=0)) # sum along columns: [4,6]Why Vectorization Matters for AI
Training a neural network involves millions of operations. Loops in Python would be too slow. NumPy’s vectorized operations run in optimized C code, making them 10‑100x faster.
Two Minute Drill
- Arithmetic is element‑wise:
+, -, *, /, **. - Universal functions:
np.sin(),np.exp(),np.log(). - Aggregations:
np.sum(),np.mean(),np.max(). - Use
axisparameter to specify direction.
Need more clarification?
Drop us an email at career@quipoinfotech.com
