numpy.ones() in Python: Introduction, Syntax & Examples

The numpy.ones() function in Python’s NumPy library creates an array filled with ones. Similar to numpy.zeros(), it’s often used as a starting point for initializing arrays before populating them with actual data. This function is widely used in numerical and scientific computing. In this article, we will understand this function, and its syntax, and learn how to use it with the help of five unique examples. Let’s get started.

Also Read: numpy.zeros() in Python

Syntax of numpy.ones() Function

numpy.ones() function sounds similar to numpy.ones_like() function but both are different. We will see how both of them are different from each other at the end of the article. But let’s first see in detail about numpy.ones().

Below is the syntax for using numpy.ones() function in Python.

Syntax:

numpy.ones(shape, dtype=float, order='C')

Parameters:

  • shape: A tuple specifying the dimensions (shape) of the output array. For example, for a 2D array, we would specify the shape as (rows, or columns).
  • dtype (optional): The data type of the elements in the array. It’s typically floating, but we can specify other data types as well.
  • order (optional): Specifies the memory layout of the array, either ‘C’ (row-major) or ‘F’ (column-major). The default is ‘C’.

Return

An array of ones with the given shape, data type, and order.

Examples of numpy.ones() Function

Let us now look at some examples to demonstrate the use of numpy.ones() function in Python. In each example, we have passed different parameter combinations to understand how this function works in different scenarios.

Example 1: Creating a 1D array by passing the ‘shape’ Parameter

In this example, we will only provide shape in the numpy.ones() function which is the dimension of the output array.

import numpy as np
np.ones(8)

Output:

Example 1: By specifying shape in numpy.ones() function

In the output above we got a 1D array filled with eight ones of data type float as the default data type is float.

Example 2: Creating a 2D array by passing the ‘shape’ Parameter

In this example, we will provide both rows and columns in the numpy.ones() function as the dimension of the output array.

np.ones((3,4))

Output:

Example 2: By specifying shape in numpy.ones() function

In the output above we got a 2D array filled with ones of data type float.

Example 3: Creating a 1D array by passing ‘shape’ & ‘dtype’ Parameters

In this example, we will provide shape and dtype in the numpy.ones() function which is the dimension of the output array and the data type of the elements in the array respectively.

np.ones(7,dtype="int")

Output:

Example 3: By specifying shape with the dtype in numpy.ones() function

In the output above we got a 1D array filled with ones of data type integer.

Example 4: Creating a 2D array by passing ‘shape’ & ‘dtype’ Parameters

In this example, we will do the same thing that we did in the last example but the only difference is we will specify tuple as a parameter for creating a 2D array.

np.ones((5,3), dtype="int")

Output:

Example 4: By specifying shape with the dtype in numpy.ones() function

In the output above we got a 2D array filled with ones of data type integer.

Example 5: Creating a 2D array by passing ‘shape’ & ‘order’ Parameters

In this example, we will provide shape which is dimension and order which specifies the memory layout of the array.

np.ones((2,5),order="F")

We have provided the order as F to store data in column-major order in the memory.

Output:

Example 5: By specifying shape with the order in numpy.ones() function

Difference Between numpy.ones() and numpy.ones_like() Function

Let’s see the difference by knowing the purpose of both of them. The numpy.ones() function returns a new array filled with ones for which we have to explicitly pass the shape of the array while the numpy.ones_like() function creates a new array filled with ones by mimicking the shape and data type of an existing array.

In simple words, the numpy.ones() function takes the shape of the array as an argument but numpy.ones_like() function takes an existing array as an argument. Also, numpy.ones() function is faster than numpy.ones_like() function for producing an array filled with all 1. Hope you understand the difference!

Summary

The numpy.ones() is a fundamental function for array initialization in a wide range of scientific and engineering applications. In this tutorial, we have discussed about numpy.ones() function provided by Python’s NumPy library and also explored five examples to create an array filled with ones. After reading this tutorial, we hope you can easily create an array filled with ones in Python.

Reference

https://stackoverflow.com/questions/56248030/how-to-get-array-of-numbers-using-ones-in-numpy

Priyanshu Singh
Priyanshu Singh
Articles: 44