Numpy Ones Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will cover the numpy.ones() function of the NumPy library. The numpy.ones() function is used to return the matrix of given shape and type and initializes all the values of the matrix to one.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/variables_data_types -.-> lab-86487{{"`Numpy Ones Function`"}} python/numeric_types -.-> lab-86487{{"`Numpy Ones Function`"}} python/tuples -.-> lab-86487{{"`Numpy Ones Function`"}} python/importing_modules -.-> lab-86487{{"`Numpy Ones Function`"}} python/numerical_computing -.-> lab-86487{{"`Numpy Ones Function`"}} python/build_in_functions -.-> lab-86487{{"`Numpy Ones Function`"}} end

Importing Required Libraries

In order to use the NumPy library and its functions, we first need to import it. In this step, we will import NumPy.

import numpy as np

The numpy.ones() Syntax

The required syntax to use the numpy.ones() function is as follows:

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

Parameters:

  • shape: This parameter is in the form of a tuple that is used to define the shape of the matrix.
  • dtype: This parameter is used to indicate the data type of the matrix. The default value of this parameter is float. This is an optional parameter.
  • order: This is an optional parameter that is used to indicate the insertion order of the matrix. It mainly indicates whether to store the result in C- or Fortran-contiguous order. The default value is 'C'.

Returns:

This function will return a matrix with all the entries initialized to 1.

Examples

Now it's time to cover a few examples of the numpy.ones() function.

Example 1:

The first example shows the output of the numpy.ones() function when using only the shape parameter.

import numpy as np

print(np.ones((3, 2)))

Output:

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

Example 2:

The second example shows the output of the numpy.ones() function when using shape, dtype, and order parameters.

import numpy as np

print(np.ones((3, 2), dtype=int, order='C'))

Output:

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

Example 3:

The third example shows the output of the numpy.ones() function when using only the shape parameter to create a 1-D array.

import numpy as np

print(np.ones(5))

Output:

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

Summary

In this lab, we learned about the numpy.ones() function of the NumPy library. We covered its syntax, parameters, and the value returned by this function along with a few code examples. The numpy.ones() function is a useful tool for creating matrices and arrays filled with ones.

Other Python Tutorials you may like