In this article we will discuss how to create a Numpy array of different shapes and initialized with 0 & 1.

## numpy.zeros()

Python’s Numpy module provides a function to create a numpy array of given shape & type and all values in it initialized with 0’s i.e.

numpy.zeros(shape, dtype=float, order='C')

*Arguments:*

**shape**: Shape of the numpy array. Single int or sequence of int.**dtype**: (Optional) Data type of elements. Default is float64.**order**: (Optional) Order in which data is stored in multi-dimension array i.e. in row major(‘F’) or column major (‘C’). Default is ‘C’.

Let’s see some examples,

### Create a flattened numpy array filled with all zeros

# create a 1D numpy array with 5 zeros's filled in it arr = np.zeros(5) print('Contents of the Numpy Array : ' , arr)

Output:

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

Here, in shape argument we passed 5. So, it returned a flattened numpy array of 5 zeros.

## Create a 2D numpy array with 5 rows & 6 columns, filled with 0’s

# create a 2D numpy array with 5 rows & 6 columns, filled with 0's arr = np.zeros((5, 6)) print('Contents of the Numpy Array : \n', arr) print('Data Type of elements in Array : ', arr.dtype)

Output:

### Frequently Asked:

- Check if 2D NumPy Array or Matrix is Symmetric
- Remove Array elements that are in Another Array – Python
- Convert a Pandas Series or Index to a NumPy array
- Python: Convert Matrix / 2D Numpy Array to a 1D Numpy Array

Contents of the Numpy 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.] [0. 0. 0. 0. 0. 0.]] Data Type of elements in Array : float64

Here we passed (5,6) as shape argument in numpy.zeros(), therefore it returned a 2D numpy array of 5 rows & 6 column with all zeros.

As default type was float64. Let’s see how to pass the data type int64 i.e.

# create a 2D numpy array with 5 rows & 6 columns filled with 0's and int data type arr = np.zeros((5, 6) , dtype=np.int64) print('Contents of the Numpy Array : \n', arr)

Output:

Contents of the Numpy 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] [0 0 0 0 0 0]]

It will create a 2D numpy array of ints filled with zeros.

## numpy.ones()

Python’s Numpy module provides a function to create a numpy array of given shape & type and all values in it initialized with 1’s i.e.

numpy.ones(shape, dtype=float, order='C')

*Arguments:*

**shape**: Shape of the numpy array. Single int or sequence of int.**dtype**: (Optional) Data type of elements. Default is float64.**order**: (Optional) Order in which data is stored in multi-dimension array i.e. in row major(‘F’) or column major (‘C’). Default is ‘C’.

Let’s see some examples,

### Create a flattened numpy array filled with all Ones

# create a 1D numpy array with 5 ones filled in it arr = np.ones(5) print('Contents of the Numpy Array : ' , arr)

Output:

[1. 1. 1. 1. 1.]

Here, in shape argument we passed 5. So, it returned a flattened numpy array of 5 zeros.

## Create a 2D numpy array with 3 rows & 4 columns, filled with 1’s

# create a 2D numpy array with 3 rows & 4 columns, filled with 1's arr = np.ones((3, 4)) print('Contents of the Numpy Array : \n', arr) print('Data Type of elements in Array : ', arr.dtype)

Output:

Contents of the Numpy Array : [[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]] Data Type of elements in Array : float64

Here we passed (3,4) as shape argument in numpy.ones(), therefore it returned a 2D numpy array of 3 rows & 4 column with all zeros.

As default type was float64. Let’s see how to pass the data type int64 i.e.

# create a 2D numpy array with 3 rows & 4 columns filled with 1's and int data type arr = np.zeros((3, 4) , dtype=np.int64) print('Contents of the Numpy Array : \n', arr)

Output:

Contents of the Numpy Array : [[1 1 1 1] [1 1 1 1] [1 1 1 1]]

It will create a 2D numpy array of ints filled with ones.

Complete example is as follows,

import numpy as np def main(): print("*** Create flattened numpy array filled with 0's using numpy.zeros() ***") # create a 1D numpy array with 5 zeros's filled in it arr = np.zeros(5) print('Contents of the Numpy Array : ' , arr) # create a 2D numpy array with 5 rows & 6 columns, filled with 0's arr = np.zeros((5, 6)) print('Contents of the Numpy Array : \n', arr) print('Data Type of elements in Array : ', arr.dtype) # create a 2D numpy array with 5 rows & 6 columns filled with 0's and int data type arr = np.zeros((5, 6) , dtype=np.int64) print('Contents of the Numpy Array : \n', arr) print("*** Create numpy array filled with 1's using numpy.ones() ***") # create a 1D numpy array with 7 one's filled in it arr = np.ones(5) print('Contents of the Numpy Array : ', arr) # create a 2D numpy array with 3 rows & 4 columns, filled with 1's arr = np.ones((3, 4)) print('Contents of the Numpy Array : \n', arr) print('Data Type of elements in Array : ', arr.dtype) # create a 2D numpy array with 5 rows & 5 columns, filled with 1's & int data type arr = np.ones((3, 4), dtype=np.int64) print('Contents of the Numpy Array : \n', arr) if __name__ == '__main__': main()

Output

*** Create flattened numpy array filled with 0's using numpy.zeros() *** Contents of the Numpy Array : [0. 0. 0. 0. 0.] Contents of the Numpy 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.] [0. 0. 0. 0. 0. 0.]] Data Type of elements in Array : float64 Contents of the Numpy 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] [0 0 0 0 0 0]] *** Create numpy array filled with 1's using numpy.ones() *** Contents of the Numpy Array : [1. 1. 1. 1. 1.] Contents of the Numpy Array : [[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]] Data Type of elements in Array : float64 Contents of the Numpy Array : [[1 1 1 1] [1 1 1 1] [1 1 1 1]]