NumPy ones – A Complete Guide

NumPy Ones Cover Image

Hello and welcome to this tutorial on Numpy ones. In this tutorial, we will be learning about the NumPy ones() method and also seeing a lot of examples regarding the same. So let us begin!


What is the NumPy ones method?

NumPy ones returns a Numpy array of the given shape and data type with all values set to 1.


Syntax of NumPy ones

Let us first have a look at the syntax of NumPy ones.

numpy.ones(shape, dtype=None, order='C', like=None)
ParameterDescriptionRequired/Optional
shapeThe desired shape of the array. It can be an int or a tuple of ints.Required
dtypeThe desired data type of the array.
The default data type is float.
Optional
orderThe desired order in which the multi-dimensional data is to be stored in the memory. It can either be row-major (‘C’) or column-major (‘F’).
The default order is ‘C’ i.e. row-major.
Optional
like (array_like)Reference object to allow the creation of arrays that are not NumPy arrays.Optional

Returns: 
An array with the given shape, data type and order filled with only ones.


Examples of using NumPy ones

Let’s now look at some practical examples of the numpy.ones() method.

1-dimensional array using ones

import numpy as np

one_dim_array = np.ones(4)
print(one_dim_array) 

Output:

[1. 1. 1. 1.]

2-dimensional array using zeros

N x M array

import numpy as np

two_dim_array = np.ones((4, 2))
print(two_dim_array) 

Output:

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

1 x N array

import numpy as np

one_row_array = np.ones((1, 3))
print(one_row_array) 

Output:

[[1. 1. 1.]]

N x 1 array

import numpy as np

one_col_array = np.ones((5, 1))
print(one_col_array) 

Output:

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

1-dimensional int-type array

import numpy as np

one_dim_int_array = np.ones(5, dtype=np.int64)
print(one_dim_int_array) 

Output:

[1 1 1 1 1]

2-dimensional int-type array

import numpy as np

two_dim_int_array = np.ones((3, 3), dtype=np.int64)
print(two_dim_int_array) 

Output:

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

1-dimensional custom data type array

import numpy as np

custom_one_dim_array = np.ones(4, dtype=[('x', 'int'), ('y', 'float')])
print(custom_one_dim_array) 
print(custom_one_dim_array.dtype) 

Output:

[(1, 1.) (1, 1.) (1, 1.) (1, 1.)]
[('x', '<i4'), ('y', '<f8')]

In this example, we specified the first value as an int and the second as a float.


2-dimensional custom data type array

We can specify the elements of the array as a tuple and also specify their data types.

import numpy as np

custom_two_dim_array = np.ones((2, 3), dtype=[('x', 'float'), ('y', 'int')])
print(custom_two_dim_array) 
print(custom_two_dim_array.dtype) 

Output:

[[(1., 1) (1., 1) (1., 1)]
 [(1., 1) (1., 1) (1., 1)]]
[('x', '<f8'), ('y', '<i4')]

Here, the code specifies the first value of the tuple in the array elements to be a float and the second one to be an int.


Conclusion

That’s all! In this tutorial, we learned about the Numpy ones method and practiced different types of examples using the same.
If you want to learn more about NumPy, feel free to go through our NumPy tutorials.


Reference