Arrays & Vectorisation

ECON 3209 · Week 3, Lecture 1 · Kerala Agricultural University

Department of Development Economics, KAU

Autumn 2026

Learning Outcomes

By the end of this lecture, you will be able to:

  1. Create and inspect NumPy arrays using ndarray
  2. Index and slice arrays efficiently
  3. Perform element-wise vectorised operations
  4. Use boolean masks and broadcasting in economic examples
  5. Explain why vectorisation is valuable in econometrics

Why NumPy for Economics?

  • Economic data often comes as columns of numbers
  • Python lists are flexible but not ideal for heavy numerical work
  • NumPy stores numbers in efficient arrays called ndarray
  • It supports fast vectorised operations on entire datasets
  • Most scientific Python libraries are built on top of NumPy

In econometrics, NumPy lets us think in vectors and matrices rather than one observation at a time.

Creating Arrays

  • np.array() converts a Python list into an array
  • np.zeros() and np.ones() create standard arrays quickly
  • np.arange() is useful for evenly spaced integer sequences
  • NumPy arrays usually contain one numeric type throughout

Shape, Size, and Data Type

  • .shape tells us rows and columns
  • .size counts total elements
  • .ndim shows number of dimensions
  • .dtype tells us the numeric type such as int64 or float64
  • These properties matter when preparing data for models

Indexing and Slicing

  • NumPy uses zero-based indexing like Python lists
  • Slices use the pattern [start:stop]
  • The stop position is excluded
  • Indexing and slicing help isolate years, districts, or variables

Vectorised Arithmetic

  • Operations happen element by element
  • No explicit loop is needed
  • This is called vectorisation
  • It is faster and often easier to read than manual looping

Broadcasting

  • Broadcasting lets NumPy combine arrays of compatible shapes
  • A single number can be applied to every element of an array
  • This is useful for inflation adjustments, tax changes, or subsidies
  • NumPy expands smaller objects automatically when rules match
  • Understanding shapes prevents broadcasting errors

Boolean Masks

  • A boolean mask is an array of True and False
  • It filters values using economic rules
  • This is a powerful alternative to writing manual loops
  • We often combine masks when cleaning or subsetting data

Vectorised Growth Calculations

  • deposits[1:] means all but the first observation
  • deposits[:-1] means all but the last observation
  • Vectorisation lets us compute growth for all adjacent years at once
  • This pattern appears in finance, prices, and productivity analysis

Why Vectorisation Beats Manual Loops

Conceptual Benefits

  • Closer to mathematical notation
  • Less repetitive code
  • Easier to combine with linear algebra
  • Lower chance of indexing mistakes

Practical Benefits

  • Faster for large datasets
  • Works well with pandas and statsmodels
  • Easy to debug intermediate arrays
  • Standard in scientific Python workflows

Applied Example: Fertiliser Subsidy Shock

  • Here each district has its own subsidy rate
  • NumPy still handles the operation element by element
  • This is a natural way to model policy differences across observations
  • Arrays make policy simulation much easier

Exercise

Create a NumPy array called yields with values 3000, 3200, 2900, 3600, 3400.

  1. Add 200 to every value to represent improved irrigation.
  2. Create a boolean mask for yields above 3300.
  3. Print only the improved yields above 3300.

Summary

  • ✅ NumPy arrays store numerical data efficiently
  • ✅ Array attributes like .shape and .dtype describe structure
  • ✅ Vectorised operations work on whole arrays at once
  • ✅ Boolean masks make filtering elegant and fast
  • ✅ Broadcasting applies smaller objects across larger arrays
  • ✅ NumPy is the numerical foundation for econometrics in Python

Next Lecture

Linear Algebra with NumPy

  • We will extend arrays into vectors and matrices
  • You will learn matrix multiplication, solving systems, and eigenvalues
  • These tools connect directly to regression algebra