NumPy ones_like – A Complete Guide

NumPy Ones Like Cover Image

In this tutorial, we will be learning about the NumPy ones_like method and also seeing a lot of examples regarding the same. So let us begin!

Recommended read: NumPy ones – A Complete Guide


What is NumPy ones_like?

The ones_like method in NumPy is a function that returns an array of ones having the same shape and size as the given array.


Syntax of NumPy ones_like

Let us have a look at the syntax of the numpy.ones_like() method first.

numpy.ones_like(a, dtype=None, order='K', subok=True, shape=None)
ParameterDescriptionRequired/Optional
a (array_like)An object which defines the shape and data type of the array to be returned.Required
dtype (data type)The data type of the desired array. Overrides the data type of the result.Optional
orderThe desired order in which the multi-dimensional data is to be stored in the memory. It can be row-major (‘C’), column-major (‘F’), ‘A’ means ‘F’ if a is Fortran contiguous, ‘C’ otherwise. ‘K’ implies matching the layout of a as much as possible.Optional
subok (bool)Determines whether the newly created array will use the sub-class type of a (subok=True) or will be a base class array (subok=False).
The default value is True.
Optional
shapeThe shape of the desired array. Overrides the shape of the result.Optional

Returns:
An array with the same shape and data type as the given array, filled with all ones.


Examples of Numpy ones_like function

Let’s now take a look at how the numpy.ones_like() function works and what is the expected output for different types of inputs.

1-dimensional array using ones_like

import numpy as np

a = np.arange(10)
print("a =", a)

b = np.ones_like(a)
print("b =", b)

Output:

a = [0 1 2 3 4 5 6 7 8 9]
b = [1 1 1 1 1 1 1 1 1 1]

2-dimensional array using ones_like

N x N array

import numpy as np

a = np.arange(10).reshape(5, 2)
print("a =\n", a)

b = np.ones_like(a)
print("b =\n", b)

Output:

a =
 [[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]
b =
 [[1 1]
 [1 1]
 [1 1]
 [1 1]
 [1 1]]

1 x N array

import numpy as np

a = np.arange(12).reshape(1, 12)
print("a =\n", a)

b = np.ones_like(a)
print("b =\n", b)

Output:

a =
 [[ 0  1  2  3  4  5  6  7  8  9 10 11]]
b =
 [[1 1 1 1 1 1 1 1 1 1 1 1]]

N x 1 array

import numpy as np

a = np.arange(12).reshape(12, 1)
print("a =\n", a)

b = np.ones_like(a)
print("b =\n", b)

Output:

a =
 [[ 0]
 [ 1]
 [ 2]
 [ 3]
 [ 4]
 [ 5]
 [ 6]
 [ 7]
 [ 8]
 [ 9]
 [10]
 [11]]
b =
 [[1]
 [1]
 [1]
 [1]
 [1]
 [1]
 [1]
 [1]
 [1]
 [1]
 [1]
 [1]]

1-dimensional float-type array using Numpy ones_like

import numpy as np

a = np.arange(10)
print("a =", a)

b = np.ones_like(a, dtype=float)
print("b =", b)

Output:

a = [0 1 2 3 4 5 6 7 8 9]
b = [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

2-dimensional float-type array

import numpy as np

a = np.arange(10).reshape(2, 5)
print("a =\n", a)

b = np.ones_like(a, dtype=float)
print("b =\n", b)

Output:

a =
 [[0 1 2 3 4]
 [5 6 7 8 9]]
b =
 [[1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]]

What’s the difference between Numpy ones vs ones_like

  • Note that, in the ones method we are creating a new array of our desired shape and data type having all the values as 1. But, here, we are directly passing an array or an array-like object to get an array of the same shape and data type.
  • The NumPy ones_like function takes more time than the NumPy ones function to produce an array with all 1.

Conclusion

That’s all! In this tutorial, we learned about the Numpy ones_like method and practiced different types of examples using the same. If you want to learn more about NumPy, feel free to go through our NumPy tutorials.


Reference