Creating Special Numpy Arrays in Python

Creating Special Numpy Arrays in Python

Table of Contents

In the last tutorial, we gave an introduction into Numpy arrays. Recall that a Numpy array can be created manually by calling the numpy.array() method and passing a list as an argument. But there may be situations where you may want to create a special type of arrays such as an array populated with only zeroes or ones or an identity array. You do not have to fill the arrays manually using the numpy.array() method. There are methods in Numpy that allow you to create such arrays right out of the gate. 

In this tutorial, you will learn how to create arrays and identity arrays, an array filled with zeros, and other special arrays. Let’s get started. 

Creating an Array filled with Zeros

To create a filled with zeros, the zeros() method is called. The method takes the shape of the array to be created as a required parameter. 

import numpy as np
print(np.zeros(5))
Output:
[0. 0. 0. 0. 0.]

If you want to create a 2-dimensional array, you pass the shape of the array as a tuple. Let’s say we want to create a 4 by 3 array filled with zeros

import numpy as np
print(np.zeros((3, 4)))
Output:
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
As seen, a 2-dimensional array, with a shape (4, 3) is created. 

Creating an Array filled with Ones

To create an array filled with ones, the ones() method is called. Just like the numpy.zeros() method, the numpy.ones() method takes the shape of the array as an argument.

import numpy as np
 
print(np.ones((3, 4)))
Output:
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]

Create an Array filled with Other Numbers

To create an array filled with a specific number, the full() method is called. This method receives two required arguments. First, the shape of the array to be created, and second the number you wish to be populated. Say, I wish to create an array, filled with 5, the full() method can be used as shown below. 

import numpy as np
 
print(np.full((5, 4), 5, dtype='int64'))
Output:
[[5 5 5 5]
 [5 5 5 5]
 [5 5 5 5]
 [5 5 5 5]
 [5 5 5 5]]

Creating an Identity Array

In mathematics, an identity matrix is a special kind of matrix whose diagonal fields are populated with 1s while the rest are fields with zeros. In Numpy, such types of matrices can be created as arrays by using the eye() method. 

The method takes the length of the array to be created as a parameter. Since an identity matrix is always a square matrix, which means it has an equal number of rows and columns, the eye() method does not require a tuple of both numbers. As in (3, 3) to create a 3 by 3 identity matrix. It rather requires just the number, as in 3. 

In the example below, we attempt to create a 3 by 3 identity matrix. 

import numpy as np
 
print(np.eye(3))
Output:
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

Another way of creating an identity array is using the numpy.identity() method. Just like in the numpy.eye() method, it receives the shape of the array as a parameter. 

import numpy as np
 
#using the identity() method
print(np.identity(3))
Output:
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

As seen, it returns the same result. 

In the next tutorial, we will discuss how to use other methods in NumPy to create NumPy arrays. In the meantime, if you have any questions, please feel free to drop them in the comment section and I’d do my best to answer them. 

One Response

Leave a Reply

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.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class