这个 努比。重复() 函数重复数组的元素–arr。 语法:
null
numpy.repeat(arr, repetitions, axis = None)
参数:
array : [array_like]Input array. repetitions : No. of repetitions of each array elements along the given axis. axis : Axis along which we want to repeat values. By default, it returns a flat output array.
返回:
An array with repetitions of array - arr elements as per repetitions, number of times we want to repeat arr
代码1:
# Python Program illustrating # numpy.repeat() import numpy as geek #Working on 1D arr = geek.arange( 5 ) print ( "arr : " , arr) repetitions = 2 a = geek.repeat(arr, repetitions) print ( "Repeating arr 2 times : " , a) print ( "Shape : " , a.shape) repetitions = 3 a = geek.repeat(arr, repetitions) print ( "Repeating arr 3 times : " , a) # [0 0 0 ..., 4 4 4] means [0 0 0 1 1 1 2 2 2 3 3 3 4 4 4] # since it was long output, so it uses [ ... ] print ( "Shape : " , a.shape) |
输出:
arr : [0 1 2 3 4] Repeating arr 2 times : [0 0 1 1 2 2 3 3 4 4] Shape : (10,) Repeating arr 3 times : [0 0 0 ..., 4 4 4] Shape : (15,)
代码2:
# Python Program illustrating # numpy.repeat() import numpy as geek arr = geek.arange( 6 ).reshape( 2 , 3 ) print ( "arr : " , arr) repetitions = 2 print ( "Repeating arr : " , geek.repeat(arr, repetitions, 1 )) print ( "arr Shape : " , geek.repeat(arr, repetitions).shape) repetitions = 2 print ( "Repeating arr : " , geek.repeat(arr, repetitions, 0 )) print ( "arr Shape : " , geek.repeat(arr, repetitions).shape) repetitions = 3 print ( "Repeating arr : " , geek.repeat(arr, repetitions, 1 )) print ( "arr Shape : " , geek.repeat(arr, repetitions).shape) |
输出:
arr : [[0 1 2] [3 4 5]] Repeating arr : [[0 0 1 1 2 2] [3 3 4 4 5 5]] arr Shape : (12,) Repeating arr : [[0 1 2] [0 1 2] [3 4 5] [3 4 5]] arr Shape : (12,) Repeating arr : [[0 0 0 ..., 2 2 2] [3 3 3 ..., 5 5 5]] arr Shape : (18,)
参考资料: https://docs.scipy.org/doc/numpy/reference/generated/numpy.repeat.html
注: 这些代码不会在online-ID上运行。请在您的系统上运行它们以探索工作环境 . 本文由 莫希特·古普塔(Mohit Gupta_OMG) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END