Python numpy.repeat()

Python numpy.repeat()

Python numpy.repeat()函数重复数组中的元素 – arr.

语法 :

numpy.repeat(arr, repetitions, axis = None)

参数 :

array : [array_like]输入数组。
repetitions :每个数组元素沿着给定的轴重复的数量。
axis : 我们想沿着这个轴重复数值。默认情况下,它返回一个平面输出数组。

返回 :

一个有重复数组的数组–Arr元素按重复次数,我们想重复Arr的次数。

代码 1 :

# Python Program illustrating
# numpy.repeat()
 
import numpy as geek
 
#Working on 1D
arr = geek.arange(5)
print("arr : \n", arr)
 
repetitions = 2
a = geek.repeat(arr, repetitions)
print("\nRepeating arr 2 times : \n", a)
print("Shape : ", a.shape)
 
repetitions = 3
a = geek.repeat(arr, repetitions)
print("\nRepeating arr 3 times : \n", 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 : \n", arr)
 
repetitions = 2
print("\nRepeating arr : \n", geek.repeat(arr, repetitions, 1))
print("arr Shape : \n", geek.repeat(arr, repetitions).shape)
 
 
repetitions = 2
print("\nRepeating arr : \n", geek.repeat(arr, repetitions, 0))
print("arr Shape : \n", geek.repeat(arr, repetitions).shape)
    
repetitions = 3
print("\nRepeating arr : \n", geek.repeat(arr, repetitions, 1))
print("arr Shape : \n", 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,)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程