# NumPy copy()

The `copy()` method returns an array copy of the given object.

### Example

``````import numpy as np

# create an array
array1 = np.arange(5)

# create a copy of the original array
array2 = np.copy(array1)

print(array2)

# Output: [0 1 2 3 4]``````

## copy() Syntax

The syntax of `copy()` is:

``numpy.copy(array, order = 'K', subok = False)``

## copy() Arguments

The `copy()` method takes three arguments:

• `array` - input data
• `subok`(optional) - determines whether to subclass the output array if the data type is changed or to return a base-class array
• `order` (optional) - specifies the order in which the elements are filled in copied class.

## copy() Return Value

The `copy()` method returns the array interpretation of given input.

## Example 1: Create Array With copy()

``````import numpy as np

# copy an array from another array
array0 = np.arange(5)
array1 = np.copy(array0)
print('Array copied from Array: ',array1)

# copy an array from a list
list1 = [1, 2, 3, 4, 5]
array2 = np.copy(list1)
print('Array copied from List: ',array2)

# copy an array from another array
tuple1 = (1, 2, 3, 4, 5)
array1 = np.copy(tuple1)
print('Array copied from Tuple: ',array1)``````

Output

```Array copied from Array:  [0 1 2 3 4]
Array copied from List:  [1 2 3 4 5]
Array copied from Tuple:  [1 2 3 4 5]```

## Example 2: Create Array With '=' and np.copy()

We can also create an array by copying it using the assignment operator.

``````import numpy as np

# create an array
array0 = np.arange(5) # [0 1 2 3 4]

# copy an array using assignment  operator
array1 = array0

# change 1st element of new array
array1[0] = -1

# print both arrays
print(array0)
print(array1)``````

Output

```[-1  1  2  3  4]
[-1  1  2  3  4]```

As shown in the example, modifying array1 also modifies array0 because they refer to the same underlying data.

If you want to create an independent copy of array0, you can use the `np.copy()` function to create a deep copy explicitly

Lets look at an example using `np.copy()`.

``````import numpy as np

# create an array
array0 = np.arange(5) # [0 1 2 3 4]

# copy an array using np.copy()
array1 = np.copy(array0)

# change 1st element of new array
array1[0] = -1

# print both arrays
print(array0)
print(array1)``````

Output

```[0 1 2 3 4]
[-1  1  2  3  4]```

## Using subok Argument in copy()

The `subok` parameter in `copy()` function determines whether the copy should be a subclass of the original array's class (`True`) or a basic `ndarray` (`False`).

Let's look at an example.

``````import numpy as np

# create a subclass of ndarray
class CustomArray(np.ndarray):
pass

# create an array using the custom subclass
arr = np.array([1, 2, 3]).view(CustomArray)

# create a copy with subclass preservation
copySubclass = np.copy(arr, subok=True)

# create a basic ndarray copy
copyNdarray = np.copy(arr, subok=False)

print("Copy with Subclass:",copySubclass)
print("Type of Subclass:",type(copySubclass))

print("Copy as ndArray:",copyNdarray)
print("Type of ndArray Copy:",type(copyNdarray))``````

Output

```Copy with Subclass: [1 2 3]
Type of Subclass: <class '__main__.CustomArray'>
Copy as ndArray: [1 2 3]
Type of ndArray Copy: <class 'numpy.ndarray'>```

## Using Optional Order Argument in copy()

The `order` argument specifies the order in which the copies are filled.

The order can be:

• `'C'` - elements are stored row-wise
• `'F'` - elements are stored column-wise
• `'A'` - tries to preserve the original array's order, otherwise defaults to C-order.
• `'K'` - copies the elements in the order they occur in memory and uses C-order by default.

Note: The `order` argument only affects the memory layout of the copied array and does not change the values or shape of the array.