In this tutorial, I’ll explain the Numpy copy function.
I’ll explain the syntax of np.copy and show you a clear example of how to use it.
If you need something specific, you can click on any of the following links.
Table of Contents:
A Quick Introduction to Numpy Copy
As you probably know, Numpy is a toolkit for working with numeric data in Python.
The primary data structure of the Numpy system is the Numpy array: a row-and-column structure that stores numbers.
It’s sort of like an Excel spreadsheet.
And we use functions from Numpy to manipulate, aggregate, and analyze the data that we store in Numpy arrays.
Numpy copy copies Numpy arrays
So what does Numpy copy do?
Numpy copy is a function that ….
… wait for it …
Copies Numpy arrays.
I’m gonna be honest. This is one of the simplest functions in Numpy.
But it’s really important, because there are other “bad” ways to make copies, as I’ll show you in the examples section.
Before we look at an example though, let’s look at the syntax.
The Syntax of Numpy copy
Ok, in this section, we’ll look at the syntax of Numpy copy.
A quick note
Just a reminder:
When we use Numpy functions, we almost always call them with the prefix np
.
This is the standard convention among Python programmers and data scientists.
Additionally, to be able to use this prefix, you must import Numpy as follows:
import numpy as np
Everything else going forward will assume that you’ve imported Numpy like this.
np.copy syntax
The syntax of Numpy copy is very simple.
You call the function as np.copy()
.
Inside the parenthesis, you provide the name of the original Numpy array that you wan to copy as the first argument.
Then, there are two optional parameters that you can use to modify the function behavior.
Let’s quickly look at those.
The parameters and arguments of np.copy
As noted above, there is only one primary argument, and two optional parameters to np.copy:
original_array
order
subok
Let’s look at those parameters a little more closely.
original_array
(required)
This is the Numpy array that you want to copy.
This is required.
Additionally, you should note that instead of a Numpy array, the function will also operate similarly on array-like objects, such as lists and tuples.
order
The order
parameter controls the order of how the values are copied.
There are several possible arguments to this paramter:
'C'
specifies that the array should be copied in C order (row first)'F'
specifies that the array should be copied in Fortran order (column first)'A'
specifies that the array should be copied in Fortran order if the input array is Fortran continuous, otherwise copy in C order'K'
attempts to match the input array as closely as possible
By default, this parameter is set to order = 'K'
.
This parameter is somewhat rarely used, so you probably should’t worry about it too much.
subok
If subok = True
, then any sub classes will be copied over to the output array.
Otherwise, the output array will be a base-class array.
By default, this parameter is set to subok = False
.
Examples of how to use Numpy Copy
Now that you’re learn how the syntax works, let’s look at an example of Numpy copy.
Examples:
Run this code first
Before you run the example, make sure that you import Numpy correctly.
You can import Numpy with this code:
import numpy as np
EXAMPLE 1: Make a copy of an existing Numpy array
Ok. In this example, we’ll make a copy of a pre-existing Numpy array.
This example will have several steps:
- Create a Numpy array
- Make a copy
- Test the copy (to show that it’s a proper copy)
Let’s do each of these, one at a time.
Create a Numpy array
First, we’ll create a Numpy array.
Here, we’re going to create an array with the values from 1 to 6, arranged into an array with 2 rows and 3 columns.
To do this, we’ll use the np.arrange function to generate the values, and Numpy reshape to reshape the array into the correct shape.
my_array = np.arange(start = 1, stop = 7).reshape(2,3)
And let’s quickly print it out:
print(my_array)
OUT:
[[1 2 3] [4 5 6]]
Create a Copy
Next, we’ll create a copy of the array.
Actually, we’re going to create two copies: a “bad” copy and a proper copy.
We’ll compare and contrast the behavior of both, so we can see why Numpy copy is a good tool to use.
Create a “bad” copy
First, we’ll create a “bad” copy.
To do this, we’ll just use the assignment operator to create an array named bad_copy_array
, where the array is set equal to the array we created previously, my_array
.
bad_copy_array = my_array
You’ll see why this is a bad copy in a moment.
Create a “good” copy with np.copy
Next, we’ll create a so-called “good” copy with Numpy copy.
copy_array = np.copy(my_array)
If you print out copy_array
, you’ll see that it contains the same values as my_array
.
Test the copy
And now, let’s test the copy.
First, we’ll actually print the values of bad_copy_array
and copy_array
.
We’ll start with bad_copy_array
.
print(bad_copy_array)
OUT:
[[1 2 3] [4 5 6]]
And now let’s print the contents of copy_array
.
print(copy_array)
OUT:
[[1 2 3] [4 5 6]]
As you can see, both arrays appear to be copies of my_array
.
Change value in original array
Now, we’re going to do something to change the original array, my_array
.
Specifically, we’ll change one of the values.
# change original my_array[-1,-1] = 99 print(my_array)
OUT:
[[ 1 2 3] [ 4 5 99]]
As you can see, the final value in the array has been changed to 99.
Check values in copy arrays
Now, let’s check the values in our copies.
Check the “bad” copy
We’ll start by checking bad_copy_array
.
print(bad_copy_array)
OUT:
[[ 1 2 3] [ 4 5 99]]
OH NO.
It looks like the same value that we changed in my_array
has also been changed in bad_copy_array
.
It’s not really a copy!
Check the copy made with Numpy copy
Now, we’ll check copy_array
, which we made with Numpy copy.
print(copy_array)
OUT:
[[1 2 3] [4 5 6]]
Well look at that.
The values in copy_array
are the same as the values that were originally in my_array
, before we changed my_array
.
Explanation
So what happened here?
We made the “bad” copy using assignment. When we do this, it doesn’t exactly make a copy. Its actually just another name that points at the original. So when you change the original (my_array
), it also changes the “bad” copy (bad_copy_array
).
But we made the “good” copy with Numpy copy. This creates a proper copy. An all new object. So if we change the original, the “good” copy remains unchanged!
The lesson here is that you need to be careful when you use the assignment operator to copy a Numpy array.
In most cases, if you need to a true copy, you should use the Numpy copy function.
Leave your other questions in the comments below
Do you have other questions about Numpy copy?
If so, leave your questions in the comments below.
For more tutorials, sign up for our email list
This tutorial should have helped you understand how to copy Numpy arrays with np.copy.
But if you want to really learn how to do data wrangling and data science in Python, there’s a lot more to learn.
If you’re ready to learn the full range of Python data science skills, then sign up for our free email list.
When you sign up, you’ll get free tutorials on:
- NumPy
- Pandas
- Base Python
- Scikit learn
- Machine learning
- Deep learning
- … and more.
Members of our email list get our free tutorials delivered to their inbox, FREE, several times per month.