Vectorized computation
Numerical work shows up everywhere in AI systems, from scoring options to transforming data. Writing these computations clearly and efficiently matters, because they often sit on hot paths inside an agent’s workflow. This lesson exists to orient us to vectorized computation—the style of working with whole arrays at once instead of stepping through values one by one.
Applying operations to entire arrays
With NumPy, we can apply an operation to an entire array in a single expression. The operation is understood as acting on every element.
import numpy as np
distances = np.array([58.4, 108.2, 149.6])
scaled = distances * 1_000_000
The multiplication is applied across the array, producing a new array of scaled values. There is no explicit loop in our code, but the intent is still clear.
Element-wise arithmetic operations
Arithmetic operators work element by element when applied to arrays of the same shape. This includes addition, subtraction, multiplication, and division.
import numpy as np
orbital_days = np.array([88, 225, 365])
earth_days = orbital_days / 365
Each element is divided independently. This style reads like math, which makes numerical code easier to reason about.
Aggregation operations over arrays
Often we want a single summary value from many numbers. NumPy provides aggregation operations that operate across the entire array.
import numpy as np
temperatures = np.array([167, 464, -65])
average = temperatures.mean()
The aggregation is expressed directly, without manual accumulation or bookkeeping.
Comparing vectorized operations to loops
The same computation can usually be written with a loop, but vectorized code is shorter and more direct.
import numpy as np
values = np.array([1, 2, 3, 4, 5])
squared = values ** 2
A loop would describe how to do the work step by step. Vectorized code focuses on what the computation is, leaving the mechanics to the library.
Using vectorized computation for efficiency
Vectorized operations are not just convenient. They are also efficient, because NumPy executes them in optimized native code rather than Python-level loops.
import numpy as np
masses = np.array([3.3e23, 4.9e24, 6.0e24])
total_mass = masses.sum()
This efficiency becomes important as datasets grow and computations appear inside repeated agent decisions or scoring logic.
Conclusion
We now recognize vectorized computation as a core way numerical work is expressed in Python’s scientific ecosystem. Applying operations to whole arrays, aggregating results, and avoiding explicit loops helps us write code that is both clearer and faster. That orientation is enough to start using vectorization confidently wherever numerical data appears.