{"id":7834,"date":"2023-11-03T08:16:52","date_gmt":"2023-11-03T02:46:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=7834"},"modified":"2023-11-10T12:14:11","modified_gmt":"2023-11-10T06:44:11","slug":"np-ones-create-1d-2d-numpy-array-filled-with-ones-1s","status":"publish","type":"post","link":"https:\/\/python-programs.com\/np-ones-create-1d-2d-numpy-array-filled-with-ones-1s\/","title":{"rendered":"np.ones() \u2013 Create 1D \/ 2D Numpy Array filled with ones (1\u2019s)"},"content":{"rendered":"
In this article, we will learn to create array of various shapes where all the values are initialized with 0 & 1.<\/p>\n
A function is provided by Python’s numpy module i.e. i.e.<\/p>\n Arguments:-<\/p>\n Let’s see the below 4 different types implementation with code<\/p>\n We can create a flattened numpy array with all values as ‘0’.<\/p>\n We can create a 2D numpy array by passing (4,3) as argument in We know that default value of dtype is float64, let’s try to change the dtype to int64.<\/p>\n A function is provided by Python’s numpy module i.e. i.e.<\/p>\n Arguments:-<\/p>\n We can make all values as 1 in a flattened array.<\/p>\n We can create a 2D numpy array by passing row and column as argument in We know default type of dtype is float64, let’s try to change it to int64.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Create a 1D \/ 2D Numpy Arrays of zeros or ones In this article, we will learn to create array of various shapes where all the values are initialized with 0 & 1. numpy.zeros() : A function is provided by Python’s numpy module i.e. numpy.zeros() which will create a numpy array of given shape and …<\/p>\nnumpy.zeros()<\/code> which will create a numpy array of given shape and where all values are initialized with 0’s.<\/p>\n
numpy.zeros(shape, dtype=float, order='C')<\/pre>\n
\n
\n
\n
\n
\n
<\/a>Creating a flattened 1D numpy array filled with all zeros :<\/h3>\n
import numpy as sc\r\n# create a 1D numpy array with values as 0\r\nnumarr = sc.zeros(5)\r\nprint('Contents of the Flattened Numpy Array : ')\r\nprint(numarr)\r\n<\/pre>\n
Output :\r\nContents of the Flattened Numpy Array :\r\n[0. 0. 0. 0. 0.]<\/pre>\n
<\/a>Creating a 2D numpy array with 4 rows & 3 columns, filled with 0\u2019s :<\/h3>\n
numpy.zeros()<\/code> which will return all values as ‘0’.<\/p>\n
import numpy as sc\r\n# create a 2D numpy array with values as 0 \r\nnumarr = sc.zeros((4,3))\r\nprint('Contents of the 2D Numpy Array : ')\r\nprint(numarr)\r\n<\/pre>\n
Output :\r\nContents of the 2D Numpy Array :\r\n[[0. 0. 0.]\r\n [0. 0. 0.]\r\n [0. 0. 0.]\r\n\u00a0[0. 0. 0.]]<\/pre>\n
import numpy as sc\r\n# create a 2D numpy array with values as 0 which are of int data type\r\nnumarr = sc.zeros((4,3), dtype=sc.int64)\r\nprint('Contents of the 2D Numpy Array : ')\r\nprint(numarr)\r\n<\/pre>\n
Output :\r\nContents of the 2D Numpy Array :\r\n[[0 0 0]\r\n [0 0 0]\r\n [0 0 0]\r\n\u00a0[0 0 0]]<\/pre>\n
numpy.ones() :<\/h3>\n
numpy.ones()<\/code> which will create a numpy array of given shape and where all values are initialized with 1’s.<\/p>\n
numpy.ones(shape, dtype=float, order='C')<\/pre>\n
\n
\n
<\/a>Creating a flattened 1D numpy array filled with all Ones :<\/h3>\n
import numpy as sc\r\n# create a flattened 1D numpy array of size 5 where all values are 1\r\nnumarr = sc.ones(5)\r\nprint('Contents of the Flattened Numpy Array : ')\r\nprint(numarr)\r\n<\/pre>\n
Output :\r\nContents of the Flattened Numpy Array :\r\n[ 1.\u00a0 1.\u00a0 1.\u00a0 1.\u00a0 1.]<\/pre>\n
<\/a>Creating a 2D numpy array with 3 rows & 3 columns, filled with 1\u2019s\u00a0 :<\/h3>\n
numpy.ones()<\/code> where all the values are 1.<\/p>\n
import numpy as sc\r\n# create a 2D numpy array with 3 rows & 4 columns with all values as 1\r\nnumarr = sc.ones((3,3))\r\nprint('Contents of the 2D Numpy Array : ')\r\nprint(numarr)\r\nprint('Data Type of the contents in given Array : ',numarr.dtype)\r\n<\/pre>\n
Output :\r\nContents of the 2D Numpy Array :\r\n[[1. 1. 1.]\r\n [1. 1. 1.]\r\n [1. 1. 1.]]\r\nData Type of the contents in\u00a0 given Array :\u00a0 float64<\/pre>\n
import numpy as sc\r\n# create a 2D numpy array with values as 1 which are of int data type\r\nnumarr = sc.ones((4,3), dtype=sc.int64)\r\nprint('Contents of the 2D Numpy Array : ')\r\nprint(numarr)\r\n<\/pre>\n
Output :\r\nContents of the 2D Numpy Array :\r\n[[1 1 1]\r\n [1 1 1]\r\n [1 1 1]\r\n\u00a0[1 1 1]]<\/pre>\n