# Tutorial – 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 :
```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.

In :
```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 :
```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 :
```# 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 :
```# 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 :
```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 :
```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 :
```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 :
```# 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 :
```# 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 :
```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 :
```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 :
```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 :
```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 :
```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.]])```

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