×

NumPy Array empty(), zeros() and ones()

The NumPy package has a lot of functionalities for array creation. One of which is array() function which we discussed earlier in depth. Now we check out other ways to create numpy arrays.

The following are some array creation routines to create a ndarray:

  • empty()
  • zeros()
  • ones()

empty() function

The empty() function of NumPy creates an uninitialized array having shape and dtype as given to the empty() function.

Syntax:

empty(shape, dtype=float, order='C', *, like=None)

The followings are the arguments of empty() function:

  • shape: Takes single integer input or tuple of integers as input(number of rows and columns).
  • dtype: Sets the data type of elements in the ndarray(default is float).
  • order: Store row-major(ā€˜Cā€™) or column-major(ā€˜Fā€™)order in memory for multi-dimensional data.

The parameter shape is a mandatory argument and dtype, order, and like are the optional arguments in empty() function.

We import the NumPy package using the import statement. Creating a 1-dimensional integer ndarray using empty() function.

import numpy as np
np_arr = np.empty(3, dtype=int) 
np_arr #other than Jupyter Notebook users, print(np_arr)
#Output:
array([1601724512, 1885431923, 1920229221])

The output shows that np_arr contains 3 random integer values(because the resultant array is uninitialized).

empty() function for creating 2-d Float ndarray:

np_arr = np.empty((2, 3)) 
np_arr #other than Jupyter Notebook users, print(np_arr)
#Output:
array([[1.03977794e-312, 9.54898106e-313, 1.01855798e-312],
          [1.10343781e-312, 9.76118064e-313, 1.90979621e-312]])

zeros() function

The zeros() function of NumPy creates a ndarray initialized with 0 values having shape and dtype as given as input.

Syntax:

zeros(shape, dtype=float, order='C', *, like=None)

The arguments of zeros() function are same as empty() function.

zeros() to create ndarray:

We create a 1-dimensional float ndarray by passing a single integer value(in our case 5 for 5 elements) as a shape to the zeros() function and no need to set dtype as the default of dtype is float.

np_arr = np.zeros(5) 
np_arr #other than Jupyter Notebook users, print(np_arr)
#Output:
array([0., 0., 0., 0., 0.])

zeros() for creating 2-d Complex ndarray:

np_arr = np.zeros((3, 3), dtype=complex) 
np_arr #other than Jupyter Notebook users, print(np_arr)
#Output:
array([[0.+0.j, 0.+0.j, 0.+0.j],
       [0.+0.j, 0.+0.j, 0.+0.j],
       [0.+0.j, 0.+0.j, 0.+0.j]])

ones() function:

The ones() function of NumPy creates a ndarray initialized with 1 having shape and dtype as given as input.

Syntax:

np.ones(shape, dtype=None, order='C', *, like=None)

The arguments of ones() function are the same as zeros() and empty() function. The only change is that the dtype default value is set to None.

ones() to create ndarray:

Creating a ndarray from tuple of integer values using ones() function.

np_arr = np.ones((2, 2), dtype=float) 
np_arr #other than Jupyter Notebook users, print(np_arr)
#Output:
array([[1., 1.],
          [1., 1.]])

ones() function for creating ndarray with custom dtype:

Creating a ndarray from a nested list using ones() function and set dtype using a list of tuples any value say ‘x’ having dtype set to int and for value ‘y’ having dtype float.

np_arr = np.ones((2, 2), dtype=[('x' , 'int'), ('y', 'float')]) 
np_arr #other than Jupyter Notebook users, print(np_arr)
#Output:
array([[(1, 1.), (1, 1.)],
       [(1, 1.), (1, 1.)]], dtype=[('x', '< i4'), ('y', ' < f8')])

Passing custom dtype parameter can also be done in zeros() function and empty() function.