ch8s2_NumPyArraysAndOperations

NumPy arrays (or **ndarrays**) are the **core data structure** in NumPy and the foundation of high-performance numerical computing in Python.

Chapter 8: Introduction to NumPy

Sub-Chapter: NumPy Arrays and Operations — The Backbone of Numerical Computing

NumPy arrays (or ndarrays) are the core data structure in NumPy and the foundation of high-performance numerical computing in Python.
They provide efficient, vectorized operations on homogeneous data — enabling mathematical computations that are fast, expressive, and memory-efficient.


🧩 1. What Makes NumPy Arrays Special?

Unlike Python lists, which store objects of mixed types and have high overhead, NumPy arrays:

Comparison Example

import numpy as np
import time

nums = list(range(1_000_000))
arr = np.arange(1_000_000)

# Python list
start = time.time()
nums_result = [x * 2 for x in nums]
print("List:", time.time() - start)

# NumPy array
start = time.time()
arr_result = arr * 2
print("NumPy:", time.time() - start)

🚀 NumPy operations can be 50–100× faster than native Python loops.


⚙️ 2. Creating Arrays

You can create NumPy arrays from Python sequences or using NumPy’s built-in constructors.

import numpy as np

a = np.array([1, 2, 3])                    # From list
b = np.array([[1, 2, 3], [4, 5, 6]])       # 2D array
zeros = np.zeros((2, 3))                   # All zeros
ones = np.ones((3, 3))                     # All ones
range_array = np.arange(0, 10, 2)          # Evenly spaced values
linspace = np.linspace(0, 1, 5)            # 5 values between 0 and 1
identity = np.eye(3)                       # 3×3 identity matrix
randoms = np.random.randint(0, 10, (2, 3)) # Random integers 0–9

Quick Reference: Array Creation

FunctionDescriptionExample
np.array()Convert list to ndarraynp.array([1,2,3])
np.zeros()Create zerosnp.zeros((3,3))
np.ones()Create onesnp.ones((2,4))
np.arange()Range with stepnp.arange(0,10,2)
np.linspace()Evenly spacednp.linspace(0,1,5)
np.eye()Identity matrixnp.eye(3)
np.empty()Uninitialized memorynp.empty((2,3))
np.random.rand()Uniform randomnp.random.rand(2,2)

🔍 3. Array Properties and Attributes

Each NumPy array exposes useful metadata.

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

print(arr.ndim)     # 2 dimensions
print(arr.shape)    # (2, 3)
print(arr.size)     # 6 elements
print(arr.dtype)    # int64
print(arr.itemsize) # Bytes per element
print(arr.nbytes)   # Total memory usage

🧠 All NumPy arrays are typed — mixing floats and ints will upcast automatically (e.g. intfloat).


🔢 4. Array Operations (Vectorization)

NumPy automatically applies operations element-wise — known as vectorization.

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]
print(a / 2)   # [0.5 1. 1.5]

Comparison and Logical Operations

arr = np.array([10, 20, 30, 40])
print(arr > 25)         # [False False  True  True]
print(np.logical_and(arr > 10, arr < 40))

🧮 5. Matrix and Linear Algebra Operations

NumPy supports advanced linear algebra directly.

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

print(A @ B)           # Matrix multiplication
print(np.dot(A, B))    # Same as A @ B
print(np.transpose(A)) # Transpose
print(np.linalg.inv(A))# Inverse
print(np.linalg.det(A))# Determinant

🧠 6. Statistical and Aggregate Functions

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

print(arr.sum())         # 21
print(arr.mean())        # 3.5
print(arr.std())         # Standard deviation
print(arr.min(axis=0))   # Column-wise min
print(arr.max(axis=1))   # Row-wise max

Use axis=0 for columns and axis=1 for rows.


🔄 7. Reshaping and Transposing

You can change the shape of an array without copying its data.

matrix = np.arange(12).reshape(3, 4)

print(matrix.shape)      # (3, 4)
print(matrix.reshape(4, 3))
print(matrix.flatten())  # 1D copy
print(matrix.ravel())    # 1D view (no copy)
print(matrix.T)          # Transposed

Shape Diagram

Original (3×4)
[[0, 1, 2, 3],
 [4, 5, 6, 7],
 [8, 9, 10, 11]]

Reshaped (4×3)
[[0, 1, 2],
 [3, 4, 5],
 [6, 7, 8],
 [9,10,11]]

📈 8. Broadcasting Explained

Broadcasting allows operations between arrays of different shapes by expanding smaller arrays automatically.

matrix = np.array([[1, 2, 3], [4, 5, 6]])
vector = np.array([10, 20, 30])

print(matrix + vector)

Output:

[[11 22 33]
 [14 25 36]]

Broadcasting Rules

  1. Compare dimensions from right to left.
  2. Dimensions are compatible if they are equal or one of them is 1.
  3. If incompatible, a ValueError occurs.

Visual Explanation

Matrix (2×3): [[1 2 3]
               [4 5 6]]
Vector (1×3): [10 20 30]

[[11 22 33]
 [14 25 36]]

🧩 9. Combining and Splitting Arrays

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

combined = np.vstack((a, b))  # Vertical stack
hstacked = np.hstack((a, b.T))# Horizontal stack
split = np.split(a, 2)        # Split into two arrays

⚡ 10. Real-World Example — Data Normalization

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

mean = np.mean(data, axis=0)
std = np.std(data, axis=0)
normalized = (data - mean) / std

print(normalized)

🧾 11. Quick Reference Tables

Array Attributes

AttributeDescriptionExample Output
ndimNumber of dimensions2
shapeTuple of array size(3, 4)
sizeTotal number of elements12
dtypeData typefloat64
itemsizeBytes per element8

Common Functions

CategoryFunctionPurpose
Creationnp.zeros(), np.ones(), np.arange(), np.random.rand()Create arrays
Mathnp.add, np.multiply, np.powerElement-wise math
Statisticsnp.mean, np.std, np.min, np.maxAggregation
Shapereshape, flatten, ravel, resizeManipulation
Linear Algebranp.dot, np.linalg.inv, np.linalg.detMatrix operations

🧭 12. Best Practices

✅ Use vectorized operations instead of loops.
✅ Prefer broadcasting to manual repetition.
✅ Always check dtype when mixing types.
✅ Avoid unnecessary copies with .ravel() instead of .flatten().
✅ Use axis wisely for column/row aggregation.
✅ Use np.random for reproducible test data.


🧠 Summary

ConceptDescriptionExample
Array CreationFrom lists or constructorsnp.array([[1,2],[3,4]])
Element-wise OpsFast, vectorized matha + b, a * 2
Matrix OpsDot, transpose, inverseA @ B, A.T
ReshapeChange structurearr.reshape(3,4)
BroadcastingAlign shapesmatrix + vector

NumPy arrays turn Python into a high-performance, data-parallel computation environment.
Mastering them unlocks everything from basic math to advanced machine learning pipelines.