• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:14 mins read
You are currently viewing Python NumPy ones() Function

The NumPy ones() function in Python is used to create an array of the specified shape and type, where all the elements are set to 1. This function is very similar to zeros(). 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.

Advertisements

1. Quick Examples of NumPy ones() Function

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


# Quick examples of numpy ones() function

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

# Example 2: Create a 1D array of ones 
# With 5 elements and integer data type
arr = np.ones(5, dtype = int)

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

# Example 4: Create two-dimensional array with ones 
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 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 – The shape of the array. It can be an integer or a tuple of integers specifying the size of each dimension.
  • dtype (optional) – The data type of the array elements. If not specified, the data type is inferred from the input data, 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()

You can create a 1D array using the numpy.ones() function, you can simply provide the desired length as the shape parameter. For example, np.ones(7) creates a 1D array with 7 elements, and all elements are initialized to 1.


# Import numpy module
import numpy as np

# Use numpy.ones() 
# To create 1-D Array 
arr = np.ones(7)
print("After getting a 1D array filled with ones:\n",arr)

Yields below output.

numpy ones

You can specify a specific data type for the elements in the array by using the dtype parameter of the numpy.ones() function. For example, dtype=int is used to specify that the elements in the array should have an integer data type.


# Create a 1D array of ones 
# With 5 elements and integer data type
arr = np.ones(5, dtype = int)
print("1D array with integer data type:\n",arr)

Yields below output.

numpy ones

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 you create an array with 3 rows and 4 columns using np.ones(), it will return an array of float ones.


# Create two-dimensional array with ones 
arr = np.ones((3, 4))
print("2D Array with ones:\n",arr)

# Output:
# 2D Array with ones:
#  [[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 you want to create an array of ones as an integer data type, so that you 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)
print("2D array with integer data type:\n",arr)

# Output:
# 2D array with integer data type
# [[1 1 1]
# [1 1 1]
# [1 1 1]
# [1 1 1]]

7. Create Array of Heterogeneous Data Types

To create a NumPy array with ones and heterogeneous data types using a structured dtype. For example, the array has a shape of (3, 2) and is defined to have two fields, ‘x’ with integer type (‘int’) and ‘y’ with float type (‘float’).

In this array, each element has two fields, ‘x’ and ‘y’, with ‘x’ being an integer and ‘y’ being a float, both initialized to ones. You can further customize the data types or field names based on your specific needs.


# Create array with heterogeneous data types
arr = np.ones((3,2), dtype=[('x', 'int'), ('y', 'float')])
print("Array with heterogeneous data types:\n",arr)

# Output :
# Array with heterogeneous data types:
# [[(1, 1.) (1, 1.)]
# [(1, 1.) (1, 1.)]
# [(1, 1.) (1, 1.)]]

Frequently Asked Questions

What does numpy.ones() do?

numpy.ones() is a function in the NumPy library that creates an array filled with ones. It allows you to specify the shape and data type of the array.

How to create a 1D array with all elements set to 1 using numpy.ones()?

To create a 1D array with all elements set to 1 using numpy.ones(), you can simply provide the desired length as the shape parameter.

Can I create an array of ones with a specific data type?

You can create an array of ones with a specific data type using the numpy.ones() function by specifying the dtype parameter. For example, dtype=int is used to specify that the elements in the array should have an integer data type

How do I create a 2D array with all elements set to 1?

To create a 2D array with all elements set to 1 using numpy.ones(), you need to specify the shape of the array.

How to create a heterogeneous array with ones?

Creating a truly heterogeneous array (an array with elements of different data types) using numpy.ones() can be a bit involved because numpy.ones() is designed to create arrays with elements of a uniform data type. However, you can achieve a heterogeneous array using structured data types or a combination of arrays.

What is the default data type for numpy.ones()?

The default data type for the elements created by numpy.ones() is float. When you use numpy.ones() to create an array without specifying the dtype parameter, the elements in the array will be of floating-point type.

Conclusion

In this article, I have explained the syntax and usage of numpy.ones() function and used this function to create ndarray of specific shapes and datatype filled with ones with examples.

Happy Learning!!

References