How to create a 3 X 3 Matrix will all ones in it using NumPy

I am learner , bit stuck with it
Not getting how do I create below 3 X 3 matrix will all 1 ones in it

  1 , 1 , 1
  1 , 1 , 1
  1 , 1 , 1

My code :

import numpy as np
arr=np.ones(np.full(1)).reshape(3,3)
arr

>Solution :

Slightly change to the solution of author : user1740577

The user is expecting only integer value and not decimal values

By adding dtype=int the OP’s expected output can be achieved

Code :

import numpy as np
np.ones((3,3),dtype=int)

Expected output :

array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]])

Leave a Reply