Select Page

This tutorial will explain how to use the NumPy repeat function, which is also called np.repeat or numpy.repeat.

Numpy repeat repeats the elements of an array

The NumPy repeat function essentially repeats the numbers inside of an array. It repeats the individual elements of an array.

Having said that, the behavior of NumPy repeat is a little hard to understand sometimes. It’s a little un-intuitive.

To help explain how this function works, I’m going to explain everything step by step. I’ll review the basics of NumPy arrays and how they are structured. Then I’ll explain the NumPy repeat function. Next, I’ll explain the actual syntax of np.repeat, before moving on to some examples and questions.

Table of Contents:

You can click on any of the above links and it will take you to the appropriate section in the tutorial.

But if you really want to understand how this works, I recommend that you read the full tutorial.

Ok. Onward.

A quick review of NumPy arrays

First, let’s just review NumPy arrays.

A NumPy array is just an array of numbers that we create with the NumPy package. It’s basically a special kind of object in Python that we use to store and manipulate numeric data.

NumPy arrays can be 1-dimensional:

An example of a 1-dimensional NumPy array

… 2-dimensional:

An example of a 2-dimensional NumPy array with the numbers 0 to 7.

Or even have 3 or more dimensions.

The real constraint with NumPy arrays is that they essentially can only hold numbers.

You can think of NumPy arrays like vectors, matrices, and tensors in mathematics.

NumPy arrays have axes

Importantly, NumPy arrays have “axes.”

What are axes?

You can think of axes like directions.

A 1-dimensional array only has one axis, called axis-0:

An image that shows that a 1-dimensional NumPy array has one axis, called axis-0

And 2-dimensional arrays have two axes, called axis-0 and axis-1:

An image that shows that a 2-dimensional NumPy array has two axes, axis-0 and axis-1.

As it turns out, NumPy axes are important because they are often used when we manipulate arrays.

We’ll actually use NumPy axes when we manipulate our NumPy arrays with NumPy repeat.

A quick introduction to NumPy repeat

Now that we’ve reviewed NumPy arrays, let’s talk more specifically about the NumPy repeat function.

NumPy repeat “repeats” elements of an array

Essentially, the NumPy repeat function repeats the elements of an array.

A simple illustration of how NumPy repeat works.

That’s basically all it does!

It can get a little more complicated though in more complicated cases.

NumPy repeat creates flat 1-dimensional arrays by default

One important thing to understand is that by default, NumPy repeat will flatten out an array.

That might not make sense, so let me give you an example. Let’s say that you have a 2-dimensional array with some numbers.

If you apply the np.repeat function on the 2-dimensional input array, then by default, it will repeat the values and put those repeated values into a 1-dimensional array.

An illustration of how np.repeat 'flattens' out input arrays by producing 1-dimensional output arrays.

By default, the output will be 1-dimensional.

It’s possible to change that behavior, but to do so, you need to understand the syntax of np.repeat.

That being said, let’s take a look at the syntax.

The syntax of np.repeat

The syntax of the np.repeat function is fairly straightforward.

First of all, you will typically call the function with the name np.repeat().

How you call the function depends on how you import NumPy

However, it’s important to note that how exactly you call the function depends on how you’ve imported NumPy.

Remember that before you use NumPy in a Python program, you need to import NumPy.

The common way to do this is with the code import numpy as np. That’s a very common convention used by many programmers.

When you import NumPy with the alias “np,” it allows you to refer to the NumPy package with the prefix “np.” in your syntax.

So if you import NumPy with the code import numpy as np, then you will call the repeat function as np.repeat().

On the other hand, if you instead import NumPy with the code import numpy, you’ll have to use the prefix “numpy.” in your syntax, which means that you’d have to call the function as numpy.repeat().

Essentially, how you call the function depends on how you import NumPy.

Having said that, I’ll mostly refer to the function as np.repeat(), but you might see me use the term numpy.repeat() as well.

np.repeat syntax

Let’s finally take a close look at the syntax of np.repeat.

First, you’ll typically call the function with the name np.repeat().

An image that shows the syntax of np.repeat with the important function parameters.

Then inside of the function are a set of parameters that you can use to specify exactly how the function will work.

The parameters are important, so let’s take a look at them.

The parameters of np.repeat

The np.repeat function has 3 parameters:

  • a
  • repeats
  • axis

Let’s examine each of these individually.

a (required)

The a parameter enables you to specify the “array” that you will use as the input to np.repeat.

Keep in mind that although the np.repeat function is primarily designed to operate on NumPy arrays, it will also work on any array-like object. That includes Python lists and Python tuples. Technically, it will even operate on a string (although it will treat it like a Python list). The important thing to understand is that np.repeat is flexible concerning what inputs it will work on.

Also keep in mind that the a parameter is required. This means that you must provide some sort of input to the function.

repeats (required)

The repeats parameter enables you to specify how many times to repeat the items in the input array.

The argument to this parameter will be an integer …

… so if you want to repeat each item twice, you’ll type ‘2‘.

… if you want to repeat each item three times, you’ll type ‘3‘.

… etcetera.

This is fairly self explanatory, but you’ll be able to see how this works in practice in the examples section of the tutorial.

axis

Finally, the axis parameter enables you to specify the axis along which you will repeat the elements.

This is a little confusing to NumPy beginners, because most people don’t really understand what axes are.

Remember, NumPy arrays have axes. A 1D NumPy array has one axis, and a 2D array has two axes.

An illustration of the axes of a 2-dimensional NumPy array.

These “axes” are essentially like directions.

When we use the axis parameter, we are specifying the direction along which we will repeat the items.

So for example, if we have a 2-dimensional NumPy array and we want to repeat the items downwards down the columns, we will set axis = 0.

If we want to repeat the items horizontally across the rows, we will set axis = 1.

(Although, keep in mind that the axis specifications will be different for a 1-dimensional array.)

Again, this might not make a lot of sense. If you’re confused about NumPy axes, make sure to read our tutorial that explains what a NumPy axis is.

You should also pay close attention to example 4 and example 5 in this tutorial which explain how to repeat elements down the columns or across the rows of a 2D array.

The output of np.repeat

Finally, let’s talk about the output of np.repeat.

This is simple.

The output of NumPy repeat is a NumPy array.

But keep in mind that the size and exact shape of the output will depend on how we use the np.repeat function.

NumPy repeat examples

Now that I’ve explained the syntax, let’s look at some examples of how to use NumPy repeat.

Examples:

Run this code first

One quick note before you run any code:

You need to import NumPy properly.

I’m assuming here that you have NumPy already installed on your machine (if you don’t, you should install Anaconda, which will include NumPy).

But even if you have NumPy installed on your computer, you will need to import it into your working environment by using the following code:

import numpy as np

This essentially makes the NumPy functions available in our code by typing the prefix “np.” followed by the name of the function.

Ok. With that out of the way, let’s look at some examples.

Example 1: Repeat a single number

First, let’s start veeeery simple.

Here, we’re just going to repeat a single number several times.

To do this, we’ll call the np.repeat function. Inside of the parenthesis, we’ll provide the value that we want to repeat with the a = parameter, and specify the number of repeats with the repeats = parameter.

Essentially, we’re going to repeat the number “7” four times.

Here’s the code:

np.repeat(a = 7, repeats = 4)

Which produces the following output:

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

This is really easy to understand.

We indicate that we will repeat the number ‘7’ with the a parameter, and indicate that we will repeat it four times with the repeats parameter.

A simple example of np.repeat where we repeat the number 7 four times.

Notice also that the output is a NumPy array. If you want, you could check that with the code type(np.repeat(a = 7, repeats = 4)).

Redo the example without the parameter names

Very quickly, let’s redo the above examples without explicitly referencing the parameter names.

For better or for worse, many Python programmers write their code without explicitly showing the parameter names.

Personally, I think this is a bad habit (one that I’ve been guilty of myself). It’s much more clear to explicitly show the parameter names.

But just so you can see it, let’s run the code without the parameter names. We’ll just pass arguments to the function by position.

np.repeat(7, 4)

OUT:

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

The output is still the same.

The only difference is that we’ve removed the actual parameters. Python knows that the argument in the first position refers to the “a” parameter and the item in the second position refers to the “repeats” parameter.

The code looks a little different, but it’s essentially the same and the output is the same.

Again, I want you to see this so you understand what’s going on when the parameter names are removed.

Example 2: Repeat a 1-d list of numbers multiple times

Now, we’re going to move on to a slightly more complicated example.

Instead of repeating a single number, we’re going to repeat multiple numbers.

This example is almost exactly the same as the first example, except we’re going to make one small modification to our code.

… we’re going to provide a list of numbers to the a parameter.

Let’s take a look.

Here, we’re going to repeat the numbers 6 and 7 two times each. We specify the two numbers to repeat with the code a = [6,7]. Then the parameter repeats = 2 indicates that we’ll repeat those numbers two times.

np.repeat(a = [6,7], repeats = 2)

OUT:

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

Notice what happened here.

Our input numbers, 6 and 7, are repeated two times each.

And notice how it repeats the numbers. It repeats the first number two times and then repeats the second number two times.

The output is a NumPy array, as we should expect, but it’s a 1-dimensional NumPy array.

This is important!

By default, the output of numpy.repeat is a flat, 1-dimensional array that simply repeats all of the elements of the input.

We’ll see this behavior in the next example as well, and we’ll only be able to change it when we finally start to use the axis parameter.

Example 3: Repeat the values of a 2-d array

In this example, we’re going to use a 2-d array as the input to NumPy repeat.

Create numpy array

First, let’s create a NumPy array.

Here, we’re going to use the NumPy array function to create a 2-dimensional array with four numbers.

np_array_2d = np.array([[6,7],[8,9]])

We can print this out with the code print(np_array_2d) which produces the following output:

[[6 7]
 [8 9]]

The resulting NumPy array, np_array_2d, is basically an array with 2 rows and two columns.

Repeat values with np.repeat

Now, we’re going to use np.repeat to repeat the elements of the array.

Here’s the code:

np.repeat(a = np_array_2d, repeats = 2)

OUT:

array([6, 6, 7, 7, 8, 8, 9, 9])

This example is actually easy to understand if you understood example 1 and example 2.

The array np_array_2d is the input to the function. We’re using the repeats parameter to specify that we want to repeat every element of the input two times.

An example of using np.repeat on a 2-dimensional input array.

When it executes, the np.array function simply takes every individual element of the input, repeats it 2 times, and puts it into a 1-dimensional output array.

Example 4: Repeat values down the columns

Now, let’s make things more complicated.

We’re going to work with a 2-dimensional NumPy array again.

But we’re now going to use numpy.repeat to repeat the values downward down each column.

This is a little more complicated and more difficult to understand, so let’s take it step by step.

Create numpy array

First let’s just create a 2-d NumPy array.

This is the same as the array in the previous example, so if you already ran that code, you don’t need to run this again.

np_array_2d = np.array([[6,7],[8,9]])

If we print this out with the code print(np_array_2d), you can see that it’s a 2×2 array with four values:

[[6 7]
 [8 9]]
Repeat values downward using axis = 0

Now, we’re going to repeat the values of each column downward down each column.

Let’s first run the code and then let me explain.

Here, we’re going to run np.repeat with 2 repeats. But we’re now going to use the axis parameter.

np.repeat(a = np_array_2d, repeats = 2, axis = 0)

OUT:

array([[6, 7],
       [6, 7],
       [8, 9],
       [8, 9]])
Explanation

So look at what happened …

The np.repeat function repeated the values of each column downward.

Why?

To understand this, you need to understand NumPy axes.

Remember that NumPy arrays have axes. The axes of an array are like directions. For a 2-dimensional array, axis-0 is the axis that points downwards down the columns and axis-1 is the axis that points horizontally across the rows.

An image of a NumPy array that labels axis-0 and axis-1.

When we use numpy.repeat with axis = 0, we’re telling numpy.repeat to operate in the axis-0 direction.

So when we set axis = 0, it takes the values of the array and it repeats them in that direction. Remember, axis-0 points downwards down the columns. So in this case, it repeats the values downwards down the columns.

An image that shows how np.repeat repeats the elements downwards down axis-0 when we set axis=0.

The behavior of np.repeat actually makes sense, but to really understand this, you really need to understand NumPy array axes first. So if you’re a little confused by this example, you might want to read our tutorial about NumPy axes.

Example 5: Repeat elements across the rows

Next, let’s do something similar to the last example. In the previous example, we repeated elements down the columns.

In this example, we will repeat elements horizontally across the rows.

This is very easy to do and it will make sense if you read the previous example.

We’re essentially going to set axis = 1 which indicates that we want np.repeat to operate in the axis-1 direction.

Create numpy array

Again, let’s create a 2-d NumPy array.

This is the same as the array in the last example, so if you already ran that code, you don’t need to run this again.

np_array_2d = np.array([[6,7],[8,9]])

If we print this out with the code print(np_array_2d), you can see the contents. It’s a 2-dimensional NumPy array:

[[6 7]
 [8 9]]
Repeat values across using axis = 1

Now, let’s repeat those values horizontally.

To do this, we’ll use np.repeat with axis = 1.

np.repeat(a = np_array_2d, repeats = 2, axis = 1)

And here’s the output:

array([[6, 6, 7, 7],
       [8, 8, 9, 9]])
Explanation

This is very similar to the previous example where we repeated values downward.

But here, by setting axis = 1, we’re telling np.repeat that we want to repeat the values in the axis-1 direction. Remember, for a 2-dimensional array, axis-1 is the direction that points horizontally across the rows.

An image that shows how np.repeat repeats the elements horizontally across axis-1 when we set axis=1.

So essentially, this code will take each element in the input array and repeat it horizontally across axis-1.

Again, this is fairly easy to understand once you really understand NumPy axes. To learn more about axes, read our tutorial that explains what a NumPy axis is and how we use them.

Frequently asked questions about NumPy repeat

Now let’s address some frequently asked questions about NumPy repeat.

Frequently asked questions:

How can I duplicate the rows of a 2-D array?

If you already have a 2-dimensional NumPy array, then it’s relatively easy to duplicate all of the rows.

To do this, we’re simply going to use np.repeat with axis = 0.

Let me show you.

First, we’ll just create a 2D numpy array:

np_array_2d = np.array([[1,2,3],[4,5,6]])

Now, let’s duplicate the rows with NumPy repeat:

np.repeat(a = np_array_2d, repeats = 2, axis = 0)

OUT:

array([[1, 2, 3],
       [1, 2, 3],
       [4, 5, 6],
       [4, 5, 6]])

As you can see, np.repeat has produced an output array that repeats every row of the input.

Note that this is essentially the same as example 4 in the examples section of this tutorial.

How can I duplicate a 1-dimensional array?

Duplicating a 1D array is a little harder because a 1-dimensional array only has one axis (axis 0).

This is a problem, because when we duplicate the rows, there needs to be another dimension in which to copy the rows.

So essentially, to do this, we actually need to increase the number of dimensions of the array first, and then copy the row.

Let’s take a look.

First, let’s just create a 1-dimensional NumPy array.

np_array_1d = np.array([1,2,3,4])

If you were to use np.repeat on this with axis = 1, then you’d get an error.

So before we run that code, we need to expand the number of dimensions.

np_array_2d = np.expand_dims(np_array_1d, axis = 0)

And let’s print this out so you can see it.

print(np_array_2d)

OUT:

[[1 2 3 4]]

Now, let’s duplicate the row.

np.repeat(a = np_array_2d, repeats = 2, axis = 0)

OUT:

array([[1, 2, 3, 4],
       [1, 2, 3, 4]])

As you can see, the output of np.repeat in this case is a new array with the row duplicated two times.

What is the difference between np.repeat and np.tile?

The np.repeat function repeats the individual elements of an input array.

But np.tile will take the entire array – including the order of the individual elements – and copy it in a particular direction.

So the difference is between copying the individual numbers verses copying the whole array all at once.

To learn more about np.tile, check out our tutorial about NumPy tile.

There’s a lot more to learn about NumPy

In this tutorial, we’ve strictly focused on the NumPy repeat function.

But if you’re serious about doing data manipulation in Python (i.e., for machine learning and deep learning), you really need to know more.

I recommend that you learn about:

You’ll also want to learn about summary functions like np.sum, np.median, etc.

Moreover, even after you learn NumPy, you will probably need to learn a little about Pandas and matplotlib.

For more tutorials, sign up for our email list

If you’re serious about learning NumPy (and Pandas, and matplotlib), then we can help.

Here at Sharp Sight, we teach data science.

We can show you how to do data manipulation, data visualization, data analysis, and machine learning in Python.

Want to learn more?

Sign up right now for our email list.

Members of our email list get immediate access to our free tutorials.

When you sign up, you’ll get free tutorials on:

  • NumPy
  • Pandas
  • Base Python
  • Scikit learn
  • Machine learning
  • Deep learning
  • … and more.

You’ll also get exclusive notifications when we open our premium data science courses for enrollment.

Want to learn data science?

Then sign up right now for our email list.