这个 努比。插入() 函数在给定索引之前沿所述轴插入值。 语法:
null
numpy.insert(array, object, values, axis = None)
参数:
array : [array_like]Input array. object : [int, array of ints]Sub-array with the index or indices before which values is inserted values : [array_like]values to be added in the arr. Values should be shaped so that arr[...,obj,...] = values. If the type of values is different from that of arr, values is converted to the type of arr axis : Axis along which we want to insert the values. By default, it object is applied to flattened array
返回:
An copy of array with values being inserted as per the mentioned object along a given axis.
代码1:从1D数组中删除
# Python Program illustrating # numpy.insert() import numpy as geek #Working on 1D arr = geek.arange( 5 ) print ( "1D arr : " , arr) print ( "Shape : " , arr.shape) # value = 9 # index = 1 # Insertion before first index a = geek.insert(arr, 1 , 9 ) print ( "Array after insertion : " , a) print ( "Shape : " , a.shape) # Working on 2D array arr = geek.arange( 12 ).reshape( 3 , 4 ) print ( "2D arr : " , arr) print ( "Shape : " , arr.shape) a = geek.insert(arr, 1 , 9 , axis = 1 ) print ( "Array after insertion : " , a) print ( "Shape : " , a.shape) |
输出:
1D arr : [0 1 2 3 4] Shape : (5,) Array after insertion : [0 9 1 2 3 4] Shape : (6,) 2D arr : [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] Shape : (3, 4) Array after insertion : [[ 0 9 1 2 3] [ 4 9 5 6 7] [ 8 9 9 10 11]] Shape : (3, 5)
代码2:使用标量
# Python Program illustrating # numpy.insert() import numpy as geek # Working on 2D array arr = geek.arange( 12 ).reshape( 3 , 4 ) print ( "2D arr : " , arr) print ( "Shape : " , arr.shape) # Working with Scalars a = geek.insert(arr, [ 1 ], [[ 6 ],[ 9 ],], axis = 0 ) print ( "Array after insertion : " , a) print ( "Shape : " , a.shape) # Working with Scalars a = geek.insert(arr, [ 1 ], [[ 8 ],[ 7 ],[ 9 ]], axis = 1 ) print ( "Array after insertion : " , a) print ( "Shape : " , a.shape) |
输出:
2D arr : [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] Shape : (3, 4) Array after insertion : [[ 0 1 2 3] [ 6 6 6 6] [ 9 9 9 9] [ 4 5 6 7] [ 8 9 10 11]] Shape : (5, 4) Array after insertion : [[ 0 8 1 2 3] [ 4 7 5 6 7] [ 8 9 9 10 11]] Shape : (3, 5)
代码3:在不同点插入
# Python Program illustrating # numpy.insert() import numpy as geek #Working on 1D arr = geek.arange( 6 ).reshape( 2 , 3 ) print ( "1D arr : " , arr) print ( "Shape : " , arr.shape) # value = 9 # index = 1 # Insertion before first index a = geek.insert(arr, ( 2 , 4 ), 9 ) print ( "Insertion at two points : " , a) print ( "Shape : " , a.shape) # Working on 2D array arr = geek.arange( 12 ).reshape( 3 , 4 ) print ( "2D arr : " , arr) print ( "Shape : " , arr.shape) a = geek.insert(arr, ( 0 , 3 ), 66 , axis = 1 ) print ( "Insertion at two points : " , a) print ( "Shape : " , a.shape) |
输出:
1D arr : [[0 1 2] [3 4 5]] Shape : (2, 3) Insertion at two points : [0 1 9 2 3 9 4 5] Shape : (8,) 2D arr : [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] Shape : (3, 4) Insertion at two points : [[66 0 1 2 66 3] [66 4 5 6 66 7] [66 8 9 10 66 11]] Shape : (3, 6)
参考资料: https://docs.scipy.org/doc/numpy/reference/generated/numpy.insert.html#numpy.insert
注: 这些代码不会在online-ID上运行。请在您的系统上运行它们以探索工作环境。
本文由 莫希特·古普塔(Mohit Gupta_OMG) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END