Return a new array of given shape and type, filled with fill_value.
Parameters : shape int or sequence of ints
Shape of the new array, e.g., (2, 3) or 2 .
fill_value scalar or array_like
dtype data-type, optional The desired data-type for the array The default, None, means
order <‘C’, ‘F’>, optional
Whether to store multidimensional data in C- or Fortran-contiguous (row- or column-wise) order in memory.
like array_like, optional
Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the __array_function__ protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.
How to Use Numpy Full
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.
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.
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.
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.
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.
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:
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.
And here’s the output:
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:
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:
Which creates the following output:
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:
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:
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.
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?
Yes, actually you can.
And here is the output:
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.
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:
- Base Python
- Scikit learn
- Machine learning
- Deep learning
- … and more.
If you want to learn more about data science, then sign up now:
Sign up for FREE data science tutorials
If you want to master data science fast, sign up for our email list.
When you sign up, you’ll receive FREE weekly tutorials on how to do data science in R and Python.
NumPy full: Create Arrays With a Fill Value
NumPy arrays are essential to most data analysis and data science workflows in Python. Because of this, being able to generate arrays is an important skill. In this tutorial, you’ll learn how to use the NumPy full() function to generate arrays filled with a given value. This function is similar to the NumPy zeroes() and NumPy ones() functions, though it generates arrays of different shapes filled with a given number.
By the end of this tutorial, you’ll have learned:
- How the NumPy full() function works
- How to generate one-dimensional and two-dimensional arrays filled with a number
- How to set the data type when generating arrays with NumPy full()
Table of Contents
Understanding the NumPy full() Function
The NumPy full() function is used to generate arrays filled with a given number. The function can generate one-dimensional and two-dimensional arrays of a specified data type.
Before diving into using the NumPy full() function, let’s explore how the function is made up. This allows you to better understand the parameters and default arguments of the function. Knowing this prepares you to understand how to customize the output of the resulting function.
We can see that the function has four different parameters, two of which have default arguments provided. The table below breaks down these arguments in detail:
|Parameter||Description||Default Argument||Accepted Values|
|shape=||The shape of the new array.||N/A||int or sequence of ints|
|fill_value=||The fill value to use.||N/A||scalar or array-like|
|dtype=||The data type for the array. If None is provided, then the data type will be that of the fill value passed in.||None||data type|
|order=||Whether to store multidimensional data in C- for Fortan-style orer.||‘C’||‘C’ or ‘F’|
The parameters and default arguments of the NumPy full() function
Now that you have a strong understanding of the function, let’s dive into how we can use the function to generate arrays.
Generate a 1-D NumPy Array Filled With the Same Number
NumPy makes it very easy to create one-dimensional arrays filled with a given value. This can be done by passing a single integer into the shape= parameter. This allows you to specify the length of the resulting array. By passing a second value into the function, you can specify the value you want to fill the array with.
Let’s generate an array with five values, containing the value 3:
In the code block above, we pass the values of 5 and 3 into the full() function. Because the shape= and fill_value= parameters are the first and second values positionally, we can avoid using their keywords.
Generate a 2-D NumPy Array Filled With the Same Number
To generate a two-dimensional NumPy array filled with a given value, you can pass a tuple of values into the shape parameter. This allows you to set the dimensions of the resulting array. Let’s see how we can create a 3 by 3 array containing the value 3.
Let’s break down what we did above:
- We passed in (3, 3) to declare that we want a 3 by 3 array
- We also passed in the fill value of 3, to fill the array with the value of 3
In the following section, you’ll learn how to specify the data type when generating NumPy arrays.
Modify the Data Type When Generating NumPy Arrays with full()
By default, the NumPy full() function will use the data type of the fill value that was passed in. This means that, in the previous examples, the data type of the value was an integer. However, we can specify the data type of the elements in the array by using the dtype= parameter. Let’s see how we can recreate the array from above, using floating point values.
By passing in ‘float’ as the third argument, we can specify the data type of the resulting array to be of floats. This is despite passing in an integer as the fill value argument.
NumPy: numpy.full() function
The numpy.full() function is used to create a new array of the specified shape and type, filled with a specified value.
|shape||Shape of the new array, e.g., (2, 3) or 2.||Required|
|dtype||The desired data-type for the array The default, None, means np.array(fill_value).dtype.||optional|
|order||Whether to store multidimensional data in C- or Fortran-contiguous (row- or column-wise) order in memory||optional|
[ndarray] Array of fill_value with the given shape, dtype, and order.
Example: Create arrays filled with a constant value using numpy.full()
The above code creates arrays filled with a constant value using the numpy.full() function. In the first example, np.full((3, 3), np.inf) creates a 3×3 numpy array filled with np.inf (infinity). np.inf is a special floating-point value that represents infinity, and is often used in calculations involving limits and asymptotes.
In the second example, np.full((3, 3), 10.1) creates a 3×3 numpy array filled with the value 10.1. Here, the dtype parameter is omitted, so numpy infers the data type of the array from the given value.
Example: Create an array filled with a single value using np.full()
In the above code, np.full((3,3), 55, dtype=int) creates a 3×3 numpy array filled with the integer value 55. The dtype parameter is explicitly set to int, so the resulting array has integer data type.
Python — NumPy Code Editor:
Follow us on Facebook and Twitter for latest update.
- Weekly Trends
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook