np.repeat和np.tile的使用

np.repeat:

# -*- coding: UTF-8 -*-
import numpy as np

a = [1, 2, 3]
b = np.repeat(a, 2)# 可以使用在普通列表a上,返回的是ndarray

print(a)

print(b)
b = b.repeat(3)# ndarray才有repeat属性
print(b)
# [1, 2, 3]
# [1 1 2 2 3 3]
# [1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3]

np.tile:

# -*- coding: UTF-8 -*-
import numpy as np

a = range(10)
a = np.tile(a, 2)
print(a)
# [0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]

a = range(3)
a = np.tile(a, (3, 4))
print(a)
# [[0 1 2 0 1 2 0 1 2 0 1 2]
#  [0 1 2 0 1 2 0 1 2 0 1 2]
#  [0 1 2 0 1 2 0 1 2 0 1 2]]

猜你喜欢

转载自blog.csdn.net/xky1306102chenhong/article/details/81603244
np