Skip to main content

Introduction

NumPy (Numerical Python) is the main package for scientific computing in Python. It performs a wide variety of advanced mathematical operations with high efficiency. In this guide, you’ll learn several key NumPy functions including creating arrays, slicing, indexing, reshaping, and stacking.
NumPy arrays are faster and more compact than Python lists, making them essential for large-scale mathematical operations. Check out the official NumPy documentation for comprehensive details.

Getting Started

First, import NumPy to load its functions:
import numpy as np

Why Use NumPy Arrays?

Arrays are one of the core data structures of the NumPy library. You can think of them as a grid of values, all of the same type. While Python lists are convenient for storing different data types, NumPy arrays offer significant advantages:

Performance

NumPy arrays are much faster than Python lists, especially for large datasets

Memory Efficiency

Arrays take up less space and process data more efficiently

Built-in Functions

Extensive API with mathematical operations requiring only a few lines of code

Uniform Type

All elements are of the same type, enabling optimized operations

Creating NumPy Arrays

Basic Array Creation

The simplest way to create an array is using the array() function:
# Create a 1-D array
a = np.array([1, 2, 3])
print(a)
# Output: [1 2 3]

# Create a 1-D array with two elements
one_dimensional_arr = np.array([10, 12])
print(one_dimensional_arr)
# Output: [10 12]

Using np.arange()

Create arrays with evenly spaced values within a given interval:
# Array starting from 0 with 3 integers
b = np.arange(3)
print(b)
# Output: [0 1 2]

# Array from 1 to 20, incremented by 3
c = np.arange(1, 20, 3)
print(c)
# Output: [ 1  4  7 10 13 16 19]

# Specify integer data type
c_int = np.arange(1, 20, 3, dtype=int)
print(c_int)

Using np.linspace()

Create arrays with a specified number of evenly spaced values:
# 5 evenly spaced values from 0 to 100
lin_spaced_arr = np.linspace(0, 100, 5)
print(lin_spaced_arr)
# Output: [  0.  25.  50.  75. 100.]

# Convert to integers
lin_spaced_arr_int = np.linspace(0, 100, 5, dtype=int)
print(lin_spaced_arr_int)
# Output: [  0  25  50  75 100]
Use dtype parameter to specify the data type. Common types include int, float, and np.float64. The default for np.linspace() is floating point.

Data Types

NumPy supports various data types:
# Float array
b_float = np.arange(3, dtype=float)
print(b_float)
# Output: [0. 1. 2.]

# Character array
char_arr = np.array(['Welcome to Math for ML!'])
print(char_arr)
print(char_arr.dtype)
# Output: <U23 (23-character unicode string)

Built-in Array Functions

NumPy provides convenient functions to create special arrays:
# Array of ones
ones_arr = np.ones(3)
print(ones_arr)
# Output: [1. 1. 1.]

Multidimensional Arrays

Creating 2-D Arrays

Multidimensional arrays have more than one column:
# Create a 2-D array directly
two_dim_arr = np.array([[1, 2, 3], [4, 5, 6]])
print(two_dim_arr)
# Output:
# [[1 2 3]
#  [4 5 6]]

Reshaping Arrays

Convert a 1-D array into a multidimensional array:
# 1-D array
one_dim_arr = np.array([1, 2, 3, 4, 5, 6])

# Reshape to 2x3 array
multi_dim_arr = np.reshape(one_dim_arr, (2, 3))
print(multi_dim_arr)
# Output:
# [[1 2 3]
#  [4 5 6]]

Array Attributes

Access important array properties:
# Number of dimensions
print(multi_dim_arr.ndim)  # Output: 2

# Shape (rows, columns)
print(multi_dim_arr.shape)  # Output: (2, 3)

# Total number of elements
print(multi_dim_arr.size)  # Output: 6
  • ndarray.ndim - Number of dimensions
  • ndarray.shape - Tuple showing dimensions
  • ndarray.size - Total number of elements

Array Math Operations

NumPy enables fast element-wise operations:
arr_1 = np.array([2, 4, 6])
arr_2 = np.array([1, 3, 5])

# Addition
addition = arr_1 + arr_2
print(addition)  # Output: [3 7 11]

# Subtraction
subtraction = arr_1 - arr_2
print(subtraction)  # Output: [1 1 1]

# Element-wise multiplication
multiplication = arr_1 * arr_2
print(multiplication)  # Output: [ 2 12 30]

Broadcasting

Perform operations between arrays and scalars:
# Convert miles to kilometers (1 mile = 1.6 km)
vector = np.array([1, 2])
kilometers = vector * 1.6
print(kilometers)  # Output: [1.6 3.2]
Broadcasting allows operations on arrays of different shapes. NumPy automatically expands the smaller value to match the larger array’s shape.

Indexing and Slicing

Indexing 1-D Arrays

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

# Select third element (counting starts from 0)
print(a[2])  # Output: 3

# Select first element
print(a[0])  # Output: 1

Indexing 2-D Arrays

two_dim = np.array([[1, 2, 3],
                    [4, 5, 6], 
                    [7, 8, 9]])

# Select element at row 2, column 1
print(two_dim[2][1])   # Output: 8
print(two_dim[2, 1])   # Output: 8 (preferred syntax)

Slicing Syntax

The slice notation is: array[start:end:step]
1

Start Index

If omitted, defaults to 0 (beginning of array)
2

End Index

If omitted, defaults to length of array (end-exclusive)
3

Step Size

If omitted, defaults to 1
a = np.array([1, 2, 3, 4, 5])

# Get elements from index 1 to 3
sliced_arr = a[1:4]
print(sliced_arr)  # Output: [2 3 4]

# Get first three elements
sliced_arr = a[:3]
print(sliced_arr)  # Output: [1 2 3]

# Get last three elements
sliced_arr = a[2:]
print(sliced_arr)  # Output: [3 4 5]

# Get every second element
sliced_arr = a[::2]
print(sliced_arr)  # Output: [1 3 5]

Slicing 2-D Arrays

two_dim = np.array([[1, 2, 3],
                    [4, 5, 6], 
                    [7, 8, 9]])

# Get first two rows
sliced_arr_1 = two_dim[0:2]
print(sliced_arr_1)
# Output:
# [[1 2 3]
#  [4 5 6]]

# Get all rows, second column
sliced_two_dim_cols = two_dim[:, 1]
print(sliced_two_dim_cols)  # Output: [2 5 8]

Stacking Arrays

Join multiple arrays horizontally or vertically:
a1 = np.array([[1, 1], 
               [2, 2]])
a2 = np.array([[3, 3],
               [4, 4]])

Vertical Stacking

vert_stack = np.vstack((a1, a2))
print(vert_stack)
# Output:
# [[1 1]
#  [2 2]
#  [3 3]
#  [4 4]]

Horizontal Stacking

horz_stack = np.hstack((a1, a2))
print(horz_stack)
# Output:
# [[1 1 3 3]
#  [2 2 4 4]]

Splitting Arrays

# Split horizontally into 2 arrays
horz_split_two = np.hsplit(horz_stack, 2)
print(horz_split_two)

# Split vertically into 2 arrays
vert_split_two = np.vsplit(vert_stack, 2)
print(vert_split_two)
Use np.hsplit() and np.vsplit() to split arrays into smaller pieces. You can specify the number of equal sections or the indices where splits should occur.

Summary

You’ve learned the fundamentals of NumPy arrays:
  • Creating arrays with various methods
  • Understanding array attributes and data types
  • Performing mathematical operations
  • Indexing and slicing for data access
  • Stacking and splitting arrays

Next: Linear Systems

Learn how to represent and solve systems of linear equations using matrices

Build docs developers (and LLMs) love