Today we are going to learn how to copy a NumPy array. We will try in our code snippet with different methods as well. Hope You guys will practice with us to get your result as expected. Let’s get into it.
What is a NumPy Array?
An array is a collection of data of similar datatypes stored at contiguous memory locations. It is the simplest data structure where each data element can be accessed directly by only using its index number. In Python, an array is a type of data structure of the NumPy library. Some key features of a NumPy array are given as follows.
- It is homogeneous in nature. We can perform all the operations on the array elements.
- The Arrays in NumPy can be one-dimensional or multidimensional.
- It contains tools for code integration.
- We can create arrays of different data types.
- All the elements of the NumPy Array have equal sizes in the memory block.
Let’s understand how to create a NumPy Array. We can follow the following code snippet to create our first NumPy array using the Python IDLE shell. Let’s code with you.
#importing the numpy module import numpy as np #creating array my_array = np.array(["Hi", "Hello", "Welcome", "to", "JournalDev"]) #printing our array print(my_array) print(type(my_array))
The above code snippet will give output as follows, where we can see that an array was created and one more thing we had printed is the type of the Array, it is
["Hi", "Hello", "Welcome", "to", "JournalDev"] <class 'numpy.ndarray'>
Before following our code snippet, make sure You have installed the NumPy module on your computer, if not You can download this module by using the
pip package installer in the command prompt as follows.
pip install numpy
Now, We will understand different ways to copy our NumPy Array using different methods as follows.
1. Using NumPy.copy() Method
numpy.copy() method creates a copy of our Array. In the below example, We are going to copy the Array
‘array‘ to another Array
‘copy_array‘ using this method.
#importing the numpy module import numpy as np # Creating a numpy Arrayusing numpy.array() array = np.array([10.7, 16.94, 18.21, 25.50, 25.3, 56.9, 52.1]) #printing our original Array print(array) # Using numpy.copy() function new_array= np.copy(array) #printing the copied Arrayas new_array print(new_array)
The above code snippet will copy the contents of the Array to the new_array using the
.copy() method and will give the output as follows.
The syntax for the above method is :
The parameters on the above code snippet are :
array– the name of the array to be copied
order– Controls the memory layout of the copy. by default order=’K’. It’s optional.
2. Copying Multi-Dimensional Numpy Arrays
Let’s see another example of the
.copy() method to copy multi-dimensional NumPy Arrays. Follow the below code snippet.
import numpy as np # Creating a multi dimensional Array array = np.array([['a','b','c'], ['h','d','s']]) #printing the original Array print(array) # using numpy.copy() method to copy new_array = np.copy(array) #printing the copied Array print(new_array)
The above code snippet will copy the multidimensional Array to the new Array and will give the output as follows.
3. Copying Numpy Arrays Using the Assignment Operator
import numpy as np # creating a 2 Dimensional array array = np.array([['a','b','c'], ['h','d','s']]) # using Assignment operator new_array= array # printing copied array print(new_array)
in the above code snippet, The contents of the Array were assigned to new_array by the assignment operator “
=“. Let’s see the output for the same.
4. Using NumPy.empty_like() Method
import numpy as np # creating an Array array = np.array(["hello", "how", "are", "You"]) print(array) # Creating an empty Numpy array similar to Array new_array = np.empty_like(array) # Now assign Array to copy new_array [:] = array #printing our new Array print(new_array )
This method returns arbitrary data with the same data. it doesn’t initialize the value. Since it doesn’t initialize, we need to assign the values to it. In this way, we copied our array to new_array. The output of the above code snippet is given as follows.
In this article, I have learned how to create a copy of the NumPy Array using for different methods with examples. Hope You must have practiced and enjoyed our code snippets. We must visit again with some more exciting topics.