NumPy: Copy Function

 

In this guide we are going to be discussing NumPy’s copy function as well as how NumPy manages your computer’s memory when performing tasks.    

Let’s start by creating a variable named “array_a” and assigning it to a NumPy array with a range of six. From here we’re going to create a second array named “array_b” and that will be assigned to “array_a”. Now, let’s go ahead and return both object identifiers as well.

And as you probably expected the objects share the same id and location in memory.

Now, let’s modify array_b by assigning it to a slice of array_a, from zero to three. And when we run this our objects are now assigned to their own ID. 

While it seems like these two objects are stored in their own location that’s not really what’s going on. What NumPy is doing is making a shallow copy, or view, of the original base array. The view is actually part of the base array even though it looks like you’re working with something else.    

  

It’s also important to keep in mind that since array_b is a reference to array_a, any change made to array_b will modify array_a as well. So, really quickly we’ll change the first element in array_b from zero to one hundred, then we’ll once again return both arrays. 

And as expected, both arrays have been modified. 

In contrast to a view, NumPy allows us to explicitly create copies of an array as well. Copies are separate objects from the original array, though right after copying the two look the same. And unlike a view that creates a shallow copy to share memory with the base array, the copy function creates what we call a deep copy that has its own location in memory.

  

Okay, now let’s make a third array called array underscore c and assign that to our first array, then we’ll pass in our copy function. And since array_c doesn’t share memory with array_a or array_b any changes we make will be independent from the others. So, let’s change the first element of array_c back from one hundred to zero. 

And everything worked perfectly.

Finally, I’d like to show you a couple tricks to help you figure out if you’re working with a view or copy. As you’ve seen throughout this guide simply calling for the ID doesn’t work. And so the first method leverages a NumPy function that returns a boolean, and it’s the may share memory function. It’s also important to note that a return of True doesn’t necessarily mean that the two arrays share any element. It just means that they might.

    

The second method requires us to ask if an array is the base array, or if it shares memory from another array. To determine if an array is a base array we pass in the array in question dot base followed by, is None. If the array is a base array True will be returned. If we know that an array isn’t a base array and want to determine which array it’s a view of, we start the same way by passing in for example array_b dot base followed by is array_a. If array_a is the base array for array_b then we’ll have True returned again. And so I’m going to pass in a couple of these statements, and we will run this all at once. 

And as you can see everything that we already knew is once again confirmed. 

 



 

FINAL CODE: