{"id":8513,"date":"2021-06-10T14:06:02","date_gmt":"2021-06-10T08:36:02","guid":{"rendered":"https:\/\/python-programs.com\/?p=8513"},"modified":"2021-11-22T18:53:30","modified_gmt":"2021-11-22T13:23:30","slug":"numpy-zeros-numpy-ones-create-a-numpy-array-of-zeros-or-ones","status":"publish","type":"post","link":"https:\/\/python-programs.com\/numpy-zeros-numpy-ones-create-a-numpy-array-of-zeros-or-ones\/","title":{"rendered":"numpy.zeros() & numpy.ones() | Create a numpy array of zeros or ones"},"content":{"rendered":"
We are going to how we can create variou types of numpy arrays.<\/p>\n
The numpy module in python makes it able to create a numpy array all initialized with 0\u2019s.<\/p>\n
Syntax: numpy.zeros(shape, dtype=, order=)<\/pre>\nParameters :<\/strong><\/p>\n
\n
- shape :<\/strong> The shape of the numpy array.(single Int or a sequence)<\/li>\n
- dtype :<\/strong> It is an optional parameter that takes the data type of elements.(default value is float32)<\/li>\n
- order :<\/strong> It is also an optional parameter which defines the order in which the array will be stored(\u2018C\u2019 for column-major which is also the default value and \u2018F\u2019 for row-major)<\/li>\n<\/ol>\n
Flattened numpy array filled with all zeros :<\/h3>\n
Below code is the implementation for that.<\/p>\n
import numpy as np\r\n\r\n#Creating a numpy array containing all 0's\r\nzeroArray = np.zeros(5)\r\nprint(\"The array contents are : \", zeroArray)\r\n<\/pre>\nOutput : \r\nThe array contents are :\u00a0 [0. 0. 0. 0. 0.]<\/pre>\nCreating a 2D numpy array with 5 rows & 6 columns, filled with 0\u2019s :<\/strong><\/p>\n
To create an array with 5 rows and 6 columns filled with all 0\u2019s, we need to pass 5 and 6 as parameters into the function.<\/p>\n
Below code is the implementation for that.<\/p>\n
import numpy as np\r\n\r\n# Creating a 5X6 numpy array containing all 0's\r\nzeroArray = np.zeros((5, 6))\r\nprint(\"The array contents are : \", zeroArray)\r\n<\/pre>\nOutput :\r\nThe array contents are :\u00a0 [[0. 0. 0. 0. 0. 0.]\r\n [0. 0. 0. 0. 0. 0.]\r\n [0. 0. 0. 0. 0. 0.]\r\n [0. 0. 0. 0. 0. 0.]\r\n\u00a0[0. 0. 0. 0. 0. 0.]]<\/pre>\nIt created a zero numpy array of 5X6 size for us.<\/p>\n
numpy.ones( ) :<\/h3>\n
Just like the
numpy.zeros( )<\/code>,
numpy.ones( )<\/code> is used to initialize the array elements to 1. It has same syntax.<\/p>\n
Syntax - numpy.ones(shape, dtype=float, order='C')<\/pre>\nCreating a flattened numpy array filled with all Ones :<\/h3>\n
Below code is the implementation for that.<\/p>\n
import numpy as np\r\n\r\n# Creating a numpy array containing all 1's\r\noneArray = np.ones(5)\r\nprint(\"The array contents are : \", oneArray)\r\n<\/pre>\nOutput :\r\nThe array contents are :\u00a0 [1. 1. 1. 1. 1.]<\/pre>\nCreating a 2D numpy array with 3 rows & 4 columns, filled with 1\u2019s :<\/h3>\n
To create a 2D numpy array with 3 rows and 4 columns filled with 1\u2019s, we have to pass (3,4) into the function.<\/p>\n
Below code is the implementation for that.<\/p>\n
import numpy as np\r\n\r\n# Creating a 3X4 numpy array containing all 1's\r\noneArray = np.ones((3, 4))\r\nprint(\"The array contents are : \", oneArray)\r\nprint(\"Data Type of elements in Array : \", oneArray.dtype)\r\n<\/pre>\nOutput :\r\nThe array contents are :\u00a0 [[1. 1. 1. 1.]\r\n [1. 1. 1. 1.]\r\n [1. 1. 1. 1.]]\r\nData Type of elements in\u00a0 Array :\u00a0 float64<\/pre>\nLet\u2019s see how we can set the datatype to integer.<\/p>\n
import numpy as np\r\n\r\n# Creating a 3X4 numpy array containing all 1's int64 datatype\r\noneArray = np.ones((3, 4), dtype=np.int64)\r\nprint(\"The array contents are : \", oneArray)\r\nprint(\"Data Type of elements in Array : \", oneArray.dtype)\r\n<\/pre>\nOutput :\r\nThe array contents are :\u00a0 [[1 1 1 1]\r\n [1 1 1 1]\r\n [1 1 1 1]]\r\nData Type of elements in\u00a0 Array :\u00a0 int64<\/pre>\n<\/p>\n","protected":false},"excerpt":{"rendered":"
Create a 1D \/ 2D Numpy Arrays of zeros or ones We are going to how we can create variou types of numpy arrays. numpy.zeros( ) : The numpy module in python makes it able to create a numpy array all initialized with 0\u2019s. Syntax: numpy.zeros(shape, dtype=, order=) Parameters : shape : The shape of …<\/p>\n