Python NumPy ones() Function

Spread the love

Python NumPy ones() function is used to return a new array of given shape and type filled with ones. This function is very similar to zero(). The ones() function takes three arguments and returns the array filled with value 1. In this article, I will explain how to use the NumPy ones() function with examples.

1. Quick Examples of Python NumPy ones() Function

If you are in a hurry, below are some quick examples of how to use the Python NumPy ones() function.

# Below are the quick examples

# Example 1: Create an 1-D arr of ones
arr = np.ones(7)

# Example 2: Create an array of integer ones
arr = np.ones(7, dtype = int)

# Example 3: an aray with 6 ones and integer data type
arr = np.ones((6,), dtype=int)

# Example 4: create multi-dimensional array with ones use numpy.ones() function
arr = np.ones((3, 4))

# Example 5: Use two-dimensional array with ones
arr = (3,4)
arr2 = np.ones(arr)

# Example 6: use numpy.ones() where data type is float
arr = np.ones([4, 3], dtype = int)

# Example 7: create array with heterogeneous data types
arr = np.ones((3,2), dtype=[('x', 'int'), ('y', 'float')])

2. Syntax Python NumPy ones() Function

Following is the syntax of the function.

# Syntax of NumPy ones()
numpy.ones(shape, dtype = None, order = 'C')

2.1 Parameters of ones()

  • shape – It is an int or tuple of ints and a required input.
  • dtype – The desired dtype or data type for the array, e.g., int8. Default is float64.
  • order – To store multi-dimensional data in row-major (C) or column-major (F) order/pattern in the memory location.

2.2 Return Value of ones()

It returns ndarray of ones with the given shape, order, and datatype.

3. Usage of ones() Function

The np.ones() is a Numpy library function that returns an array of similar shape and size with values of elements of the array as 1. 

4. Create 1-D array using ones()

To create a one-dimensional array of ones by passing a single integer value ‘7’ using the NumPy ones() function, with no data type and order. For example,

import numpy as np
# Use numpy.ones() to create 1-D Array 
arr = np.ones(7)

# Output:
# [1. 1. 1. 1. 1. 1. 1.]

You can also create a array using np.ones() with a specific data type, and pass the required datatype as a dtype parameter. Let’s take an example,

# Use numpy.ones() to create 1-D Array 
arr = np.ones(7, dtype = int)

# Output:
# [1 1 1 1 1 1 1]

5. Create Multi-Dimensional Array with Ones()

To create a multi-dimensional array of ones, bypassing the shape of columns and rows as the parameter of np.ones() function. When we create an array with 3 rows and 4 columns using np.ones(), it will return a array of float ones.

# create multi-D array using numpy.ones() 
arr = np.ones((3, 4))

# Output:
# [[1. 1. 1. 1.]
# [1. 1. 1. 1.]
# [1. 1. 1. 1.]]

6. Create Multi-Dimensional Array of Integer Ones

As I mentioned above np.ones() returns an array of float ones. But if we want to create an array of ones as an integer data type, so that we can pass the data type parameter in the ones() function, it will return an array of integer ones. For example,

# Create an array of integer ones
arr = np.ones([4, 3], dtype = int)

# Output:
# [[1 1 1]
# [1 1 1]
# [1 1 1]
# [1 1 1]]

7. Create Array of Heterogeneous Ones

You can also create arrays with heterogeneous data type using np.ones() method.

# create array with heterogeneous ones
arr = np.ones((3,2), dtype=[('x', 'int'), ('y', 'float')])

# Output :
# [[(1, 1.) (1, 1.)]
# [(1, 1.) (1, 1.)]
# [(1, 1.) (1, 1.)]]

8. Conclusion

In this article, I have explained how to use Python np.ones() function and using this function how to create a NumPy array of 1 along with a specific datatype.

Happy Learning!!


Leave a Reply

You are currently viewing Python NumPy ones() Function