[Reprint] Usage of Python numpy insert(), delete(), append() functions

Reference link: numpy.delete in Python

Introduction: 

The function of these three functions is to add or delete a row/column of a matrix or array, and then introduce the usage of the functions one by one. 

import numpy as np

 

>>> a = np.array(np.arange(12).reshape(3,4))

>>> a

array([[ 0,  1,  2,  3],

       [ 4,  5,  6,  7],

       [ 8,  9, 10, 11]])

 

One, numpy.insert() 

Function syntax: 

numpy.insert(arr, obj, values, axis=None)

 

Function: Insert a vector into a row or column. Parameter description: arr: array input matrix obj: int inserted before the row/column values: array the matrix to be inserted axis: int insert a row (0) or column (1) Return value: Return an array inserted into the vector. If axis=None, return a flattened array example: 

>>> np.insert(a,1,[0,0,0,0],0)

array([[ 0,  1,  2,  3],

       [ 0,  0,  0,  0],

       [ 4,  5,  6,  7],

       [ 8,  9, 10, 11]])

 

>>> np.insert(a,1,[0,1,0],1)

array([[ 0,  0,  1,  2,  3],

       [ 4,  1,  5,  6,  7],

       [ 8,  0,  9, 10, 11]])

 

二、numpy.delete() 

Function syntax: 

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

 

Function function: delete a row or column Parameter description: arr: array input matrix obj: int which row or column to delete axis: int delete row (0) or column (1) Return value: Return to delete a row/column array example: 

>>> np.delete(a,0,1)

array([[ 1,  2,  3],

       [ 5,  6,  7],

       [ 9, 10, 11]])

 

>>> np.delete(a,2,0)

array([[0, 1, 2, 3],

       [4, 5, 6, 7]])

 

三、numpy.append() 

Function syntax: 

numpy.append(arr, values, axis=None)

 

Function: Add a row/column to the end of the array. Parameter description: arr: array Input matrix values: array The matrix to be inserted axis: int According to row (0) or column (1) insertion example: 

>>> np.append(a, [[1,2,3,4]], 0) #Pay attention to the form of insertion here

array([[ 0,  1,  2,  3],

       [ 4,  5,  6,  7],

       [ 8,  9, 10, 11],

       [ 1,  2,  3,  4]])

 

>>> np.append(a,[[1],[2],[3]], 1)

array([[ 0,  1,  2,  3,  1],

       [ 4,  5,  6,  7,  2],

       [ 8,  9, 10, 11,  3]])

Guess you like

Origin blog.csdn.net/u013946150/article/details/113102360