Instagram
youtube
Facebook
Twitter

Create a NumPy Array of Ones with Shape (2, 5)

Create a NumPy Array of Ones with Shape (2, 5)

Short Description of the Program:

This program creates a 2D NumPy array filled with ones, having 2 rows and 5 columns using the np.ones() function.
It demonstrates how to initialize a matrix where all values are set to 1.0 by default.
    
Explanation:

  • import numpy as np
    Imports the NumPy library.

     
  • np.ones((2, 5))
    Creates a 2D array of shape 2 rows and 5 columns, with all elements set to 1.0 (default float type).

     
  • print()
    Displays the created array.

Program:

import numpy as np

array_ones = np.ones((2, 5))

print("2D NumPy Array filled with Ones:\n", array_ones)