Python NumPy Functions – Ones, Zeros, Empty Functions

python numpy functions ones, zeros, empty functions

In this article, we’ll learn about the three Python NumPy Functions. These are ones, zeros, and empty functions. We’ll first understand its purpose, then understand how to implement it with practical Python code examples.

So, let’s throw ourselves right into it.

What are Python NumPy Ones, Zeros, and Empty Functions

python numpy functions ones, zeros, empty functions

python numpy functions ones, zeros, empty functions

By using these NumPy functions, we can easily create arrays with large amounts of values by using very less code. For instance, if you want to create an array of 100, 200, or more/less in which all elements will be one. For that, we’ll use the Python NumPy ones function. If you want to create an array of zero elements, then use the zeros function of Python NumPy. In case, you want to create an array of uninitialized items, then use the empty function.

Let’s now understand its usage with practical code examples.

Creating a Python NumPy 2D Array

import numpy as np 
arr_2d= np.array([[1,1,1],[1,1,1]])
print(arr_2d)

Output

[[1 1 1]
 [1 1 1]]

We imported Python NumPy, then created a 2D array using the array function of NumPy. Finally, we’ve printed it and it displays a 2d NumPy array. In our previous article, we explained Python 1D, and 2D in detail. Click here to check it out.

In this example, we’ve only 6 items, but let’s say we want to have a 2D of 100 items then specifying each item like this will make the process so lengthy and difficult for the coder. Let’s try to find a solution for it in the next block.

Python NumPy Functions

The three Python NumPy functions that we’ll be using in this article are as follows:

  1. Ones Function
  2. Zeros Function
  3. Empty Function

1. Ones Function

This is used when we want to create an array that includes 1. See the below code:

arr_ones= np.ones(8)
arr_ones

Output

array([1., 1., 1., 1., 1., 1., 1., 1.])
In this code, we’ve specified the ones() function and passed it 8 which means we want 8 columns and 1 row. In that case, we’ve to give the number of columns to it as we’ve done in the above code.

We can also see that the resulting values are in float format which by default is specified. But we can change it to other specific types as well.

Change the Type of the Ones Item in the Array

To change the type, we can use the dtype parameter of the ones function. See below:

arr_ones= np.ones(8,dtype=int)
arr_ones
Output
array([1, 1, 1, 1, 1, 1, 1, 1])

We’ve changed the type to int using the type parameter. We can pass bool, float, etc to it as well. Let’s try it with a boolean value as well. See below:

np.ones(8,dtype=bool)    // rest of the code is same as specified above
Output
array([ True,  True,  True,  True,  True,  True,  True,  True])

1 is taken as true and 0 is taken as false so 1 is replaced by true in the array.

Multiple Rows and Columns using Ones Function

If you want to give some specific rows and columns to create an array with a specific shape using the ones function then you’ve to pass a Python tuple as the first argument to the ones function. See the below code:

array_ones= np.ones((4,6),dtype=int)
array_ones
Output
array([[1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1]])

We’ve used a tuple (4,6) as the first argument to the ones function which means 4 rows and 6 columns. The first item is for rows and the second one is for columns. In this way, we can specify a 2D array with our specific number of rows and columns.

Order Parameter of Python Numpy Ones Function

It specifies the order of array elements stored inside memory. Layout of Storage or order of memory is controlled by this order parameter. This parameter takes two values: These are:

‘C’ – It specifies C-style or row-major order. In NumPy, this is the default order. It defines the row-by-row storage of array items inside memory. In memory, the elements of the same row are adjacently stored.

‘F’ – It is Fortran-style or column-major order. Inside memory, it specifies the column-by-column storage of array items.  The elements of the same column are adjacently stored.

This is how we can specify it. See the below code:

np.ones((5,3), order='F')   // use F or C, the default is set to C

2. Zeros Function

It works the same as the ones function. The difference is that it sets the values to zero. See the below code:

array_zeros= np.zeros((5,3),dtype=int,order='C')
array_zeros

Output

array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]])

It also has the dtype and order parameters. We can also pass only the number of columns to it if we want only a single row. See below:

np.zeros(5)

Output

array([0., 0., 0., 0., 0.])

We can see that it also returns a float value by default which we can change by using the dtype parameter of the zeros function. Let’s pass a boolean value to it and see the output.

np.zeros(3,dtype=bool)

Output

array([False, False, False])

0 is taken as false so false is shown in the resulting NumPy array. Let’s pass the string format to it and see what we get. See below:

np.zeros((4,8), dtype=str )

Output

array([['', '', '', '', '', '', '', ''],
       ['', '', '', '', '', '', '', ''],
       ['', '', '', '', '', '', '', ''],
       ['', '', '', '', '', '', '', '']], dtype='<U1')

When 0 is converted to a string in Python then an empty string is returned as shown in the above output.

Convert Empty String to Boolean

We can also convert an empty string to a boolean using the bool function. See below:

em_str=''
print(bool(em_str))
Output
False

3. Empty Function

This function creates an array with random values. See below:

arr_emp=np.empty(5)
arr_emp

Output

array([2.12199579e-314, 4.67296746e-307, 4.96041908e-321, 3.79442416e-321,
       0.00000000e+000])

We can see that random numbers have been generated using the Python NumPy empty function. See the below examples as well to create a matrix of random numbers.

array_emp=np.empty((5,3),dtype=int) 
array_emp

Output

array([[4128860, 6029375, 3801155],
       [5570652, 6619251, 7536754],
       [5898332, 4522053, 4718675],
       [8257609, 6029361, 7340097],
       [4456560, 7602273,      97]])

Conclusion

In conclusion, we’ve learned Python NumPy ones, zeros, and empty functions in detail with practical examples using Python code. Hope you’ve learned something new from this article.

Don’t forget to visit our other articles in which we’ll learn about more Python Numpy function with code examples. Thank you for reading it.

Leave a Comment

Your email address will not be published. Required fields are marked *