Python initialize 2d array with zeros
View Discussion Show
Improve Article Save Article View Discussion Improve Article Save Article The numpy.zeros() function returns a new array of given shape and type, with zeros. Syntax: numpy.zeros(shape, dtype = None, order = 'C') Parameters : shape : integer or sequence of integers order : C_contiguous or F_contiguous C-contiguous order in memory(last index varies the fastest) C order means that operating row-rise on the array will be slightly quicker FORTRAN-contiguous order in memory (first index varies the fastest). F order means that column-wise operations will be faster. dtype : [optional, float(byDeafult)] Data type of returned array. Returns : ndarray of zeros having given shape, order and datatype. Code 1 : Python
Output : Matrix b : [0 0] Matrix a : [[0 0] [0 0]] Matrix c : [[ 0. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]] Code 2 : Manipulating data types Python
Output : [(0.0, 0) (0.0, 0)] Reference : https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.zeros.html#numpy.zeros Note : zeros, unlike zeros and empty, does not set the array values to zero or random values respectively.Also, these codes won’t run on online IDE’s. Please run them on your systems to explore the working. This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. In this article, we will discuss how to create an array of zeros in Python. In array items are stored at contiguous memory locations and here we will try to add only Zeros into the array with different methods. Here we will cover different approaches to creating a zero’s element array. The different approaches that we will cover in this article are:
Method 1: Using simple multiplicationIn this example, we are multiplying the array of zero to 9. In return, we will get an array with 9 elements of 0’s. Python3
Output: [0, 0, 0, 0, 0, 0, 0, 0, 0] Method 2: Using a loopIn this example, we are creating a 0’s array using a for loop of range from 0 to 10. Python3
Output: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Method 3: Using List comprehensionExample 1: Creating 1D array using list comprehension As we know here for loop will generate such a list, and we iterate zero within the list till the given range. Python3
Output: [0, 0, 0, 0, 0] Example 2: Creating 2D array using list comprehension In this example, we are creating a 2-D array using a list comprehension in python to create 0’s of 5 rows and 10 columns. Python3
Output: [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] Method 4: Using the In-Build method numpy.zeros() methodIn this example, we are creating a NumPy array with zeros as the numpy.zeros()function is used which returns a new array of given shape and type, with zeros. Python3
Output: In the output, i4 specifies 4 bytes of integer data type, whereas f8 specifies 8 bytes of float data type. [[[(0, 0.) (0, 0.) (0, 0.)] [(0, 0.) (0, 0.) (0, 0.)]] [[(0, 0.) (0, 0.) (0, 0.)] [(0, 0.) (0, 0.) (0, 0.)]]] [('x', ' |