Data Science with Python: From Zero to Insights
Chapter 2 / 8· 20 min read· 0 cards

NumPy: Fast Numerical Computing

Arrays and vectorized operations — the high-speed foundation that pandas and ML are built on.

Why NumPy exists

Python lists are flexible but slow for heavy number-crunching. Data science deals with thousands or millions of numbers, so it needs something faster. That's NumPy (Numerical Python) — a library providing a special array type that's vastly faster than lists for numerical work, plus a huge set of mathematical operations. Nearly every data science tool, including pandas and scikit-learn, is built on top of NumPy. Understanding it gives you the foundation for everything else.

import numpy as np   # the universal convention: import numpy as np

# Create an array from a list
arr = np.array([1, 2, 3, 4, 5])
print(arr)          # [1 2 3 4 5]
print(type(arr))    # <class 'numpy.ndarray'>

The convention is always import numpy as np — every data scientist writes it this way, so do the same. A NumPy array (ndarray) looks like a list but is far more powerful for maths.


The superpower: vectorized operations

Here's what makes NumPy special. With a normal list, doing maths on every element requires a loop. With a NumPy array, you operate on the whole array at once — no loop needed. This is called vectorization, and it's both cleaner and dramatically faster:

arr = np.array([1, 2, 3, 4, 5])

# Maths applies to EVERY element automatically — no loop!
print(arr * 2)      # [ 2  4  6  8 10]
print(arr + 10)     # [11 12 13 14 15]
print(arr ** 2)     # [ 1  4  9 16 25]

# Operate between two arrays element-by-element
prices = np.array([100, 200, 300])
quantities = np.array([2, 1, 3])
print(prices * quantities)   # [200 200 900] -- total per item

Look how arr * 2 doubles every element in one clean expression. With a Python list, [1,2,3] * 2 would just repeat the list — completely different. NumPy's vectorized maths is the heart of fast data science: you describe the operation once, and it applies to all the data at blazing speed.


Useful array operations

NumPy arrays come with a rich set of built-in computations, all fast and easy:

data = np.array([88, 72, 95, 60, 100])

print(data.sum())     # 415  -- total
print(data.mean())    # 83.0 -- average
print(data.max())     # 100  -- highest
print(data.min())     # 60   -- lowest
print(data.std())     # standard deviation (spread of the data)
print(len(data))      # 5    -- how many elements

These one-word methods — sum, mean, max, min, std — instantly summarise an array. In data science you constantly compute averages and totals over large datasets, and NumPy makes it effortless.


Boolean indexing: filtering data

One of NumPy's most powerful features is filtering with conditions. You can select exactly the elements that meet a criterion — a preview of how you'll filter data tables later:

scores = np.array([88, 45, 95, 30, 72, 60])

# A condition produces an array of True/False
print(scores >= 60)        # [ True False  True False  True  True]

# Use that to keep only the passing scores
passing = scores[scores >= 60]
print(passing)             # [88 95 72 60]

# Count how many passed
print((scores >= 60).sum())  # 4  -- True counts as 1

The expression scores >= 60 creates a "mask" of True/False values, and scores[mask] keeps only the elements where it's True. This pattern — filter data by a condition — is fundamental, and you'll use it constantly in pandas with real datasets.


2D arrays: rows and columns

Real data is often a table — rows and columns. NumPy handles 2D arrays naturally, which is a stepping stone to the data tables you'll work with in pandas:

# A 2D array (3 rows, 3 columns)
matrix = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

print(matrix.shape)     # (3, 3) -- 3 rows, 3 columns
print(matrix[0])        # [1 2 3] -- first row
print(matrix[1, 2])     # 6 -- row 1, column 2
print(matrix.sum(axis=0))  # [12 15 18] -- sum each column
print(matrix.mean(axis=1)) # [2. 5. 8.] -- average each row

The axis idea is important: axis=0 works down the columns, axis=1 works across the rows. This row/column thinking is exactly what you need for tables of data. NumPy gives you the fast numerical engine; in the next chapter we add pandas, which puts a friendly, labelled table on top of it — the tool you'll use more than any other.

Reading mode · scroll to read at your own pace

Finished "NumPy: Fast Numerical Computing"?

Mark this chapter complete so you can pick up exactly where you left off. Your progress saves locally — sign in to sync across devices.

Was this chapter clear?

Try it yourself — open the Code Playground15+ languages — Python, JavaScript, Java, C++, SQL & more — full IDE-style editor, instant run. Your code is auto-saved per language.