Create a 1D / 2D Numpy Arrays of zeros or ones
Numpy array of 1s: In this article, we will learn to create array of various shapes where all the values are initialized with 0 & 1.
numpy.zeros() :
Numpy array ones: A function is provided by Python’s numpy module i.e. numpy.zeros()
which will create a numpy array of given shape and where all values are initialized with 0’s.
i.e.
numpy.zeros(shape, dtype=float, order='C')
Arguments:-
- shape : It denotes shape of the numpy array. It may be single int or sequence of int.
- dtype : It denotes data type of elements. Default value for dtype is float64.
- order : It denotes the order in which data is stored in a multi-dimension array. It is of two types
- ‘F’: Data will be stored Row major order
- ‘C’: Data will be stored in Column major order. Default value of order is ‘C’.
Let’s see the below 4 different types implementation with code
- Creating a flattened 1D numpy array filled with all zeros
- Creating a 2D numpy array with 4 rows & 3 columns, filled with 0’s
- Creating a flattened 1D numpy array filled with all Ones
- Creating a 2D numpy array with 3 rows & 3 columns, filled with 1’s
Creating a flattened 1D numpy array filled with all zeros :
We can create a flattened numpy array with all values as ‘0’.
import numpy as sc # create a 1D numpy array with values as 0 numarr = sc.zeros(5) print('Contents of the Flattened Numpy Array : ') print(numarr)
Output : Contents of the Flattened Numpy Array : [0. 0. 0. 0. 0.]
- numpy.zeros() & numpy.ones() | Create a numpy array of zeros or ones
- Create Numpy Array of different shapes & initialize with identical values using numpy.full() in Python
- Create an empty NumPy Array of given length or shape and data type in Python
Creating a 2D numpy array with 4 rows & 3 columns, filled with 0’s :
We can create a 2D numpy array by passing (4,3) as argument in numpy.zeros()
which will return all values as ‘0’.
import numpy as sc # create a 2D numpy array with values as 0 numarr = sc.zeros((4,3)) print('Contents of the 2D Numpy Array : ') print(numarr)
Output : Contents of the 2D Numpy Array : [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]
We know that default value of dtype is float64, let’s try to change the dtype to int64.
import numpy as sc # create a 2D numpy array with values as 0 which are of int data type numarr = sc.zeros((4,3), dtype=sc.int64) print('Contents of the 2D Numpy Array : ') print(numarr)
Output : Contents of the 2D Numpy Array : [[0 0 0] [0 0 0] [0 0 0] [0 0 0]]
numpy.ones() :
A function is provided by Python’s numpy module i.e. numpy.ones()
which will create a numpy array of given shape and where all values are initialized with 1’s.
i.e.
numpy.ones(shape, dtype=float, order='C')
Arguments:-
- shape : It denotes shape of the numpy array. It may be single int or sequence of int.
- dtype : It denotes data type of elements. Default value for dtype is float64.
- order : It denotes the order in which data is stored in a multi-dimension array. It is of two types
- ‘F’: Data will be stored Row major order
- ‘C’: Data will be stored in Column major order. Default value of order is ‘C’.
Creating a flattened 1D numpy array filled with all Ones :
We can make all values as 1 in a flattened array.
import numpy as sc # create a flattened 1D numpy array of size 5 where all values are 1 numarr = sc.ones(5) print('Contents of the Flattened Numpy Array : ') print(numarr)
Output : Contents of the Flattened Numpy Array : [ 1. 1. 1. 1. 1.]
Creating a 2D numpy array with 3 rows & 3 columns, filled with 1’s :
We can create a 2D numpy array by passing row and column as argument in numpy.ones()
where all the values are 1.
import numpy as sc # create a 2D numpy array with 3 rows & 4 columns with all values as 1 numarr = sc.ones((3,3)) print('Contents of the 2D Numpy Array : ') print(numarr) print('Data Type of the contents in given Array : ',numarr.dtype)
Output : Contents of the 2D Numpy Array : [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]] Data Type of the contents in given Array : float64
We know default type of dtype is float64, let’s try to change it to int64.
import numpy as sc # create a 2D numpy array with values as 1 which are of int data type numarr = sc.ones((4,3), dtype=sc.int64) print('Contents of the 2D Numpy Array : ') print(numarr)
Output : Contents of the 2D Numpy Array : [[1 1 1] [1 1 1] [1 1 1] [1 1 1]]