np.tile()とnp.repeat()
12081 ワード
私のホームページへようこそnp.tile()とnp.repeat()はarrayを繰り返すことができるがnp.tile()はaxisを最小単位(axis-wise)として繰り返すがnp.repeat()はelementを最小単位(element-wise)で繰り返す
np.tile(A,reps)
入力:Aは配列であり、repsはlistであり、repsの要素はAの各axisを繰り返す回数を表します.1つの配列で、次元の数はmax(A.ndim,len(reps))に等しく、A.ndimとA.shapeを混同しないように注意してください. A.ndimです. A.ndim>len(reps)は、A.ndim=len(reps)となるようにlistの長さを大きくする必要がある.すなわち、repsの先頭に要素1を追加する.例えば、元のlistは[2,2]、長さを大きくすると[1,2,2] となる.
公式の例:
np.repeat(a, repeats, axis=None)
入力:aは配列であり、repeatsは各要素の繰り返し回数(repeatsは一般的にスカラーであり、やや複雑な点はlistである)であり、axisの方向に繰り返し戻ります.axisを指定しない場合は、繰り返した結果を平らに(次元が1)にして返します.axisを指定すると、公式の例は表示されません.
np.tile(A,reps)
入力:Aは配列であり、repsはlistであり、repsの要素はAの各axisを繰り返す回数を表します.1つの配列で、次元の数はmax(A.ndim,len(reps))に等しく、A.ndimとA.shapeを混同しないように注意してください.
公式の例:
# 1,
a = np.array([0, 1, 2])
# axis=0 2
np.tile(A=a, reps=2)
# array([0, 1, 2, 0, 1, 2])
# 2, 1:A.ndim < len(reps)
a = np.array([0, 1, 2])
# a.shape (1,3), axis=0 2 , axis=1 2
np.tile(A=a, reps=(2, 2))
#array([[0, 1, 2, 0, 1, 2], [0, 1, 2, 0, 1, 2]])
# 3, 1:A.ndim < len(reps)
a = np.array([0, 1, 2])
# a.shape (1,1,3), axis=0 2 , axis=1 1 , axis=2 2
np.tile(A=a, reps=(2, 1, 2))
#array([[[0, 1, 2, 0, 1, 2]], [[0, 1, 2, 0, 1, 2]]])
# 4, 2:A.ndim > len(reps)
b = np.array([[1, 2], [3, 4]])
# reps=[2] [1,2], axis=0 1 , axis=1 2
np.tile(A=b, reps=2)
#array([[1, 2, 1, 2], [3, 4, 3, 4]])
# 5,
b = np.array([[1, 2], [3, 4]])
# axis=0 , axis=1 1
np.tile(A=b, reps=(2, 1))
#array([[1, 2], [3, 4], [1, 2], [3, 4]])
# 6, 1:A.ndim < len(reps)
c = np.array([1,2,3,4])
# c.shape (4,1), axis=0 4 , axis=1 1
np.tile(A=c, reps=(4,1))
# array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]])
np.repeat(a, repeats, axis=None)
入力:aは配列であり、repeatsは各要素の繰り返し回数(repeatsは一般的にスカラーであり、やや複雑な点はlistである)であり、axisの方向に繰り返し戻ります.axisを指定しない場合は、繰り返した結果を平らに(次元が1)にして返します.axisを指定すると、公式の例は表示されません.
# 1,
# 3 4
np.repeat(a=3, repeats=4)
# array([3, 3, 3, 3])
# 2,
# 2 ,
x = np.array([[1,2],[3,4]])
np.repeat(a=x, repeats=2)
# array([1, 1, 2, 2, 3, 3, 4, 4])
# 3,
x = np.array([[1,2],[3,4]])
# axis=1 , axis=1 3
np.repeat(a=x, repeats=3, axis=1)
#array([[1, 1, 1, 2, 2, 2], [3, 3, 3, 4, 4, 4]])
# 3,
x = np.array([[1,2],[3,4]])
# axis=0 , axis=0 0 1 , 1 2
np.repeat(a=x, repeats=[1, 2], axis=0)
# array([[1, 2], [3, 4], [3, 4]])