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

We are going to how we can create variou types of numpy arrays.

### numpy.zeros( ) :

The numpy module in python makes it able to create a numpy array all initialized with 0’s.

Syntax: numpy.zeros(shape, dtype=, order=)

**Parameters :**

**shape :**The shape of the numpy array.(single Int or a sequence)**dtype :**It is an optional parameter that takes the data type of elements.(default value is float32)**order :**It is also an optional parameter which defines the order in which the array will be stored(‘C’ for column-major which is also the default value and ‘F’ for row-major)

### Flattened numpy array filled with all zeros :

Below code is the implementation for that.

import numpy as np #Creating a numpy array containing all 0's zeroArray = np.zeros(5) print("The array contents are : ", zeroArray)

Output : The array contents are : [0. 0. 0. 0. 0.]

**Creating a 2D numpy array with 5 rows & 6 columns, filled with 0’s :**

To create an array with 5 rows and 6 columns filled with all 0’s, we need to pass 5 and 6 as parameters into the function.

Below code is the implementation for that.

import numpy as np # Creating a 5X6 numpy array containing all 0's zeroArray = np.zeros((5, 6)) print("The array contents are : ", zeroArray)

Output : The array contents are : [[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 created a zero numpy array of 5X6 size for us.

### numpy.ones( ) :

Just like the `numpy.zeros( )`

, `numpy.ones( )`

is used to initialize the array elements to 1. It has same syntax.

Syntax - numpy.ones(shape, dtype=float, order='C')

### Creating a flattened numpy array filled with all Ones :

Below code is the implementation for that.

import numpy as np # Creating a numpy array containing all 1's oneArray = np.ones(5) print("The array contents are : ", oneArray)

Output : The array contents are : [1. 1. 1. 1. 1.]

### Creating a 2D numpy array with 3 rows & 4 columns, filled with 1’s :

To create a 2D numpy array with 3 rows and 4 columns filled with 1’s, we have to pass (3,4) into the function.

Below code is the implementation for that.

import numpy as np # Creating a 3X4 numpy array containing all 1's oneArray = np.ones((3, 4)) print("The array contents are : ", oneArray) print("Data Type of elements in Array : ", oneArray.dtype)

Output : The array contents are : [[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]] Data Type of elements in Array : float64

Let’s see how we can set the datatype to integer.

import numpy as np # Creating a 3X4 numpy array containing all 1's int64 datatype oneArray = np.ones((3, 4), dtype=np.int64) print("The array contents are : ", oneArray) print("Data Type of elements in Array : ", oneArray.dtype)

Output : The array contents are : [[1 1 1 1] [1 1 1 1] [1 1 1 1]] Data Type of elements in Array : int64