Member-only story
Hi everyone, welcome back. NumPy is a library for the Python programming language. NumPy is short for “Numerical Python” and is a popular library that is used in data science. NumPy is used specifically to work with arrays as it provides various functions and support. In this tutorial, we will go over the copy and view functions that can be used on NumPy arrays. With this introduction out of the way, let’s get into it.
Copy() Function
Let’s start by looking at the copy() function:
import numpy as np
array1 = np.array([1, 2, 3])
array2 = array1.copy()
print(array2)Output:
[1 2 3]
We created our first array, then created a copy of it and stored it into our array2 variable. We printed the array2 variable and as we can see in the output, it is the same as the original array.
View() Function
Now let’s look at the view() function:
import numpy as np
array1 = np.array([1, 2, 3])
array2 = array1.view()
print(array2)Output:
[1 2 3]
We did the same exact thing except we changed the copy function to the view function. Our output, verifies that our new array is the same as the original array.