How to Initialize a NumPy Array with Zeros and Ones

Numpy is a popular Python library for data science focusing on linear algebra. In this article, you’ll learn how to initialize your NumPy array.

How to Initialize a NumPy Array with Zeros?

To initialize your NumPy array with zeros, use the function np.zeros(shape) where shape is a tuple that defines the shape of your desired array. For example, np.zeros((3,)) defines a one-dimensional array with three “0” elements, i.e., [0 0 0]. And np.zeros((2,2)) is a two-dimensional array with two rows and two columns, i.e., [[0 0] [0 0]].

Here are a number of examples:

>>> np.zeros((3,))
array([0., 0., 0.])
>>> np.zeros((10,))
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>>> np.zeros((2, 2))
array([[0., 0.],
       [0., 0.]])
>>> np.zeros((2, 3, 4))
array([[[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]],

       [[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]]])
  • The first tuple value of the shape tuple is the number of elements in axis 0.
  • The second tuple value of the shape tuple is the number of elements in axis 1.
  • The n-th tuple value of the shape tuple is the number of elements in axis n.

The resulting array is filled with zeros of type float. If you want to change this, you can set another data type as a second optional dtype argument, e.g., np.zeros((2, 2), dtype='numpy.int8') to initialize an array with integer zero values.

How to Initialize a NumPy Array with Ones?

To initialize your NumPy array with ones, use the function np.ones(shape) where shape is a tuple that defines the shape of your desired array. For example, np.ones((3,)) defines a one-dimensional array with three “1” elements, i.e., [1 1 1]. And np.ones((2,2)) is a two-dimensional array with two rows and two columns, i.e., [[1 1] [1 1]].

Here are a number of examples:

>>> np.ones((3,))
array([1., 1., 1.])
>>> np.ones((10,))
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
>>> np.ones((2, 2))
array([[1., 1.],
       [1., 1.]])
>>> np.ones((2, 3, 4))
array([[[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]],

       [[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]]])
  • The first tuple value of the shape tuple is the number of elements in axis 0.
  • The second tuple value of the shape tuple is the number of elements in axis 1.
  • The n-th tuple value of the shape tuple is the number of elements in axis n.

The resulting array is filled with “1.0” of type float. If you want to change this, you can set another data type as a second optional dtype argument, e.g., np.ones((2, 2), dtype='numpy.int8') to initialize an array with integer “1” values.

Let’s test your understanding of these concepts in an interactive NumPy puzzle, shall we?

Initialize NumPy Array — Puzzle

import numpy as np

n = 100 # dimensionality

W = np.zeros((n, n))
for i in range(len(W)):
    W[i][i] = 2

X = np.ones((n,n))

Y = W * X
print(int(Y[-1][-1]))

What is the output of this puzzle?

You can solve the puzzle on our interactive puzzle app here:

NumPy Puzzle

This puzzle performs a simple linear regression calculation. It tests your understanding of three NumPy concepts.

  • First, you can specify the shape of the array as a tuple (n,m) where n is the number of rows and m the number of columns.
  • Second, you can create new arrays of a specified shape using the functions ones() and zeros(). The initial values of such an array are 1s and 0s, respectively.
  • Third, you can do matrix multiplication using the intuitive multiplication operator '*'. Each cell (i,j) of the new matrix is the product of the row vector i of the first matrix with the column vector j of the second matrix.

As a result, we print the last element of the two-dimensional matrix Y (bottom-right).

Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)

Coffee Break NumPy