NumPy: Getting Started with NumPy

NumPy is the foundation of Python scientific computing. Whether you're doing machine learning, data analysis, or engineering simulation, almost every numerical library builds on NumPy. This chapter starts with the performance pain points of Python lists and reveals how NumPy achieves 160x speedup — your first step into numerical computing.

⚠️ Note: The code below needs to run in a local Python environment.

1. What You'll Learn



2. Speed-Up Journey with Million-Level Data

(1) The Problem: Alice's List Loop

Alice is a data analyst who needs to square 1 million random numbers. She writes the most "natural" Python code — a for loop processing list elements one by one.

▶ Example

TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

: Squaring a list (Difficulty ⭐)

PYTHON
import time
import random

size = 1_000_000  # 1 million numbers
data = [random.random() for _ in range(size)]

start = time.time()
result = [x * x for x in data]
elapsed = time.time() - start

print(f"List comprehension: {elapsed:.4f} seconds")
# Typical output: List comprehension: 0.8000 seconds
TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

Alice's code runs in about 0.8 seconds. That doesn't seem bad — until she needs to do the same operation on hundreds of millions of data points, where the wait becomes unbearable.

(2) The Solution: Bob's NumPy Vectorization

Bob changes only two lines: replacing the list with np.array and the loop with a single vectorized multiplication.

▶ Example

TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

: NumPy squaring (Difficulty ⭐⭐)

PYTHON
import time
import numpy as np

size = 1_000_000  # 1 million numbers
data = np.random.random(size)

start = time.time()
result = data * data  # vectorized operation
elapsed = time.time() - start

print(f"NumPy vectorized: {elapsed:.4f} seconds")
# Typical output: NumPy vectorized: 0.0050 seconds
TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

The same 1 million numbers take only about 5 milliseconds with NumPy — roughly 160x faster.

(3) The Payoff: What Makes It 160x Faster

NumPy's speed isn't about "Python being faster" — it's about offloading computation to C and using contiguous memory:

This is NumPy's core philosophy — Python's convenience with C's speed.



3. Why Python Lists Struggle with Numbers

(1) Loop Overhead: Interpreted Execution

Python is dynamically typed. Each loop iteration goes through: type check → method lookup → execute operation → box the result. For numerical work, these extra steps are pure waste.

PYTHON
# Each iteration: type check + method lookup + boxing
result = []
for x in data:          # Python loop overhead per iteration
    result.append(x * x)  # type check, method dispatch, result boxing
TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

Compared to compiled C machine code, a single Python loop iteration can be tens of times more expensive than the actual multiplication.

(2) Type Overhead: Every Element Is a Full Object

A Python list doesn't store raw numbers — it stores PyObject pointers. The integer 42 takes 28 bytes in Python, but only 4 bytes in C.



4. NumPy's Core Advantages

(1) What Is ndarray

ndarray (N-dimensional array) is NumPy's core data structure. It is:

(2) ndarray vs Python List

Feature Python list NumPy ndarray
Element type Any mix Homogeneous (single dtype)
Memory layout Pointer array, scattered elements Contiguous memory block
Single int memory 28 bytes (PyObject) 8 bytes (int64)
Bulk operations List comprehension / for loop Vectorized, single statement
Broadcasting Not supported Automatic dimension expansion
Slice view Returns a copy Returns a view by default (zero-copy)
Multi-dimensional Nested lists (irregular) Native N-dimensions, shape attribute
Under the hood CPython interpreter C / Fortran compiled code

▶ Example

TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

: Memory comparison (Difficulty ⭐)

PYTHON
import sys
import numpy as np

size = 1_000_000  # 1 million integers

# Python list
py_list = list(range(size))
list_bytes = sys.getsizeof(py_list) + sum(sys.getsizeof(x) for x in py_list[:1000]) * size // 1000
print(f"Python list: ~{list_bytes / 1024 / 1024:.1f} MB")

# NumPy ndarray
np_array = np.arange(size, dtype=np.int64)
array_bytes = np_array.nbytes
print(f"NumPy ndarray: {array_bytes / 1024 / 1024:.1f} MB")

print(f"Ratio: {list_bytes / array_bytes:.1f}x more memory for list")
# Typical output:
# Python list: ~44.7 MB
# NumPy ndarray: 7.6 MB
# Ratio: 5.9x more memory for list
TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

A simple int64 ndarray uses about 6x less memory than a Python list of the same size — because ndarray stores raw values, not full Python objects.

(3) A First Look at Broadcasting

"Broadcasting" lets arrays of different shapes operate directly together, without manually expanding dimensions.

▶ Example

TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

: Array operations vs list comprehension (Difficulty ⭐⭐)

PYTHON
import numpy as np

# Task: add 100 to every element and multiply by 2
data = [1, 2, 3, 4, 5]

# --- Python list ---
result_list = [(x + 100) * 2 for x in data]
print(result_list)
# [202, 204, 206, 208, 210]

# --- NumPy ndarray ---
arr = np.array(data)
result_np = (arr + 100) * 2  # broadcasting + vectorization
print(result_np)
# [202 204 206 208 210]

# With a 2D array and 1D array
matrix = np.array([[1, 2, 3],
                   [4, 5, 6]])
row = np.array([10, 20, 30])
print(matrix + row)
# [[11 22 33]
#  [14 25 36]]
TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

A list comprehension needs to spell out the loop logic; NumPy just needs one mathematical expression — code is the formula.



5. NumPy Ecosystem Overview

(1) The NumPy Ecosystem

NumPy is the foundation of the entire Python scientific computing ecosystem. Almost every major library depends on it:

100%
mindmap
  root((NumPy Ecosystem))
    Data Science
      Pandas
      Polars
    Machine Learning
      Scikit-learn
      TensorFlow
      PyTorch
    Scientific Computing
      SciPy
      Matplotlib
      SymPy
    Deep Learning Frameworks
      Keras
      JAX
      MXNet
    Specialized Domains
      scikit-image
      NetworkX
      AstroPy
TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

(2) Ecosystem Reference Table

Library Domain Why It Depends on NumPy
Pandas Data analysis DataFrame stores column data as ndarray under the hood
Scikit-learn Machine learning Model inputs and outputs are ndarray
SciPy Scientific computing Provides linear algebra / optimization / integration on top of ndarray
Matplotlib Visualization Plot data sources are ndarray
TensorFlow Deep learning Tensor concept originates from ndarray, convertible
PyTorch Deep learning Tensor and ndarray are interchangeable
Polars Data analysis Columnar storage, still NumPy-compatible

(3) When to Use NumPy vs Pure Python

Scenario Pure Python NumPy Recommendation
A few dozen simple operations Good enough Slightly faster Pure Python
10K+ bulk operations Slow 10~100x faster NumPy
Matrix multiplication Nested loops, very slow BLAS-backed, very fast NumPy
Solving linear equations Impractical to write manually np.linalg.solve NumPy
String processing Flexible Not its strength Pure Python
Mixed-type data Lists handle it naturally Requires structured arrays Depends


6. Installation and Your First Program

(1) Installation Methods

Method Command When to Use
pip pip install numpy General, simplest
conda conda install numpy Anaconda / Miniconda environments
System package apt install python3-numpy Linux system-wide install
From source python setup.py build Custom BLAS backend needed

Beginners should use pip install numpy. Anaconda users can use conda install numpy.

▶ Example

TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

: Verify installation (Difficulty ⭐)

BASH
# Install
pip install numpy

# Verify
python -c "import numpy as np; print(np.__version__)"
# Output (example): 2.1.0
TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

(2) Creating Your First ndarray

▶ Example

TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

: Creating arrays with np.array (Difficulty ⭐)

PYTHON
import numpy as np

# From a Python list
a = np.array([1, 2, 3, 4, 5])
print(type(a))   # <class 'numpy.ndarray'>
print(a.dtype)   # int64
print(a.shape)   # (5,)

# From nested list (2D)
b = np.array([[1, 2, 3],
              [4, 5, 6]])
print(b.shape)   # (2, 3)
print(b.ndim)    # 2

# Specify dtype explicitly
c = np.array([1, 2, 3], dtype=np.float32)
print(c.dtype)   # float32
TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

(3) Checking Version and Configuration

▶ Example

TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

: Checking version and config (Difficulty ⭐)

PYTHON
import numpy as np

print(f"NumPy version: {np.__version__}")

# Show build configuration (BLAS, LAPACK, etc.)
np.show_config()
# Output includes: BLAS, LAPACK backend info,
# which determines computation performance
TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

The BLAS/LAPACK backend shown by np.show_config() determines how fast your NumPy matrix operations run — performance differences between OpenBLAS, MKL, and other backends can be several times.



7. Hands-On: Mean and Standard Deviation of 10M Numbers

Computing the mean and standard deviation of 10 million floating-point numbers — one of the most common data analysis tasks. We'll implement it in both pure Python and NumPy, comparing code size, speed, and memory.

▶ Example

TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.

: Comprehensive comparison — list vs NumPy (Difficulty ⭐⭐⭐)

PYTHON
import time
import math
import random
import numpy as np
import sys

size = 10_000_000  # 10 million

# ========== Pure Python ==========
py_data = [random.random() for _ in range(size)]

t0 = time.time()
mean_py = sum(py_data) / len(py_data)
var_py = sum((x - mean_py) ** 2 for x in py_data) / len(py_data)
std_py = math.sqrt(var_py)
elapsed_py = time.time() - t0

# Memory estimate for list
mem_py = sys.getsizeof(py_data) + size * 24  # ~24 bytes per float object

# ========== NumPy ==========
np_data = np.random.random(size)

t0 = time.time()
mean_np = np.mean(np_data)
std_np = np.std(np_data)
elapsed_np = time.time() - t0

# Memory for ndarray
mem_np = np_data.nbytes

# ========== Results ==========
print(f"--- Pure Python ---")
print(f"  Mean: {mean_py:.6f}  Std: {std_py:.6f}")
print(f"  Time: {elapsed_py:.3f}s")
print(f"  Memory: ~{mem_py / 1024 / 1024:.0f} MB")

print(f"--- NumPy ---")
print(f"  Mean: {mean_np:.6f}  Std: {std_np:.6f}")
print(f"  Time: {elapsed_np:.3f}s")
print(f"  Memory: {mem_np / 1024 / 1024:.1f} MB")

print(f"--- Comparison ---")
print(f"  Speedup: {elapsed_py / elapsed_np:.1f}x")
print(f"  Memory saving: {mem_py / mem_np:.1f}x")
# Typical output:
# --- Pure Python ---
#   Mean: 0.500032  Std: 0.288675
#   Time: 3.200s
#   Memory: ~305 MB
# --- NumPy ---
#   Mean: 0.500032  Std: 0.288675
#   Time: 0.030s
#   Memory: 76.3 MB
# --- Comparison ---
#   Speedup: 106.7x
#   Memory saving: 4.0x
TEXT
> Output: Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.
Metric Pure Python NumPy Comparison
Lines of code (core logic) 4 lines 2 lines NumPy uses 50% fewer
Execution time ~3.2s ~0.03s ~100x faster
Memory usage ~305 MB ~76 MB 4x less

Bottom line: The larger your data, the more NumPy shines. At 10 million data points, NumPy crushes pure Python in both speed and memory.



❓ FAQ

Q How is NumPy related to Python?
A NumPy is a third-party library, not part of the Python standard library. Its core computation engine is written in C and exposed through a Python interface, giving you near-C speeds from Python.
Q Why is NumPy so much faster than lists?
A Three reasons — ① C-level implementation bypasses the Python interpreter overhead; ② contiguous memory layout maximizes CPU cache efficiency; ③ vectorized operations process the entire array in one instruction, no element-by-element loop.
Q Do I need to learn C to use NumPy?
A No. NumPy's design goal is to let Python users enjoy C-level speed with Python syntax. You only need C if you want to contribute low-level code or write custom C extensions for NumPy.
Q Can NumPy replace Excel?
A For numerical computation and batch processing, NumPy is far more powerful and efficient than Excel. But NumPy doesn't offer a spreadsheet UI, drag-and-drop charts, or interactive filtering — you'd typically pair it with Pandas (tabular data) and Matplotlib (visualization).
Q What's the difference between NumPy 2.x and 1.x?
A NumPy 2.0 (released in 2024) mainly improved C API stability, added some string operation APIs, and enhanced the dtype system. For everyday Python users, the API changes are minor — most 1.x code migrates seamlessly. New projects should use 2.x.
Q What's the difference between ndarray and Python's array module?
A Python's built-in array module only supports 1D homogeneous arrays with no broadcasting, linear algebra, or FFT capabilities. NumPy's ndarray supports multi-dimensional data, broadcasting, and a rich set of mathematical functions — they're in completely different leagues.

📖 Summary



📝 Exercises

  1. Beginner (Difficulty ⭐): Install NumPy and run import numpy as np; print(np.__version__). Make sure the version is ≥ 1.24. Record the output.

  2. Intermediate (Difficulty ⭐⭐): Create a list and an ndarray, each containing 5 million random numbers. Use list comprehension and NumPy vectorization to compute the square of each number plus 10. Time both approaches with time.time() and compare. Record the speed ratio.

  3. Advanced (Difficulty ⭐⭐⭐): Run np.show_config() and identify the BLAS backend your NumPy uses (OpenBLAS / MKL / other). Research how this backend affects matrix operation performance and write a brief explanation.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏