这三个函数的功能是增删矩阵或数组的某一行/列,接下来逐个介绍函数用法。
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]])
numpy.insert(arr, obj, values, axis=None)
>>> 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(arr, obj, axis=None)
>>> 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(arr, values, axis=None)
>>> np.append(a, [[1,2,3,4]], 0) #这里注意插入的形式
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]])