This tutorial will explain how to use he Numpy full function in Python (AKA, np.full or numpy.full).

##### Numpy full creates a Numpy array filled with the same value

At a high level, the Numpy full function creates a Numpy array that’s filled with the same value.

It’s a fairly easy function to understand, but you need to know some details to really use it properly.

Having said that, this tutorial will give you a quick introduction to Numpy arrays. Then it will explain the Numpy full function, including the syntax. After explaining the syntax, it will show you some examples and answer some questions.

The following links will take you to the appropriate part of the tutorial. So if you’re in a hurry, you can just click on a link.

**Contents:**

- A quick review of Numpy and Numpy arrays
- Introduction to Numpy full
- The syntax of np.full
- Numpy full examples
- Numpy full FAQ

However, it’s probably better to read the whole tutorial, especially if you’re a beginner.

## A quick review of Numpy and Numpy arrays

Quickly, let’s review Numpy and Numpy arrays.

So what is Numpy?

To put it simply, Numpy is a toolkit for working with numeric data in Python. Said differently, it’s a set of tools for doing data manipulation with numbers.

More specifically, Numpy operates on special *arrays* of numbers, called Numpy arrays.

##### Numpy arrays are arrays of numbers

What is a Numpy array?

You can think of a Numpy array like a vector or a matrix in mathematics.

Like a matrix, a Numpy array is just a grid of numbers.

These Numpy arrays can be 1-dimensional … like a vector:

And Numpy arrays can be 2-dimensional:

They can also have *more than two dimensions*. These higher-dimensional Numpy arrays are like tensors in mathematics (and they are often used in advanced machine learning processes like Python’s Keras and TensorFlow).

##### Numpy arrays have a shape

One thing to remember about Numpy arrays is that they have a *shape*.

The shape of a Numpy array is essentially the number of rows and columns. (Or more technically, the number of units along each axis of the array.)

So let’s say that you have a 2-dimensional Numpy array.

This array has a shape of `(2, 4)`

because it has two rows and four columns.

You need to know about Numpy array shapes because when we create new arrays using the numpy.full function, we will need to specify a shape for the new array with the `shape =`

parameter.

You’ll read more about this in the syntax section of this tutorial.

##### Numpy provides functions for creating and manipulating arrays

Essentially, Numpy just provides functions for creating these numeric arrays and manipulating them.

For example, we can use Numpy to perform summary calculations. We can use Numpy functions to calculate the mean of an array or calculate the median of an array.

And Numpy has functions to change the shape of existing arrays. So we use Numpy to combine arrays together or reshape a Numpy array.

But before we do any of those things, we need an array of numbers in the first place.

Numpy has a variety of ways to create Numpy arrays, like Numpy arrange and Numpy zeroes.

One of the other ways to create an array though is the Numpy full function.

## A quick introduction to Numpy full

The Numpy full function is fairly easy to understand.

It essentially just creates a Numpy array that is “full” of the same value.

So for example, you could use it to create a Numpy array that is filled with all 7s:

It can get a little more complicated though, because you can specify quite a few of the details of the output array. For example, you can specify how many rows and columns. You can also specify the data type (e.g., `integer`

, `float`

, etc).

That being said, to really understand how to use the Numpy full function, you need to know more about the syntax.

## The syntax of np.full

The syntax of the Numpy full function is fairly straight forward.

Moreover, if you’ve learned about other Numpy functions, some of the details might look familiar (like the `dtype`

parameter).

But on the assumption that you might need some extra help understanding this, I want to carefully break the syntax down.

### np.full syntax

To call the Numpy full function, you’ll typically use the code `np.full()`

.

Having said that, you need to remember that how exactly you call the function depends on how you’ve imported numpy. If you’ve imported Numpy with the code `import numpy as np`

then you’ll call the function as `np.full()`

. But if you’ve imported numpy differently, for example with the code `import numpy`

, you’ll call the function differently.

For the most part here, I’ll refer to the function as np.full.

Ok. So you call the function with the code `np.full()`

. Then inside of the function there are a set of parameters that enable you to control exactly how the function behaves.

Let’s take a closer look at those parameters.

### The parameters of np.full

The three main parameters of np.full are:

`shape`

`fill_value`

`dtype`

There’s actually a fourth parameter as well, called `order`

. However, we don’t use the `order`

parameter very often, so I’m not going to cover it in this tutorial.

Let’s examine each of the three main parameters in turn.

`shape`

(required)

The `shape`

parameter specifies the shape of the output array.

Remember, the output of the Numpy full function is a Numpy array.

Also remember that all Numpy arrays have a *shape*. The shape of a Numpy array is the number of rows and columns.

When we specify a shape with the `shape`

parameter, we’re essentially specifying the number of rows and columns we want in the output array. That’s it.

To do this, we need to provide a number or a list of numbers as the argument to `shape`

.

If we provide a single integer `n`

as the argument, the output will be a 1-dimensional Numpy array with `n`

observations.

But if we provide a list of numbers as the argument, the first number in the list will denote the number of rows and the second number will denote the number of columns of the output. (And if we provide more than two numbers in the list, np.full will create a higher-dimensional array.)

This might not make a lot of sense yet, but sit tight. I’ll show you examples in the examples section of this tutorial.

`fill_value`

The `fill_value`

parameter is easy to understand.

It’s the value that you want to use as the individual elements of the array.

So if you set `fill_value = 7`

, the output will contain all 7s. If you set `fill_value = 102`

, then every single element of the output array will be 102.

`dtype`

Finally, there’s the dtype parameter.

This just enables you to specify the data type of the elements of the output array.

By default, Numpy will use the data type of the `fill_value`

. So if your fill value is an integer, the output data type will be an integer, etc.

But you can manually specify the output data type here.

In the simplest cases, you’ll use data types like `int`

(integer) or `float`

, but there are more complicated options since Numpy recognizes a large variety of data types.

For the sake of simplicity, I’m not going to work with any of the more exotic data types … we’ll stick to floats and ints.

Ok … now that you’ve learned about the syntax, let’s look at some working examples.

## Numpy full examples

Using Numpy full is fairly easy once you understand how the syntax works.

Still, I want to start things off simple. We’ll start with simple examples and increase the complexity as we go.

**Examples:**

- Create a 1-dimensional array filled with the same number
- Create a 2-dimensional array filled with the same number
- Modify the data type of the output array

#### Run this code first

One quick note before you run any code …

You need to make sure to import Numpy properly.

To import Numpy run this code:

import numpy as np

This will enable us to call functions from the Numpy package.

(Note: this assumes that you already have Numpy *installed*. If you don’t have Numpy installed, the import statement won’t work! If you don’t have Numpy installed, I recommend using Anaconda.)

Ok, with that out of the way, let’s look at the first example.

### EXAMPLE 1: Create a 1-dimensional array filled with the same number

This first example is as simple as it gets.

We’re going to create a Numpy array filled with all 7s.

np.full(shape = 3, fill_value = 7)

And here’s the output:

array([7, 7, 7])

This is really simple.

To specify that we want the array to be filled with the number ‘7’, we set `fill_value = 7`

.

By setting `shape = 3`

, we’re indicating that we want the output to have three elements.

So the code `np.full(shape = 3, fill_value = 7)`

produces a Numpy array filled with three 7s.

###### Redo without parameter names

Quickly, I want to redo that example *without* the explicit parameter names.

Among Python programmers, it’s *extremely* common to remove the actual parameters and to only use the arguments to those parameters.

Let me show you:

np.full(3, 7)

OUT:

array([7, 7, 7])

In terms of output, this the code `np.full(3, 7)`

is equivalent to `np.full(shape = 3, fill_value = 7)`

. The output is exactly the same. Numpy knows that the “3” is the argument to the `shape`

parameter and the “7” is the argument to the `fill_value`

parameter.

Unfortunately, I think `np.full(3, 7)`

is harder to read, particularly if you’re a beginner and you haven’t memorized the syntax yet.

Having said that, I think it’s much better as a best practice to explicitly type out the parameter names.

### EXAMPLE 2: Create a 2-dimensional array filled with the same number

Next, let’s create a 2-dimensional array filled with the same number.

Here, we’re going to create a 2 by 3 Numpy array filled with 7s.

To do this, we’re going to call the np.full function with `fill_value = 7`

(just like in example 1). This will fill the array with 7s.

But to specify the *shape* of the array, we will set `shape = (2,3)`

.

Let’s take a look:

np.full(shape = (2,3), fill_value = 7)

Which creates the following output:

array([[7, 7, 7], [7, 7, 7]])

This is very straight forward.

By setting `shape = (2,3)`

, we’re indicating that we want the output to have 2 rows and and 3 columns. The code `fill_value = 7`

fills that 2×3 array with 7s.

###### Note: we can create arrays of different sizes and shapes

In the example above, I’ve created a relatively small array. Two rows and three columns.

But understand that we can create arrays that are much larger.

Here’s an example of a 5×20 array:

np.full(shape = (5,20), fill_value = 7)

OUT:

array([[7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]])

You could even go a step further and create an array with thousands of rows or columns (or more).

Like almost all of the Numpy functions, np.full is flexible in terms of the sizes and shapes that you can create with it.

### EXAMPLE 3: Modify the data type of the output array

Now, let’s build on example 2 and increase the complexity just a little.

Here, we’re going to create a Numpy array that’s filled with floating point numbers instead of integers.

Just like in example 2, we’re going to create a 2×3 array filled with 7s.

Now remember, in example 2, we set `fill_value = 7`

. But notice that the value “7” is an *integer*. Because of this, np.full just produced an output array filled with integers. That’s the default. By default, the output data type matches the data type of `fill_value`

.

It’s possible to override that default though and manually set the data type by using the `dtype`

parameter.

Let’s take a look:

np.full(shape = (2,3), fill_value = 7, dtype = float)

OUT:

array([[7., 7., 7.], [7., 7., 7.]])

Here, we have a 2×3 array filled with 7s, as expected.

But notice that the array contains floating point numbers. You can tell, because there is a decimal point after each number. You could also check the dtype attribute of the array with the code `np.full(shape = (2,3), fill_value = 7, dtype = float).dtype`

, which would show you that the data type is `dtype('float64')`

.

This is a simple example with a fairly familiar data type. Just keep in mind that Numpy supports a wide range of data types, including a few “exotic” options for Numpy (try some cases with `dtype = np.bool`

).

### EXAMPLE 4: Create a 3-dimensional array with np.full

For the final example, let’s create a 3-dimensional array.

So far, we’ve been creating 1-dimensional and 2-dimensional arrays. We’ve been sticking to smaller sizes and shapes just to keep the examples simple (when you’re learning something new, start simple!).

But you need to realize that Numpy in general, and np.full in particular can work with very large arrays with a large number of dimensions.

So let’s look at the slightly more complicated example of a 3D array.

To do this, we’re going to provide more arguments to the `shape`

parameter.

Remember from the syntax section and the earlier examples that we can specify the shape of the array with the `shape`

parameter. If we provide a single number as the argument to `shape`

, it creates a 1D array. If we provide a list of *two* numbers (i.e., `shape = [2,3]`

), it creates a 2D array.

So how do you think we create a 3D array?

Pass an extra number to `shape`

.

Let’s take a look.

np.full(shape = (2,3,4), fill_value = 7)

OUT:

array([[[7, 7, 7, 7], [7, 7, 7, 7], [7, 7, 7, 7]], [[7, 7, 7, 7], [7, 7, 7, 7], [7, 7, 7, 7]]])

As you can see, this produces a Numpy array with 2 units along axis-0, 3 units along axis-1, and 4 units along axis-2.

As a side note, 3-dimensional Numpy arrays are a little counter-intuitive for most people. I hesitate to use the terms ‘rows’ and ‘columns’ because it would confuse people. I’ll probably do a separate blog post to explain 3D arrays in another place.

Having said that, just be aware that you *can* use Numpy full to create 3-dimensional and higher dimensional Numpy arrays.

## Numpy full FAQ

Now that you’ve seen some examples and how Numpy full works, let’s take a look at some common questions about the function.

**Frequently asked questions:**

- Can you fill a Numpy array with True or False?
- Is Numpy full slower than Numpy zeros and Numpy empty?

### Can you fill a Numpy array with True or False?

Yes, actually you can.

np.full(shape = [2,2], fill_value = True, dtype = bool)

And here is the output:

array([[ True, True], [ True, True]])

As you can see, the code creates a 2 by 2 Numpy array filled with the value `True`

.

Note that there are actually a few other ways to do this with np.full, but using this method (where we explicitly set `fill_value = True`

and `dtype = bool`

) is probably the best.

### Is Numpy full slower than Numpy zeros and Numpy empty?

If you’re just filling an array with the value zero (0), then the Numpy zeros function is faster.

You can check it out by timing them.

%timeit np.zeros(shape = 100000000) %timeit np.full(shape = 100000000, fill_value = 0)

You can learn more about Numpy zeros in our tutorial about the np.zeros function.

Having said that, if your goal is simply to initialize an empty Numpy array (or an array with an arbitrary value), the Numpy empty function is faster.

You can learn more about Numpy empty in our tutorial about the np.empty function.

##### Leave your other questions in the comments below

If you have questions about the Numpy full function, leave them in the comments.

## There’s more to learn about Numpy

This tutorial should tell you almost everything you need to know about the Numpy full function.

But if you’re new to using Numpy, there’s a *lot* more to learn about Numpy more generally.

For example, there are several other ways to create simple arrays. You can create an empty array with the Numpy empty function. Or you can create an array filled with zeros with the Numpy zeros function. And obviously there are functions like np.array and np.arange.

Moreover, there are quite a few functions for *manipulating* Numpy arrays, like np.concatenate, which concatenates Numpy arrays together.

There’s also a variety of Numpy functions for performing summary calculations (like np.sum, np.mean, etc).

My point is that if you’re learning Numpy, there’s a lot to learn.

And it doesn’t stop there … if you’re interested in data science more generally, you will need to learn about matplotlib and Pandas.

## For more data science tutorials, sign up for our email list

If you want to learn more about Numpy, matplotlib, and Pandas …

… if you want to learn about data science …

Then sign up for our email list.

Here at Sharp Sight, we teach data science.

And on a regular basis, we publish FREE data science tutorials.

If you sign up for our email list you’ll get our free tutorials delivered directly to your inbox.

You’ll learn about:

- Numpy
- Pandas
- Base Python
- Scikit learn
- Machine learning
- Deep learning
- … and more.

If you want to learn more about data science, then sign up now:

It is way too long with unnecessary details of even very simple and minute details

Clear explanation is how we do things here.

Frequently, that requires careful explanation of the details, so beginners can understand.

There are plenty of other tutorials that completely lack important details.

Read something else.

I personally love the way sharp sights does his thing.

His breakdown is perfectly aimed at beginners and this is one thing many tutors miss when teaching… they feel everyone should have known this or that and THAT’S NOT ALWAYS THE CASE!

If some details are unnecessary, just scroll to the section you need, pick your information and off you go! He has not forced anyone to read everything.

I love your way Sharp Sights… Keep it up. I’m a beginner and these posts are really helpful and encouraging.

Thanks again for your feedback, Emmanuel.

Clear explanation is how we do things here at Sharp Sight.

We try to explain the important details as clearly as possible, while also avoiding unnecessary details that most people don’t need.

That’s one of the ways we help people “master data science as fast as possible.”

And just a reminder:

If you like our free tutorials and want to get more, then share them with your friends.

If we can expand the audience, we’ll be able to hire more people and create more free tutorials for the blog.