NumPy - delete() function

The NumPy delete() function returns a new array with sub-arrays along an axis deleted. For a one dimensional array, this returns those entries not returned by arr[obj].

Syntax

```numpy.delete(arr, obj, axis=None)
```

Parameters

 `arr` `Required. `Specify the input array (array_like). `obj` `Required. `Specify indices of sub-arrays to remove along the specified axis. It can be slice, int or array of ints. `axis` `Optional. `Specify the axis along which to delete the subarray defined by obj. If axis is None, obj is applied to the flattened array.

Return Value

Returns a copy of arr with the elements specified by obj removed..

Example:

In the example below, delete() function is used to delete a given object from an array.

```import numpy as np

Arr = np.array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])

#deleteing 3rd element (axis=None)
Arr1 = np.delete(Arr, 2)

#deleteing 2nd row
Arr2 = np.delete(Arr, 1, 0)

#deleteing 2nd column
Arr3 = np.delete(Arr, 1, 1)

#displaying results
print("Arr is:")
print(Arr)
print("\nArr1 is (delete with axis=None):")
print(Arr1)
print("\nArr2 is (delete with axis=0):")
print(Arr2)
print("\nArr3 is (delete with axis=1):")
print(Arr3)
```

The output of the above code will be:

```Arr is:
[[10 20 30]
[40 50 60]
[70 80 90]]

Arr1 is (delete with axis=None):
[10 20 40 50 60 70 80 90]

Arr2 is (delete with axis=0):
[[10 20 30]
[70 80 90]]

Arr3 is (delete with axis=1):
[[10 30]
[40 60]
[70 90]]
```

Example:

In the example below, delete() function is used to delete a number of rows/columns from an array.

```import numpy as np

Arr = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])

#deleteing 2nd and 3rd rows
Arr1 = np.delete(Arr, [1, 2], axis=0)

#deleteing 2nd and 3rd columns
Arr2 = np.delete(Arr, [1, 2], axis=1)

#displaying results
print("Arr is:")
print(Arr)
print("\nArr1 is (delete with axis=0):")
print(Arr1)
print("\nArr2 is (delete with axis=1):")
print(Arr2)
```

The output of the above code will be:

```Arr is:
[[ 1  2  3  4]
[ 5  6  7  8]
[ 9 10 11 12]
[13 14 15 16]]

Arr1 is (delete with axis=0):
[[ 1  2  3  4]
[13 14 15 16]]

Arr2 is (delete with axis=1):
[[ 1  4]
[ 5  8]
[ 9 12]
[13 16]]
```

Example:

Similary a slice() function can be used to specify rows/columns to delete from an array.

```import numpy as np

Arr = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])

#deleteing alternate rows
Arr1 = np.delete(Arr, slice(None, None, 2), axis=0)

#deleteing alternate columns
Arr2 = np.delete(Arr, slice(None, None, 2), axis=1)

#displaying results
print("Arr is:")
print(Arr)
print("\nArr1 is (delete with axis=0):")
print(Arr1)
print("\nArr2 is (delete with axis=1):")
print(Arr2)
```

The output of the above code will be:

```Arr is:
[[ 1  2  3  4]
[ 5  6  7  8]
[ 9 10 11 12]
[13 14 15 16]]

Arr1 is (delete with axis=0):
[[ 5  6  7  8]
[13 14 15 16]]

Arr2 is (delete with axis=1):
[[ 2  4]
[ 6  8]
[10 12]
[14 16]]
```

❮ NumPy - Array Manipulation

5