×

numpy.delete() in Python

numpy.delete() in Python

The numpy.delete() function returns a new array with sub-arrays along an axis deleted. For 1-D array, this function returns those entries which are not returned by arr[obj].

Syntax

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

Parameter

arr: This parameter returns the input array.

obj : This parameter indicates which sub-arrays to remove.

axis: It represents the axis along which to delete the subarray defined by obj. If axis is None, obj is applied to the flattened array.

Return

This function returns a copy of ‘arr’ with the elements specified by obj removed.

Example 1

# Python Program explaining
# numpy.delete() function
import numpy as np
#Working on 1D
inp_arr = np.arange(5)
print("arr value: \n", inp_arr)
print("Shape: ", inp_arr.shape)
# deletion 1D array 
object = 2
a = np.delete(inp_arr, object)
print("\nDeleteing arr value 2 times: ", a)
print("Shape: ", a.shape)

Output

arr value:
[0 1 2 3 4]
Shape:  (5,)
Deleteing arr value 2 times:  [0 1 3 4]
Shape:  (4,)

Example 2

# Python Program explaining
# numpy.delete() function
import numpy as np
arr = np.arange(9).reshape(3, 3)
print("arr value: ", arr)
print("Shape : ", arr.shape,'\n')
# deletion from 2D array 
val = np.delete(arr, 1, 0)
print("Deleting arr value 2 times : \n", val)
print("Shape : ", val.shape)
# deletion from 2D array 
val = np.delete(arr, 1, 1)
print("\ndelteing arr value 2 times : \n", val)
print("Shape : ", val.shape)

Output

arr value:  [[0 1 2]
[3 4 5]
[6 7 8]]
Shape :  (3, 3)
Deleting arr value 2 times :
[[0 1 2]
[6 7 8]]
Shape :  (2, 3)
deleting arr value 2 times :
[[0 2]
[3 5]
[6 8]]
Shape :  (3, 2)

Related Topics

numpy.split() in Python

numpy.split() in Python The numpy.split() function splits an array into multiple sub-arrays. Syntax numpy.split(ary, indices_or_sections, axis=0) Parameter ary: This parameter represents the Array to be divided into sub-arrays. indices_or_sections : This parameter represents an int or an 1-D array. If indices_or_sections is an...

1 minute read.

numpy.geomspace() in Python

numpy.geomspace() in Python The geomspace() function of Python numpy class returns the numbers spaced equally on a log scale (a geometric progression). This method is similar to numpy.logspace() but with endpoints specified directly. Syntax numpy.geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0) Parameter start: This...

1 minute read.

numpy.zeros in Python

numpy.zeros in Python The zeros() method of Python numpy class returns a new array of given shape and type, filled with zeros. Syntax numpy.zeros(shape, dtype=float, order='C') Parameter The numpy.zeros() method consists of three parameters, which are as...

1 minute read.

numpy.reshape() in Python

numpy.reshape() in Python The numpy.reshape() function gives a new shape to an array without changing its data. Syntax numpy.reshape(a, newshape, order='C') Parameter The numpy.reshape() method consists of three parameters, which are as follows: array : It represents our...

1 minute read.

numpy.eye() in Python

numpy.eye() in Python: The eye() method of Python numpy class returns a 2-D array with ones on the diagonal and zeros elsewhere. Syntax numpy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C') Parameters The numpy.eye()  method consists of five parameters, which...

1 minute read.

numpy.tile() in Python

numpy.tile() in Python The numpy.tile() function constructs an array by repeating the parameter ‘A’  the number of times as specified by the ‘reps’ parameter. Syntax numpy.tile(A, reps) Parameter The numpy.tile() function consists of two parameters, which...

1 minute read.

numpy.resize() in Python

numpy.resize() in Python The numpy.resize() returns a new array with the specified shape. If the new array is larger than the original array, then the new array is filled with repeated...

1 minute read.

numpy.tril() in Python

numpy.tril() in Python The tril() function of Python numpy class returns a copy of an array with the elements above the k-th diagonal zeroed. Syntax numpy.tril(m, k=0) Parameter a: It represents the input array k: This parameter represents...

1 minute read.

numpy.delete() in Python

numpy.delete() in Python The numpy.delete() function returns a new array with sub-arrays along an axis deleted. For 1-D array, this function returns those entries which are not returned by arr[obj]. Syntax numpy.delete(arr, obj, axis=None) Parameter arr: This parameter returns...

2 minutes read.

numpy.loadtxt() Python

Python numpy.loadtxt() The loadtxt() function of Python numpy class loads the data from a text fileand provides a fast reader for simple text files. Syntax numpy.loadtxt(fname, dtype=<class'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes', max_rows=None) Parameter fname : This parameter represents a file, filename, or...

2 minutes read.

numpy.arrange() in Python

Python numpy.arrange() The arrange() function of Python numpy class returns an array with equally spaced elements as per the interval where the interval mentioned is half opened, i.e. [Start, Stop). Syntax numpy.arange([start, ]stop, [step, ]dtype=None) Parameter start :It...

1 minute read.

numpy.identity() in Python

numpy.identity() in Python The identity() method of Python numpy class returns an identity matrix i.e., a square matrix with ones on the main diagonal. Syntax numpy.identity(n, dtype=None) Parameters The numpy. identity()  method consists of two parameters,...

1 minute read.

numpy.ndarray.flatten() in Python

numpy.ndarray.flatten() in Python The numpy.ndarray.flatten() returns a copy of the array collapsed into 1-dimension. Syntax ndarray.flatten(order='C') Parameter The numpy. ndarray.flatten() method consists of one parameter, which is as follows: order : This parameter can be either...

1 minute read.

numpy.full() in Python

numpy.full() in Python The full() method of Python numpy class returns a new array of specified shape and type, filled with fill_value. Syntax numpy.full(shape, fill_value, dtype=None, order='C') Parameter shape:This parameter represents the number of rows order :The order parameter can...

1 minute read.

numpy.ones_like() in Python

numpy.ones_like() in Python The one_like() method of Python numpy class returns an array of ones with the same shape and type as the specified array. Syntax numpy.ones_like(a, dtype=None, order='K', subok=True) Parameter The numpy.ones_like() method consists of four parameters,...

1 minute read.

numpy.fromstring () in Python

numpy.fromstring () in Python The fromstring() function of Python numpy class creates a new 1-D array initialized from raw binary or text data in a string. Syntax numpy.fromstring(string, dtype=float, count=-1, sep='') Parameter The numpy.fromstring() method...

1 minute read.

numpy.broadcast_to() in Python

numpy.broadcast_to() in Python The numpy.broadcast_to() function broadcasts an array to a new shape. Syntax numpy.broadcast_to(array, shape, subok=False) Parameter The numpy.broadcast_to() function has three parameters which are as follows:  array: This parameter represents the array to broadcast. shape: It signifies the shape...

1 minute read.

numpy.hstack() in Python

numpy.hstack() in Python The numpy.hstack() function stacks the arrays in a sequence horizontally (column wise). Syntax numpy.hstack(tup) Parameter tup: This parameter represents the sequence of ‘ndarrays’ where the arrays must have the same shape, except 1-D...

1 minute read.

numpy.append() in Python

numpy.append() in Python The numpy.append() function appends the values to the end of an array. Syntax numpy.append(arr, values, axis=None) Parameter The numpy.append() function consists of three parameters, which are as follows: arr : This parameter represents the values that are...

1 minute read.

numpy.frombuffer() in Python

numpy.frombuffer() in Python The frombuffer()  function of Python numpy class creates an array by using the given buffer. Syntax numpy.frombuffer(buffer, dtype=float, count=-1, offset=0) Parameter The numpy.frombuffer() method consists of four parameters, which are as follows: buffer: This parameter...

1 minute read.