NumPy Array Operations

· 6 min read · Updated March 13, 2026 · beginner
numpy scientific-computing arrays operations

Once you understand how to create NumPy arrays, the next step is learning how to work with them. NumPy provides a rich set of operations that let you transform, combine, and summarize your data efficiently. This tutorial covers the essential operations you’ll use daily.

Element-wise Operations

One of NumPy’s strengths is its ability to perform operations on entire arrays at once, without writing loops. These are called element-wise operations.

Arithmetic Operations

You can use standard arithmetic operators directly on arrays:

import numpy as np

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

# Addition
print(arr + 10)    # [11 12 13 14 15]

# Subtraction
print(arr - 5)     # [-4 -3 -2 -1  0]

# Multiplication
print(arr * 3)     # [ 3  6  9 12 15]

# Division
print(arr / 2)     # [0.5 1.  1.5 2.  2.5]

# Floor division
print(arr // 2)    # [0 1 1 2 2]

# Modulus
print(arr % 3)     # [1 2 0 1 2]

Each operation is applied to every element individually. This is far more readable than looping through each element manually.

Comparison Operations

Comparison operators also work element-wise, returning a boolean array:

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

print(arr > 3)      # [False False False  True  True]
print(arr <= 2)      # [ True  True False False False]
print(arr == 3)      # [False False  True False False]
print(arr != 2)      # [ True False  True  True  True]

These boolean arrays are useful for filtering data, which we’ll explore later.

Universal Functions (ufuncs)

NumPy’s universal functions extend beyond basic arithmetic. They provide optimized implementations of mathematical functions:

arr = np.array([0, np.pi/2, np.pi, 3*np.pi/2])

# Trigonometric functions
print(np.sin(arr))   # [ 0.  1.  0. -1.]
print(np.cos(arr))   # [ 1.  0. -1.  0.]
print(np.tan(arr))   # [ 0. inf  0. inf]

# Exponential and logarithm
print(np.exp(arr))   # [ 1.          4.81047738 23.14069263  ...]
print(np.log(arr))   # [      -inf 0.          1.14472989 ...]

The np.where function is particularly useful—it lets you apply conditions element-wise:

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

result = np.where(arr > 3, "big", "small")
print(result)  # ['small' 'small' 'small' 'big' 'big']

Broadcasting

Broadcasting is one of NumPy’s most powerful features. It lets you perform operations between arrays of different shapes.

Basic Broadcasting Rules

When operating on two arrays, NumPy compares their shapes from right to left. Two dimensions are compatible when they are equal, or one of them is 1:

# Adding a scalar to an array
arr = np.array([1, 2, 3, 4, 5])
print(arr + 1)  # [2 3 4 5 6]

# Adding a 1D array to a 2D 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]]

Practical Broadcasting Examples

Broadcasting saves you from manually expanding arrays:

# Normalizing data (subtract mean, divide by std)
data = np.array([[1, 2, 3], [4, 5, 6]])
mean = data.mean(axis=0)  # [2.5 3.5 4.5]
std = data.std(axis=0)    # [1.118 1.118 1.118]

normalized = (data - mean) / std
print(normalized)
# [[-1.336 -1.341 -1.341]
#  [ 1.336  1.341  1.341]]

This works because the 1D arrays (mean and std) are broadcast across the 2D array.

Aggregation Functions

Aggregations reduce an array to a single value. NumPy provides many built-in aggregations.

Basic Aggregations

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

print(arr.sum())     # 15
print(arr.mean())    # 3.0
print(arr.min())     # 1
print(arr.max())     # 5
print(arr.std())     # 1.4142135623730951
print(arr.var())     # 2.0
print(arr.prod())    # 120 (1*2*3*4*5)

Axis-based Aggregations

For multi-dimensional arrays, you can specify which axis to aggregate:

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

# Sum across rows (axis=0) - collapses rows
print(matrix.sum(axis=0))   # [5 7 9]

# Sum across columns (axis=1) - collapses columns  
print(matrix.sum(axis=1))   # [ 6 15]

# Mean along rows
print(matrix.mean(axis=0))  # [2.5 3.5 4.5]

Understanding axis is crucial. Axis 0 typically represents columns (going down), while axis 1 represents rows (going across).

Finding Indices

Sometimes you need the position of minimum or maximum values:

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

print(arr.argmin())  # 1 (index of minimum)
print(arr.argmax())  # 5 (index of maximum)

These functions are handy when you need to locate outliers or anomalies in your data.

Array Manipulation

Beyond mathematical operations, NumPy provides tools for reshaping and reorganizing arrays.

Reshaping

arr = np.arange(12)  # [0 1 2 ... 11]

# Reshape to 2D
print(arr.reshape(3, 4))
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

# Reshape to 3D
print(arr.reshape(2, 3, 2))
# [[[ 0  1]
#   [ 2  3]
#   [ 4  5]]
#  [[ 6  7]
#   [ 8  9]
#   [10 11]]]

# Use -1 for automatic dimension sizing
print(arr.reshape(3, -1))  # Same as (3, 4)

The total size must match when reshaping. Using -1 lets NumPy calculate that dimension automatically.

Transposing

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

print(matrix.T)
# [[1 4]
#  [2 5]
#  [3 6]]

# Or equivalently
print(matrix.transpose())

Stacking Arrays

Combine arrays horizontally or vertically:

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

# Vertical stack (add rows)
print(np.vstack([a, b]))
# [[1 2 3]
#  [4 5 6]]

# Horizontal stack (add columns)
print(np.column_stack([a, b]))
# [[1 4]
#  [2 5]
#  [3 6]]

Splitting Arrays

The inverse of stacking—split arrays into parts:

arr = np.arange(10)

# Split into 2 equal parts
print(np.split(arr, 2))
# [array([0, 1, 2, 3, 4]), array([5, 6, 7, 8, 9])]

# Split at specific indices
print(np.split(arr, [3, 7]))
# [array([0, 1, 2]), array([3, 4, 5, 6]), array([7, 8, 9])]

Boolean Indexing

Use boolean arrays to filter data:

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

# Find elements greater than 5
print(arr[arr > 5])  # [ 6  7  8  9 10]

# Combine conditions with & (and) | (or)
print(arr[(arr > 3) & (arr < 8)])  # [4 5 6 7]

# Use np.where for conditional selection
print(np.where(arr % 2 == 0, "even", "odd"))
# ['odd' 'even' 'odd' 'even' 'odd' 'even' 'odd' 'even' 'odd' 'even']

Conclusion

These array operations form the foundation of data manipulation in NumPy. Element-wise operations let you transform data without loops. Broadcasting handles mismatched shapes elegantly. Aggregations summarize data efficiently. And array manipulation gives you flexibility in organizing your data.

Master these operations, and you’ll find yourself rarely needing to write explicit loops when working with numerical data.

See Also