Data Science · Chapter 7 of 43

NumPy Basics

NumPy is Python's numerical computing library. Its core object is the ARRAY (`ndarray`) — much faster than lists for numeric work.

Most data-science libraries (pandas, scikit-learn) are built on top of NumPy.

Example 1 (python)
import numpy as np
a = np.array([1,2,3,4])
print(a * 2)
print(a.mean())
Output
[2 4 6 8]
2.5

Vectorised math + built-in stats.

Example 2 (python)
b = np.arange(1, 10).reshape(3, 3)
print(b)
Output
[[1 2 3]
 [4 5 6]
 [7 8 9]]

Shape and reshape arrays.

Key points

  • Core object is the ndarray.
  • Vectorised operations are fast.
  • Foundation for pandas & scikit-learn.
  • Learn shapes and broadcasting early.
💡 Note: Never loop over a NumPy array with Python for-loops when a vectorised operation exists — it's 10-100× slower.

📝 Quick Quiz

1. NumPy's core object is:

2. Compared to Python lists, NumPy is:

3. np.arange(1, 5) produces: