Introduction to NumPy

Lukman J Aliyu

Arewa Data Science Academy

Welcome!

  • Topic: Introduction to NumPy
  • Duration: 1 hour
  • Audience: Beginner to intermediate Python users

Objectives

By the end of this lecture, you will:

  • Understand why NumPy is essential for numerical computing
  • Know the limitations of Python lists
  • Learn to use NumPy arrays
  • Perform vectorized operations
  • Briefly contrast with Julia’s approach to vectorization

Why Not Pure Python Lists?

# Element-wise addition with lists
x = [1, 2, 3]
y = [4, 5, 6]
z = [a + b for a, b in zip(x, y)]
print(z)
[5, 7, 9]
  • List comprehensions can get messy
  • No type enforcement
  • Poor performance for large-scale data

Why NumPy?

  • Multidimensional array object
  • Vectorized operations
  • Broadcasting
  • Memory efficiency and performance
  • Backbone of scientific Python

Importing NumPy

import numpy as np

Creating Arrays

print(f"using np.array: {np.array([1, 2, 3])}")
print(f"Initiating using np.zeros: {np.zeros((2, 3))}")
print(f"using np.ones: {np.ones(5)}")
print(f"Initiating an identity matrix: {np.eye(3)}")
print(f"using np.arange: {np.arange(0, 10, 2)}")
print(f"using np.linspace: {np.linspace(0, 1, 5)}")
using np.array: [1 2 3]
Initiating using np.zeros: [[0. 0. 0.]
 [0. 0. 0.]]
using np.ones: [1. 1. 1. 1. 1.]
Initiating an identity matrix: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
using np.arange: [0 2 4 6 8]
using np.linspace: [0.   0.25 0.5  0.75 1.  ]

Array Attributes

x = np.array([[1, 2], [3, 4]])
print(x.shape)      
print(x.ndim)       
print(x.dtype)     
(2, 2)
2
int64

Vectorized Operations

x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
print(x + y)
print(x * y)
print(x ** 2)
[5 7 9]
[ 4 10 18]
[1 4 9]

Broadcasting

x = np.array([[1], [2], [3]])
y = np.array([10, 20, 30])
print(x + y)
[[11 21 31]
 [12 22 32]
 [13 23 33]]

Comparison with Julia

x = [1, 2, 3]
y = [4, 5, 6]
z = x .+ y
println(z)
  • Julia supports dot syntax for element-wise ops
  • NumPy uses vectorized ufuncs

Indexing and Slicing

x = np.array([[1, 2, 3], [4, 5, 6]])
print(x[0, 1])  
print(x[:, 1]) 
print(x[1, :])  
2
[2 5]
[4 5 6]

Boolean Indexing

x = np.array([1, 2, 3, 4, 5])
print(x[x > 2])
[3 4 5]

Aggregations

x = np.array([[1, 2], [3, 4]])
print(x.sum())
print(x.mean())
print(x.max(axis=0))
10
2.5
[3 4]

Linear Algebra

x = np.array([[1, 2], [3, 4]])
y = np.linalg.inv(x)
print(y)
print(np.dot(x, y))
[[-2.   1. ]
 [ 1.5 -0.5]]
[[1.0000000e+00 0.0000000e+00]
 [8.8817842e-16 1.0000000e+00]]

Random Numbers

np.random.seed(0)
print(np.random.rand(3))
print(np.random.randn(2, 2))
print(np.random.randint(0, 10, size=5))
[0.5488135  0.71518937 0.60276338]
[[-2.2683282   1.33354538]
 [-0.84272405  1.96992445]]
[6 8 8 1 6]

Summary

  • NumPy arrays are fast, efficient, and powerful
  • Vectorization replaces explicit loops
  • Broadcasting makes math simpler
  • Comparisons with Julia show similar design patterns

Try it Yourself!

  • Replace Python list code with NumPy arrays
  • Experiment with slicing, broadcasting, and aggregation

Questions?

Ask away or let’s do a few exercises together!

Thank You