Numpy ones_like() Function



The Numpy ones_like() function is used to create a new numpy array of the same shape and type as an existing array, but filled with ones. This is particularly useful for initializing arrays of specific shapes without manually defining dimensions.

Syntax

Following is the syntax of the Numpy ones_like() function −

numpy.ones_like(arr, dtype=None, order='K', subok=True, shape=None)

Parameters

Following are the parameters of the Numpy ones_like() function −

  • arr: Array whose shape and type define the shape and type of the output array.
  • dtype: The desired data type for the returned array. If None, the data type of arr is used.
  • subok: If True, then the newly created array will use the subclass type of arr.
  • shape: Overrides the shape of the result.
  • order (optional): It specifys the memory layout of the array. If object is not an array, the newly created array will be in C order (row major) unless F is specified, in which case it will be in Fortran order (column major) −
  • 'C': C-style row-major order.
  • 'F': Fortran-style column-major order.
  • 'A': 'F' if the input is Fortran contiguous, 'C' otherwise.
  • 'K': This is the default value keep the order as close as possible to the input.

Return Values

This function returns an array of ones with the same shape and type as the input array.

Example

Following is a basic example to create a numpy array of ones with the same shape as an existing array using Numpy ones_like() function −

import numpy as np
Numpy_Array = np.array([[1, 2, 3], [4, 5, 6]])
ones_array = np.ones_like(Numpy_Array)
print("Numpy Array of ones -\n", ones_array)

Output

Following is the output of the above code:

Numpy Array of ones -
 [[1 1 1]
 [1 1 1]]

Example : Specifying Data Type

We can specify the data type of the ones array using the dtype parameter. For example, if we need the float data type we need to set the dtype parameter to 'float'.

Float Datatype Array

In the following example, the array of ones has a specified data type of float32 −

import numpy as np
array_b = np.array([[7, 8, 9], [10, 11, 12]], dtype=np.int32)
ones_array_float = np.ones_like(array_b, dtype=np.float32)
print("Numpy Array of ones with Float Data Type -\n", ones_array_float)

Output

Following is the output of the above code:

Numpy Array of ones with Float Data Type -
 [[1. 1. 1.]
 [1. 1. 1.]]

String Datatype Array

When we create a string data type array using numpy.ones_like(), it will replace each value of the existing array with the string '1' rather than an empty string −

import numpy as np
array_b = np.array([[7, 8, 9], [10, 11, 12]], dtype=np.int32)
ones_array_string = np.ones_like(array_b, dtype=np.str_)
print("Numpy Array of ones with String Data Type -\n", ones_array_string)

Output

Following is the output of the above code:

Numpy Array of ones with String Data Type -
 [['1' '1' '1']
 ['1' '1' '1']]

Example : Multi-dimensional Array

The numpy.ones_like() function can be used to create an array filled with ones that matches the shape and type of an existing multi-dimensional array. This is useful for initializing arrays with the same dimensions and data type as a reference array.

In this example, we create a 3D array of ones with the shape of a 3x3x3 array −

import numpy as np
my_array = np.ones((3, 3, 3))
ones_3d_array = np.ones_like(my_array)
print("3D Numpy Array of ones -\n", ones_3d_array)

Output

3D Numpy Array of ones -
 [[[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]]
numpy_array_creation_routines.htm
Advertisements