Tutorial – numpy.zeros() , numpy.ones() and numpy.eye() in Python

Numpy Zeros , Numpy Ones and Numpy Eye in Python

Introduction

Python Numpy Array is an array data structure used for linear algebra calculation. Though numpy array can be formed using a python list or tuple, we can also create some special numpy arrays using numpy.zeros(), numpy.ones() and numpy.eyes() in Python.

Let us see them one by one. But before that let us import the numpy library.

Import Numpy Library

In [1]:
import numpy as np

Numpy Zeros: numpy.zeros()

Numpy zeros function returns a numpy array of only zeros with specified shape and data type.

Syntax

zeros(shape, dtype=float, order=’C’)

  • shape – This is the shape of the required array, input has to be an int or a tuple of int.
  • dtype (optional) – This is the required data type of the array, by default it is float.
  • order (optional) – This specifies how the array will be stored in the memory. It can be either ‘C’ for row-major or ‘F’ for column-major. By default, it is ‘C’.

Example 1: One Dimensional Numpy Zeros Array

Notice that we did not specify the data type, so by default, it assumed float.

Ad
Deep Learning Specialization on Coursera
In [2]:
np_1d_zero_array = np.zeros(4)

print('Output- ')
print(np_1d_zero_array)
print('Shape- ', np_1d_zero_array.shape)
print('Data type -', np_1d_zero_array.dtype)
Output- 
[0. 0. 0. 0.]
Shape-  (4,)
Data type - float64

Let us specify the data type as integer to make the complete numpy array as int.

In [3]:
np_1d_zero_array = np.zeros(4,int)

print('Output- ')
print(np_1d_zero_array)
print('Shape- ', np_1d_zero_array.shape)
print('Data type -', np_1d_zero_array.dtype)
Output- 
[0 0 0 0]
Shape-  (4,)
Data type - int32

Example 2: Multi-Dimensional Numpy Zeros Array

To create a multidimensional numpy zeros array, we need to pass a tuple of int that signifies the shape that we want.

In [4]:
# 2-D Array
np_zero_array = np.zeros((4,2))

print('Output- ')
print(np_zero_array)
print('Shape- ', np_zero_array.shape)
print('Data type -', np_zero_array.dtype)
Output- 
[[0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]]
Shape-  (4, 2)
Data type - float64
In [5]:
# 3-D Array
np_zero_array = np.zeros((4,2,3), int)

print('Output- ')
print(np_zero_array)
print('Shape- ', np_zero_array.shape)
print('Data type -', np_zero_array.dtype)
Output- 
[[[0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]]]
Shape-  (4, 2, 3)
Data type - int32

Example 3: Numpy Zeros Array with Tuples and Custom Data Type

Numpy zeros array can be formed with tuples as the elements and we can also define the data type for them. Below is a 2×3 array whose each element is a tuple that has a mixed data type of int and float.

In [6]:
np_zero_array = np.zeros((2,3),dtype = [('x', 'int'),('y', 'float')])
print('Output- ')
print(np_zero_array)
print('Shape- ', np_zero_array.shape)
print('Data type -', np_zero_array.dtype)
Output- 
[[(0, 0.) (0, 0.) (0, 0.)]
 [(0, 0.) (0, 0.) (0, 0.)]]
Shape-  (2, 3)
Data type - [('x', '<i4'), ('y', '<f8')]

Numpy Ones: numpy.ones()

This is very similar to Numpy Zero. Here Numpy zeros function returns a numpy array of only ones with specified shape and data type.

Syntax

ones(shape, dtype=float, order=’C’)

  • shape – This is the shape of the required array, input has to be an int or a tuple of int.
  • dtype (optional) – This is the required data type of the array, by default it is float.
  • order (optional) – This specifies how the array will be stored in the memory. It can be either ‘C’ for row-major or ‘F’ for column-major. By default, it is ‘C’.
  • Example 1: One Dimensional Numpy Ones Array

Notice that we did not specify the data type, so by default, it assumed float.

Example 1: One-Dimensional Numpy Ones Array

In [7]:
np_1d_ones_array = np.ones(4)

print('Output- ')
print(np_1d_ones_array)
print('Shape- ', np_1d_ones_array.shape)
print('Data type -', np_1d_ones_array.dtype)
Output- 
[1. 1. 1. 1.]
Shape-  (4,)
Data type - float64

Let us specify the data type as integer to make the complete numpy array as int.

In [8]:
np_1d_ones_array = np.ones(4,int)

print('Output- ')
print(np_1d_ones_array)
print('Shape- ', np_1d_ones_array.shape)
print('Data type -', np_1d_ones_array.dtype)
Output- 
[1 1 1 1]
Shape-  (4,)
Data type - int32

Example 2: Multi-Dimensional Numpy Ones Array

To create a multidimensional numpy ones array, we need to pass a tuple of int that signifies the shape that we want.

In [9]:
# 2-D Array
np_ones_array = np.ones((4,2))

print('Output- ')
print(np_ones_array)
print('Shape- ', np_ones_array.shape)
print('Data type -', np_ones_array.dtype)
Output- 
[[1. 1.]
 [1. 1.]
 [1. 1.]
 [1. 1.]]
Shape-  (4, 2)
Data type - float64
In [10]:
# 3-D Array
np_ones_array = np.ones((4,2,3))

print('Output- ')
print(np_ones_array)
print('Shape- ', np_ones_array.shape)
print('Data type -', np_ones_array.dtype)
Output- 
[[[1. 1. 1.]
  [1. 1. 1.]]

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

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

 [[1. 1. 1.]
  [1. 1. 1.]]]
Shape-  (4, 2, 3)
Data type - float64

Example 3: Numpy Ones Array with Tuples and Custom Data Type

Numpy ones array can be formed with tuples as the elements and we can also define the data type for them. Below is a 2×3 array whose each element is a tuple that has a mixed data type of int and float.

In [11]:
np_ones_array = np.ones((2,3),dtype = [('x', 'int'),('y', 'float')])
print('Output- ')
print(np_ones_array)
print('Shape- ', np_ones_array.shape)
print('Data type -', np_ones_array.dtype)
Output- 
[[(1, 1.) (1, 1.) (1, 1.)]
 [(1, 1.) (1, 1.) (1, 1.)]]
Shape-  (2, 3)
Data type - [('x', '<i4'), ('y', '<f8')]

Numpy Eye: numpy.eye()

Numpy eye function helps to create a 2-D array where the diagonal has all ones and zeros elsewhere.

Syntax

eye(N, M=None, k=0, dtype=’float’, order=’C’)

  • N – It is the number of rows in the array. It has to be int.
  • M (optional) – It is the number of columns in the array. If not specified then it will default to N.
  • K (optional) – It denotes the position of diagonal ones. By default is zero i.e. in middle. A positive value denotes upper diagonal and negative value lower diagonal.
  • dtype (optional) – This is the required data type of the array, by default it is float.
  • order (optional) – This specifies how the array will be stored in the memory. It can be either ‘C’ for row-major or ‘F’ for column-major. By default, it is ‘C’.
  • Example 1: Identity Matrix

Example 1: Identity Matrix

The identity matrix is a 2-D array whose number of columns is equal to the number of rows. center diagonal is 1 and elsewhere zero. It can be created simply bypassing the value of N.

In [12]:
np_identity_matrix = np.eye(4)

print('Output- ')
print(np_identity_matrix)
print('Shape- ', np_identity_matrix.shape)
print('Data type -', np_identity_matrix.dtype)
Output- 
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
Shape-  (4, 4)
Data type - float64

Miscellaneous Examples of Numpy Eye

In [13]:
np.eye(4, 5)
Output- 
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.]])
In [14]:
np.eye(4, 5, k = 2)
Output- 
array([[0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.],
       [0., 0., 0., 0., 0.]])
In [15]:
np.eye(4, 5, k = -2)
Output- 
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.]])

 

Click Here To Download This Interactive Jupyter Notebook

 

 

Reference-  https://numpy.org/doc/

Like and Comment section (Community Members)

Create Your ML Profile!

Don't miss out to join exclusive Machine Learning community

Comments

No comments yet