numpy array with ones code example

Example 1: python how to make an array of ones

# Basic syntax:
np.ones((number_rows, number_cols), dtype=int)

# Example usage:
import numpy as np
np.ones((5, 3), dtype=int)
array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]])

# Note, use np.zeros to create an array of zeros

Example 2: numpy ones

import numpy
print(numpy.ones((2, 3), dtype=int))
# creates the array of the shape (2, 3) of value 1 in its each cell
print(numpy.ones(6, dtype=str) 
# creates an array of size 6 with values of '1'