×

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.fromiter() in Python

numpy.fromiter() in Python The fromiter() function of Python numpy class creates a ndarray by using an iterable object. It returns a one-dimensional ndarray object. Syntax numpy.fromiter(iterable, dtype, count=-1) Parameter The numpy.frombuffer() method consists of three parameters, which...

1 minute read.

numpy.atleast_1d() in Python

numpy.atleast_1d() in Python The numpy.atleast_1d() function converts the inputs to arrays with at least one dimension. Syntax numpy.atleast_1d(*arys) Parameter arys1, arys2, … : This parameter represents one or more input arrays. Return This function returns an array, or list...

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.expand_dims() in Python

numpy.expand_dims() in Python The numpy.expand_dims() function expands the shape of an array. It Inserts a new axis that appears at the axis position in the expanded array shape. Syntax numpy.expand_dims(a, axis) Parameter The numpy.expand_dims() function has two parameters...

1 minute read.

numpy.dstack() in Python

numpy.dstack() in Python The numpy.dstack() function stacks the arrays in sequence depth wise (along third axis). Syntax numpy.dstack(tup) Parameter tup: This parameter represents the sequence of arrays where the arrays must have the same shape along...

1 minute 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.vstack() in Python

numpy.vstack() in Python The numpy.vstack() function stacks the arrays in a sequence vertically (row wise). Syntax numpy.vstack(tup) Parameter tup: This parameter represents the sequence of ‘ndarrays’ where the arrays must have the same shape along all...

1 minute read.

numpy.empty() in Python

numpy.empty() in Python The empty() method of Python numpy class returns a new array of the specified shape and type, without initializing the entries. Syntax numpy.empty(shape, dtype=float, order='C') Parameters The numpy.empty method consists of three parameters, which...

1 minute read.

Read numpy array

Numpy is a numerical python that deals with multi-dimensional arrays mostly used in storing multiple values. Python's core scientific computing package is called NumPy. This Python library offers a multidimensional...

3 minutes read.

numpy.empty_like() in Python

numpy.empty_like() in Python The empty_like() method of Python numpy class returns a new array with the same shape and type as the specified array. Syntax numpy.empty_like(prototype, dtype=None, order='K', subok=True) Parameters The numpy. empty_like() method consists of four parameters,...

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.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.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.repeat() in Python

numpy.repeat() in Python The numpy.repeat() function repeats the elements of an array. Syntax numpy.repeat(a, repeats, axis=None) Parameter The numpy.repeat() function consists of three parameters, which are as follows: a: This parameter represents the input array. repeats: This parameter represents the...

1 minute read.

numpy.column_stack() in Python

numpy.column_stack() in Python The numpy.column_stack() function stacks  the 1-D arrays as columns into a 2-D array. It takes a sequence of 1-D arrays and stacks them as columns to make a...

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.squeeze() in Python

numpy.squeeze() in Python The numpy.squeeze() function removes single-dimensional entries from the shape of an array. Syntax numpy.squeeze(a, axis=None) Parameter The numpy.squeeze() function has three parameters which are as follows: a: It represents the Input data. axis: This parameter signifies an int or...

1 minute read.

numpy.asarray_chkfinite() in Python

numpy.asarray_chkfinite() in Python The numpy.asarray_chkfinite() function converts the input to an array, checking for NaNs or Infs. Syntax numpy.asarray_chkfinite(a, dtype=None, order=None) Parameter a : This parameter represents an input data, which can be in the form of lists, tuples,...

2 minutes read.

numpy.flip() in Python

numpy.flip() in Python The numpy.flip() function is used to reverse the order of elements in an array along the given axis where the shape of the array is preserved, but the...

1 minute read.

NumPy vs SciPy

NumPy- NumPy is the most important Python package for scientific computing. It's a Python library that includes a multidimensional array object, derived objects (like masked arrays and matrices), and a...

3 minutes read.