Numpy: element addition, deletion and modification of array (Ndarray) operations


Array element addition, deletion and modification

Array is also a mutable type, and elements in the array can be added, deleted, and modified. This article introduces the operations of adding and deleting array elements in detail, and the methods of these two operations are listed. The modification operation of array elements is simple. As long as you master the index and slice, you can use the index and slice to get the element and then assign it.

add element

method illustrate
numpy.append() Append element to array
numpy.insert() array insert element

numpy.append()

Append elements at the end of the array.

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

Parameter Description:

  • arr: Receive array_like, the array to which elements need to be added.
  • values: Receives array_like, elements to append to the end, shapes must match. The dimensions of arr and values ​​must be equal to append
  • axis: receives an int, if no axis is given, both arr and values ​​will be flattened before use.

return value:

  • ndarray, a copy of arr.

Example:

# 创建数组a
>>> a = np.arange(1,7).reshape(2,3)
>>> a
array([[1, 2, 3],
       [4, 5, 6]])
# 创建数组b       
>>> b = np.arange(7,10).reshape(1,3) # a,b维度相同才能追加
>>> b  
array([[7, 8, 9]]) 

Note: The dimensions of the array (arr) and the additional value (values) must be the same before they can be chased, otherwise an error will be reported:

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

When no axis is specified, a copy is generated, and the arrays a and b are flattened and appended.

# 将数组b追加到数组a后
>>> np.append(a, values=b) # 不指定axis时
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9]) 

When the axis is specified, it will be added according to the axis, but the shapes must match. When the axis is specified for row addition, the number of columns must be equal, and when the axis is specified for column addition, the number of rows must be equal.

>>> np.append(a, values=b, axis=0) # 根据行追加
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9]])

When the axis is specified, when the axis is specified as a column, the number of rows is different, the shape does not match, and cannot be appended, and a ValueError will be reported!

>>> np.append(a, values=b, axis=1)
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 2 and the array at index 1 has size 1

numpy.insert()

Inserts a value at the given axis and at the specified index position.

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

Parameter Description:

  • arr: Receive array_like, the input array.
  • obj: Receives an integer or sequence of integers, the index position.
  • values: Receive array_like, need to insert the value of the array, need to consider the shape.
  • axis: accepts an integer, the axis. Axial arrays are flattened if not given.

return value:

  • ndarray, a copy of the inserted value.

Example:

>>> a = np.arange(1,7).reshape(2,3)
>>> a
array([[1, 2, 3],
       [4, 5, 6]])
>>> b = np.ones(shape=(2,1))
>>> b
array([[1.],
       [1.]])
       
# 向数组a的行方向,索引为2的行插入数组b,会自动补全
>>> np.insert(a, 2, b, axis=0)
array([[1, 2, 3],
       [4, 5, 6],
       [1, 1, 1],
       [1, 1, 1]])
       
# 向数组a的列方向,索引为2的列插入数组b
>>> np.insert(a, 2, b, axis=1)
array([[1, 2, 1, 1, 3],
       [4, 5, 1, 1, 6]])

remove element

method illustrate
numpy.delete() Delete the subarray of an axis and return the new array after deletion

numpy.delete()

Returns a new array with subarrays removed along the axis.

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

Parameter Description:

  • arr: Receive array_like, input array.
  • obj: Receives an index, slice, or array of integers.
  • axis: receive integer, axis

return value:

  • ndarray, the array with elements removed, is a copy.

Example:

>>> a = np.arange(1,7).reshape(2,3)
>>> a
array([[1, 2, 3],
       [4, 5, 6]])
# 轴向为列,删除索引为2的列      
>>> np.delete(a, 2, axis=1)      
array([[1, 2],
       [4, 5]])

The shape is very important when operating the data. If the shape does not match, an error will be reported. You need to understand the type of the error to find the cause in time after the problem occurs. In addition, the axis is also very important. In a two-dimensional array: axis=0 means row, axis=1 means column, this concept is very confusing.

element modification

Use the index slice to get the element at the position and then use "=" to reassign the position.

Syntax: arrayname[index]=value or arrayname[slice]=value

Example:

>>> a = np.arange(1,7).reshape(2,3)
>>> a
array([[1, 2, 3],
       [4, 5, 6]])
       
# 使用索引获取到该位置后重新赋值即可修改元素       
>>> a[0, 1] = 100
>>> a
array([[ 1, 100, 3],
       [ 4,  5,  6]])     

Guess you like

Origin blog.csdn.net/shield911/article/details/124269761