All IT Courses 50% Off
Artificial Intelligence Tutorials

Creating Special Numpy Arrays in Python

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. 

Facebook Comments

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.

Related Articles

Back to top button
Close
Close

Get Python Course
worth 499$ for FREE!

Offer valid for 1st 20 seats only, Hurry up!!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

H2kinfosys Blog will use the information you provide on this form to be in touch with you and to provide updates and marketing.