w3resource

NumPy: numpy.ones_like() function

numpy.ones_like() function

The numpy.ones_like() function is used to get an array of ones with the same shape and type as an existing array.

Syntax:

numpy.ones_like(a, dtype=None, order='K', subok=True)
NumPy array: ones_like() function

Parameters:

Name Description Required /
Optional
a The shape and data-type of a define these same attributes of the returned array Required
dtype Overrides the data type of the result. New in version 1.6.0. optional
order Overrides the memory layout of the result. 'C' means C-order, 'F' means F-order, 'A' means 'F' if a is Fortran contiguous, 'C' otherwise. 'K' means match the layout of a as closely as possible. optional
subok If True, then the newly created array will use the sub-class type of ‘a’, otherwise it will be a base-class array. Defaults to True. optional

Return value:

[ndarray] Array of ones with the same shape and type as a.

Example: Reshaping and Creating Arrays with Numpy

>>> import numpy as np
>>> x = np.arange(4)
>>> x = x.reshape((2,2))
>>> x
array([[0, 1],
       [2, 3]])
>>> x = np.arange(6)
>>> x = x.reshape((2,3))
>>> x
array([[0, 1, 2],
       [3, 4, 5]])
>>> np.ones_like(x)
array([[1, 1, 1],
       [1, 1, 1]])

In the above code second and third lines create a one-dimensional NumPy array x of length 4, using arange function and then reshapes it to a 2x2 array using reshape.

7th and 8th lines create a one-dimensional NumPy array x of length 6 using arange function and then reshapes it to a 2x3 array using reshape.

The 12th line uses the ones_like function to create an array of ones with the same shape as the input array x.

Pictorial Presentation:

NumPy array: ones_like() function

Example: numpy.ones_like where data type is int

>>> import numpy as np
>>> y = np.arange(3, dtype=float)
>>> y
array([ 0.,  1.,  2.])
>>> np.ones_like(y)
array([ 1.,  1.,  1.])
>>> a = np.arange(5, dtype=int)
>>> a
array([0, 1, 2, 3, 4])
>>> np.ones_like(a)
array([1, 1, 1, 1, 1])

Pictorial Presentation:

NumPy array: ones_like() function

Example: Create an array of ones with the same shape as another array, but with a different data type

Code:

import numpy as np

# Create an array with random values and same shape as arr
print("Array with random values:")
arr = np.random.rand(2, 3, 4)
print(arr)
arr_ones = np.ones_like(arr, dtype=int)
print("\nSame shape as above array, but with a different data type:")
print(arr_ones)

Output:

Array with random values:
[[[0.50889494 0.21100556 0.96568893 0.30631918]
  [0.81124072 0.11792419 0.32569323 0.57254974]
  [0.48291669 0.05026645 0.07577729 0.64142049]]

 [[0.82505059 0.9440906  0.88148473 0.04242502]
  [0.3830825  0.40186863 0.35258408 0.31377575]
  [0.85416651 0.85453533 0.50357045 0.81113633]]]

Same shape as above array, but with a different data type:
[[[1 1 1 1]
  [1 1 1 1]
  [1 1 1 1]]

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

In the above example, we create an array with random values and dimensions (2, 3, 4) using np.random.rand() function. We then create an array of ones with the same shape as arr using np.ones_like() function, but with integer data type. The resulting array has the same shape as arr but with ones instead of random values.

Python - NumPy Code Editor:

Previous: ones()
Next: zeros()



Follow us on Facebook and Twitter for latest update.