Create a 1D / 2D Numpy Arrays of zeros or ones

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:

Advertisements
[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:

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]]

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top