If I have to create a 2D array of 1s or 0s, I can use **numpy.ones()** or **numpy.zeros()** respectively. If I want to create a 2D array of values greater than 1, which Python function should I use?

+2 votes

You can use numpy.full() to create an array or matrix of a given shape and type, filled with fill_value. The fill_value can be any value.

Here is an example for this function:

>>> import numpy as np

>>> np.full((3,4),2) # generate 3x4 matrix with 2s

array([[2, 2, 2, 2],

[2, 2, 2, 2],

[2, 2, 2, 2]])>>> np.full(10,2) # generate an array of length 10 with 2s

array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2])