Create Arrays of Zeros in NumPy

While working on a data science project, I needed to initialize arrays with zeros before populating them with calculated values. NumPy’s zeros function became my go-to solution for this common task.

In this article, I’ll share everything you need to know about creating arrays of zeros in NumPy. We’ll explore different ways to use the zeros function, from basic usage to more advanced applications, and I’ll provide practical examples from my experience.

So let’s get in!

NumPy Zeros

NumPy zeros is a built-in function that creates a new array filled with zero values. The numpy.zeros() function is one of the most fundamental array creation routines in NumPy, allowing us to quickly initialize arrays of any shape and size.

Read Convert the DataFrame to a NumPy Array Without Index in Python

Basic Usage of NumPy Zeros

The most basic way to use Python NumPy zeros is to create a simple one-dimensional array. First, make sure you have NumPy imported:

import numpy as np

To create a 1D array of zeros:

# Create an array with 5 zeros
zeros_array = np.zeros(5)
print(zeros_array)

Output:

[0. 0. 0. 0. 0.]

You can see the output in the screenshot below.

numpy zeros

By default, NumPy creates an array of floating-point zeros (dtype=float64). That’s why you see decimal points in the output.

Check out Copy a NumPy Array to the Clipboard through Python

Create Multi-dimensional Arrays with Zeros

When working with data science projects, I often need arrays with more than one dimension. To create multi-dimensional arrays with zeros, simply pass a tuple with the desired dimensions:

# Create a 2D array (matrix) with 3 rows and 4 columns
matrix = np.zeros((3, 4))
print(matrix)

Output:

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

You can see the output in the screenshot below.

np zeros

Creating multi-dimensional arrays filled with zeros using np.zeros() is a quick and efficient way to initialize data structures for numerical and data science tasks.

Read NumPy Divide Array by Scalar in Python

Specify Data Types

One of the most effective features of NumPy zeros is the ability to specify the data type. This can save memory and improve performance when working with large datasets.

# Create an array of integer zeros
int_zeros = np.zeros(5, dtype=int)
print(int_zeros)

# Create an array of boolean zeros (False)
bool_zeros = np.zeros(5, dtype=bool)
print(bool_zeros)
# Create an array of unsigned 8-bit integers (for memory efficiency)
uint8_zeros = np.zeros(5, dtype=np.uint8)
print(uint8_zeros)

Output:

[0 0 0 0 0]
[False False False False False]
 [0 0 0 0 0]

You can see the output in the screenshot below.

np.zeros

I frequently use different data types depending on the situation:

  • bool for masks and conditions
  • int for counts and indices
  • float32 to save memory when working with large datasets
  • uint8 for image processing (pixel values)

Check out np.unit8 in Python

Use np.zeros_like()

The np.zeros_like() function creates an array of zeros with the same shape and type as a given array. I find this incredibly useful when I need to create a result array that matches an input array.

# Create a sample array
original = np.array([[1, 2, 3], [4, 5, 6]])

# Create a zeros array with the same shape and type
zeros_copy = np.zeros_like(original)
print(zeros_copy)
# Output:
# [[0 0 0]
#  [0 0 0]]

It’s especially helpful when preparing placeholder arrays for computations that mirror the structure of your input data.

Read NumPy Unique Function in Python

Practical Applications of Creating NumPy Zeros

Over the years, I’ve used NumPy zeros in countless projects. Here are some practical applications that demonstrate its utility:

1. Image Processing

When working with image data, initializing with zeros is often the first step:

# Create a blank canvas for drawing
canvas = np.zeros((600, 800, 3), dtype=np.uint8)

# Draw a white rectangle
x, y, width, height = 100, 200, 300, 150
canvas[y:y+height, x:x+width] = 255

# You could display this with matplotlib or OpenCV

np.zeros() provides a blank canvas that’s ideal for drawing or image manipulation in applications like OpenCV or matplotlib

2. Accumulate Statistics

When analyzing data, I often need to accumulate results:

# Analyzing sales data for 50 states over 12 months
states = 50
months = 12
total_sales = np.zeros(states)

# Simulate monthly sales data
monthly_sales = np.random.randint(10000, 50000, size=(states, months))

# Accumulate total for each state
for month in range(months):
    total_sales += monthly_sales[:, month]

print("Top 5 states by sales:", np.argsort(total_sales)[-5:])

Zero-initialized arrays are perfect for efficiently accumulating totals and statistics across large datasets.

3. Signal Processing

When working with signal processing, I use zeros to pad signals:

# Original signal
signal = np.array([1, 2, 3, 4, 5])

# Pad with zeros (zero padding is common in signal processing)
padded_signal = np.zeros(10)
padded_signal[:len(signal)] = signal

print(padded_signal)  # Output: [1. 2. 3. 4. 5. 0. 0. 0. 0. 0.]

Padding signals with zeros using np.zeros() is a common technique to align or extend signals for analysis or filtering.

Check out Create a 2D NumPy Array in Python

4. Machine Learning Feature Engineering

For feature engineering in ML, I often create empty feature matrices:

# Create a feature matrix for 1000 samples with 20 features
n_samples = 1000
n_features = 20
X = np.zeros((n_samples, n_features))

# Fill in features (this would normally be done with real data)
# Here we're just simulating with random data
X[:, 0] = np.random.normal(size=n_samples)  # First feature: normal distribution
X[:, 1] = np.random.uniform(size=n_samples)  # Second feature: uniform distribution

np.zeros() is essential for creating structured, empty feature matrices that can be populated with engineered data.

NumPy’s zeros function is a simple yet powerful tool in your Python data analysis toolkit. Whether you’re initializing arrays for data processing, creating masks for filtering, or preparing matrices for mathematical operations, np.zeros() provides a fast and memory-efficient solution.

I hope you found this article helpful

Check out some related tutorials:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.