In this tutorial I’ll show you how to use the NumPy ones function, which is often called np.ones.
What the numpy ones function does
The NumPy ones function creates NumPy arrays filled with
1‘s. That’s it. It’s pretty straight forward.
The np.ones function is also fairly easy to use. But, there are a few details of the function that you might not know about, such as parameters that help you precisely control how it works.
Having said that, this tutorial will give you a full explanation of how the np.ones function works.
I’ll explain how the syntax works at a very high level. I’ll also explain the parameters of the ones() function.
Moreover, I’ll show you several step-by-step examples of how to use the np.ones function. These examples will show you how to use the function parameters to modify the function’s behavior.
Ok … first things first. Let’s look at the syntax of the np.ones function.
The syntax of the NumPy ones function
The syntax of the NumPy ones function is very straightforward. Syntactically, it’s very similar to a few other NumPy functions like the NumPy zeros function.
Typically, we’ll call the function with the name
np.ones(). Keep in mind that this assumes that you’ve imported NumPy into your environment using the code
import numpy as np.
Inside of the function, there are several parameters. The two most important parameters are the
shape parameter and the
Let’s quickly talk about the parameters of the np.ones function.
The parameters of the NumPy ones function
As you saw in the previous section, there are several parameters that enable you to control the behavior of the NumPy ones function. The most important is the
shape parameter, but there is also the
dtype parameter and the
Let’s look at those one at a time.
shape parameter enables you to specify the exact shape of the output of the function.
Remember … the np.ones function creates arrays that contain all
1‘s. Therefore, you don’t need to tell it the values to put in the array; they are always
1‘s. The critical thing that you need to specify then is the exact shape of the output array.
shape parameter is required.
This parameter will accept an integer argument, and it will also accept a sequence of integers. Often, for multi-dimensional array outputs, you’ll see a tuple of ints. But because the
shape parameter will accept any sequence of integers, it’s also possible to use a list or other numeric sequence to define the shape.
I’ll show you concrete examples of how to use the shape parameter later in this tutorial.
dtype parameter enables you to specify the datatype of the
1‘s in the output array.
By default, the
1‘s will be floating point numbers. Specifically, the outputs will have the type
Having said that, you can choose a data type from among the many Python and NumPy data types.
Later in this tutorial, I’ll show you an example of how to set the output datatype using the
order parameter handles how multi-dimensional arrays are stored.
To be honest, this is something you’re unlikely to use, so we’re not going to cover it in this tutorial. If you need to use this parameter, I suggest that you review the original documentation for NumPy.
Examples: how to use the NumPy ones function
Ok. Now that we’ve reviewed the parameters of the np.ones function, let’s look at some concrete examples of how the function works.
Run this code first
Before we get started, there’s a piece of code that you need to run:
import numpy as np
You need to run that code first, otherwise the following examples won’t work properly.
A very simple example of using the numpy ones function
We’re going to start simple.
Here, we’ll make a simple, 1-dimensional array of four
np.ones(shape = 4)
Which produces the following output:
array([ 1., 1., 1., 1.])
Keep in mind that you can also write the code without explicitly using the
Both the snippets of code –
np.ones(shape = 4) and
np.ones(4) – produce the same output. This is because the
shape parameter is what we call a “positional” parameter. This means that you can pass an argument to that parameter strictly by the position inside the parenthesis. NumPy knows that the first argument inside the function is supposed to correspond to the
Ok, back to the example. This is pretty straight forward, so it should be pretty clear what’s going on.
We have specified that we want the “shape” to be four values long. Essentially, this causes NumPy to create a new 1-dimensional NumPy array filled with
1‘s that is four values long. Visually, we can think of it like this:
This is really as simple as it gets. We specified that the “shape” is 4, which produces a NumPy array that is four values long.
Create an array with a specific shape, using np.ones
Next, let’s examine how to make arrays of 1’s that have a more complex shape.
In the last example, we made a simple, 1-dimensional array that was four items long.
To create arrays with more complex shapes, we need to manipulate the
shape parameter. Specifically, instead of providing an integer value to the
shape parameter, like
shape = 4, we need to provide a sequence of integers that specifies a multi dimensional shape.
Let me show you an example so you understand.
Here, we’re going to create a 2-dimensional NumPy array with 2 rows and 3 columns.
To do this, we’re going to use the
shape parameter. Specifically, we’re going to set
shape = (2, 3).
np.ones(shape = (2,3))
This code creates the following array:
array([[ 1., 1., 1.], [ 1., 1., 1.]])
For the sake of illustration, we can visually represent the array like this:
So what do we have?
This is a 2 dimensional array. The two dimensions are the rows and columns.
The shape is 2 by 3. There are 2 rows and 3 columns.
We created this by specifying that exact shape by setting
shape = (2, 3). The first integer in the sequence (2) specifies the number of rows. The second integer in the sequence (3) specifies the number of columns.
Notice that when we use the
shape parameter, the argument that we provided was a tuple of integers:
(2, 3). Having said that, the argument to the shape parameter can be any sequence. For example, we could instead have provided a list:
[2, 3]. For the purposes of manipulating the
shape parameter of np.ones, a Python list would work essentially the same as a Python tuple.
Create an array with a specific data type using np.ones
Next, let’s create a NumPy array that contains elements of a specific data type.
By default, the np.ones function creates an array of floating point numbers. Specifically, it creates an array of ones that have the data type
We can change this behavior by using the
This is very straight forward, but let’s take a look at an example so you can see how it’s done.
Here, we’re going to create an array of integers. To do this, we will set
dtype = int inside of the np.ones() function:
np.ones(3, dtype = int)
This code creates the following output:
array([1, 1, 1])
This is really straight forward. The output is a NumPy array with three ones, all integers.
If you want to check the datatype of the output, you can examine it by using the
np.ones(3, dtype = int).dtype
When you run this code, you’ll see that the data type is integer …
int64 to be specific.
Remember that when using the
dtype parameter, you can specify essentially any Python data type or NumPy data type.
Putting the pieces together: specifying shape and data type
Let’s try one more example.
Here, we’re going to use several of the parameters together to precisely control the output of the NumPy ones function.
np.ones(shape = (3, 5), dtype = int)
This code creates the following output:
array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]])
If you’ve understood the prior examples in this tutorial, you should understand what’s going on here.
We’ve called the function with the code
Inside the function, we’ve used two parameters to control the function:
shape = (3, 5) to create an array with 3 rows and 5 columns.
dtype = int to specify that we want the values of the output array to be integers. Specifically, the values in the array are of the type
Check out our other NumPy tutorials
If you’re interested in learning more about NumPy, we have several other tutorials.
There are NumPy tutorials that explain:
- The basics of NumPy arrays
- How to use the NumPy zeros function
- How to use the NumPy arrange function
- A quick introduction to the NumPy reshape function
- How to use the NumPy linspace function
… and more.
If you’re interested in NumPy (and data science in Python) then check out those tutorials.
For more Python data science tutorials, sign up for our email list
Moreover, if you want to learn more about data science in Python (and data science in general) then sign up for our email list.
Here at Sharp Sight, we teach data science.
And every week, we publish articles and tutorials about data science.
When you sign up for our email list, you’ll get immediate access to our tutorials … they’ll be delivered right to your inbox.
When you sign up, you’ll learn about:
- Base Python
- Scikit learn
- Machine learning
- Deep learning
- … and more.
Want to learn data science in Python? Sign up now.