repeat函數(shù)的作用:①擴(kuò)充數(shù)組元素 ②降低數(shù)組維度
numpy.repeat(a, repeats, axis=None):若axis=None,對于多維數(shù)組而言,可以將多維數(shù)組變化為一維數(shù)組,然后再根據(jù)repeats參數(shù)擴(kuò)充數(shù)組元素;若axis=M,表示數(shù)組在軸M上擴(kuò)充數(shù)組元素。
下面以3維數(shù)組為例,了解下repeat函數(shù)的使用方法:
In [1]: import numpy as np
In [2]: arr = np.arange(12).reshape(1,4,3)
In [3]: arr
Out[3]:
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]]])
①repeats為整數(shù)N,axis=None:數(shù)組arr首先被扁平化,然后將數(shù)組arr中的各個(gè)元素 依次重復(fù)N次
In [4]: arr.repeat(2)
Out[4]:
array([ 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8,
8, 9, 9, 10, 10, 11, 11])
②repeats為整數(shù)數(shù)組rp_arr,axis=None:數(shù)組arr首先被扁平化,然后再將數(shù)組arr中元素依次重復(fù)對應(yīng)rp_arr數(shù)組中元素對應(yīng)次數(shù)。若rp_arr為一個(gè)值的一維數(shù)組,則數(shù)組arr中各個(gè)元素重復(fù)相同次數(shù),否則rp_arr數(shù)組長度必須和數(shù)組arr的長度相等,否則報(bào)錯(cuò)
a:rp_arr為單值一維數(shù)組,進(jìn)行廣播
In [5]: arr.repeat([2])
Out[5]:
array([ 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8,
8, 9, 9, 10, 10, 11, 11])
b:rp_arr長度小于數(shù)組arr長度,無法進(jìn)行廣播,報(bào)錯(cuò)
In [6]: arr.repeat([2,3,4])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
ipython-input-6-d3b52907284c> in module>()
----> 1 arr.repeat([2,3,4])
ValueError: operands could not be broadcast together with shape (12,) (3,)
c:rp_arr長度和數(shù)組arr長度相等
In [7]: arr.repeat(np.arange(12))
Out[7]:
array([ 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6,
6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8,
8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11])
d:rp_arr長度大于數(shù)組arr長度,也無法廣播,報(bào)錯(cuò)
In [8]: arr.repeat(np.arange(13))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
ipython-input-8-ec8454224d1b> in module>()
----> 1 arr.repeat(np.arange(13))
ValueError: operands could not be broadcast together with shape (12,) (13,)
結(jié)論:兩個(gè)數(shù)組滿足廣播的條件是兩個(gè)數(shù)組的后緣維度(即從末尾開始算起的維度)的軸長度相等或其中一方的長度為1
③repeats為整數(shù)N,axis=M:數(shù)組arr的軸M上的每個(gè)元素重復(fù)N次,M=-1代表最后一條軸
In [9]: arr.repeat(2,axis=0)
Out[9]:
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]],
[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]]])
In [12]: arr.repeat(2,axis=-1)#arr.repeat(2,axis=-1)等同于arr.repeat(2,axis=2)
Out[12]:
array([[[ 0, 0, 1, 1, 2, 2],
[ 3, 3, 4, 4, 5, 5],
[ 6, 6, 7, 7, 8, 8],
[ 9, 9, 10, 10, 11, 11]]])
④repeats為整數(shù)數(shù)組rp_arr,axis=M:把數(shù)組arr1軸M上的元素依次重復(fù)對應(yīng)rp_arr數(shù)組中元素對應(yīng)次數(shù)。若rp_arr為一個(gè)值的一維數(shù)組,則數(shù)組arr1軸M上的各個(gè)元素重復(fù)相同次數(shù),否則rp_arr數(shù)組長度必須和數(shù)組arr1軸M的長度相等,否則報(bào)錯(cuò)
a:rp_arr長度和數(shù)組arr1軸M上長度相等
在軸0上擴(kuò)充數(shù)組元素
In [13]: arr1 = np.arange(24).reshape(4,2,3)
In [14]: arr1
Out[14]:
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]],
[[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23]]])
In [15]: arr1.repeat((1,2,3,4),axis=0)
Out[15]:
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]],
[[ 6, 7, 8],
[ 9, 10, 11]],
[[12, 13, 14],
[15, 16, 17]],
[[12, 13, 14],
[15, 16, 17]],
[[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23]],
[[18, 19, 20],
[21, 22, 23]],
[[18, 19, 20],
[21, 22, 23]],
[[18, 19, 20],
[21, 22, 23]]])
在軸1上擴(kuò)充數(shù)組元素
In [19]: arr1.repeat([1,2],axis=1)
Out[19]:
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11],
[ 9, 10, 11]],
[[12, 13, 14],
[15, 16, 17],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[21, 22, 23]]])
b:rp_arr為單值數(shù)組時(shí),進(jìn)行廣播
In [20]: arr1.repeat([2],axis=0)
Out[20]:
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]],
[[ 6, 7, 8],
[ 9, 10, 11]],
[[12, 13, 14],
[15, 16, 17]],
[[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23]],
[[18, 19, 20],
[21, 22, 23]]])
c:rp_arr和數(shù)組arr1某軸不滿足廣播條件,則報(bào)錯(cuò)
In [21]: arr1.repeat((1,2,3),axis=0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
ipython-input-21-8ae4dc97e410> in module>()
----> 1 arr1.repeat((1,2,3),axis=0)
ValueError: operands could not be broadcast together with shape (4,) (3,)
tile函數(shù)兩個(gè)作用:①擴(kuò)充數(shù)組元素 ②提升數(shù)組維度
numpy.tile(A, reps):根據(jù)reps中元素?cái)U(kuò)充數(shù)組A中對應(yīng)軸上的元素
①reps為整數(shù)N:可以把整數(shù)N理解成含一個(gè)元素N的序列reps,若數(shù)組.ndim大于reps序列的長度,則需在reps序列的索引為0的位置開始添加元素1,直到reps的長度和數(shù)組的維度數(shù)相等,然后數(shù)組各軸上的元素依次重復(fù)reps序列中元素對應(yīng)的次數(shù)
對于一維數(shù)組而言:是整體數(shù)組重復(fù)N次,從數(shù)組的最后一位置開始重復(fù),注意與repeat函數(shù)的區(qū)別
In [26]: arr3 = np.arange(4)
In [27]: arr3
Out[27]: array([0, 1, 2, 3])
In [28]: np.tile(arr3,2)
Out[28]: array([0, 1, 2, 3, 0, 1, 2, 3])
對多維數(shù)組而言:arr2.ndim=3,,reps=[2,],可以看出數(shù)組的長度大于序列reps的長度,因此需要向reps中添加元素,變成reps=[1,1,2],然后arr2數(shù)組再根據(jù)reps中的元素重復(fù)其對應(yīng)軸上的元素,reps=[1,1,2]代表數(shù)組arr2在軸0上各個(gè)元素重復(fù)1次,在軸1上的各個(gè)元素重復(fù)1次,在軸1上的各個(gè)元素重復(fù)2次
In [29]: arr2 = np.arange(24).reshape(4,2,3)
In [30]: arr2
Out[30]:
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]],
[[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23]]])
In [31]: np.tile(arr2,2)
Out[31]:
array([[[ 0, 1, 2, 0, 1, 2],
[ 3, 4, 5, 3, 4, 5]],
[[ 6, 7, 8, 6, 7, 8],
[ 9, 10, 11, 9, 10, 11]],
[[12, 13, 14, 12, 13, 14],
[15, 16, 17, 15, 16, 17]],
[[18, 19, 20, 18, 19, 20],
[21, 22, 23, 21, 22, 23]]])
②reps為整數(shù)序列rp_arr:若數(shù)組.ndim大于rp_arr長度,方法同①相同,若數(shù)組ndim小于rp_arr長度,則需在數(shù)組的首緣維添加新軸,直到數(shù)組的維度數(shù)和rp_arr長度相等,然后數(shù)組各軸上的元素依次重復(fù)reps序列中元素對應(yīng)的次數(shù)
a:數(shù)組維度大于rp_arr長度:需rp_arr提升為(1,2,3)
In [33]: arr2 = np.arange(24).reshape(4,2,3)
In [34]: arr2
Out[34]:
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]],
[[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23]]])
In [35]: np.tile(arr2,(2,3))
Out[35]:
array([[[ 0, 1, 2, 0, 1, 2, 0, 1, 2],
[ 3, 4, 5, 3, 4, 5, 3, 4, 5],
[ 0, 1, 2, 0, 1, 2, 0, 1, 2],
[ 3, 4, 5, 3, 4, 5, 3, 4, 5]],
[[ 6, 7, 8, 6, 7, 8, 6, 7, 8],
[ 9, 10, 11, 9, 10, 11, 9, 10, 11],
[ 6, 7, 8, 6, 7, 8, 6, 7, 8],
[ 9, 10, 11, 9, 10, 11, 9, 10, 11]],
[[12, 13, 14, 12, 13, 14, 12, 13, 14],
[15, 16, 17, 15, 16, 17, 15, 16, 17],
[12, 13, 14, 12, 13, 14, 12, 13, 14],
[15, 16, 17, 15, 16, 17, 15, 16, 17]],
[[18, 19, 20, 18, 19, 20, 18, 19, 20],
[21, 22, 23, 21, 22, 23, 21, 22, 23],
[18, 19, 20, 18, 19, 20, 18, 19, 20],
[21, 22, 23, 21, 22, 23, 21, 22, 23]]])
b:數(shù)組的維度小于rp_arr的長度:需在數(shù)組的首緣維度新增加一條軸,使其shape變?yōu)?1,4,2,3)
In [36]: np.tile(arr2,(2,1,1,3))
Out[36]:
array([[[[ 0, 1, 2, 0, 1, 2, 0, 1, 2],
[ 3, 4, 5, 3, 4, 5, 3, 4, 5]],
[[ 6, 7, 8, 6, 7, 8, 6, 7, 8],
[ 9, 10, 11, 9, 10, 11, 9, 10, 11]],
[[12, 13, 14, 12, 13, 14, 12, 13, 14],
[15, 16, 17, 15, 16, 17, 15, 16, 17]],
[[18, 19, 20, 18, 19, 20, 18, 19, 20],
[21, 22, 23, 21, 22, 23, 21, 22, 23]]],
[[[ 0, 1, 2, 0, 1, 2, 0, 1, 2],
[ 3, 4, 5, 3, 4, 5, 3, 4, 5]],
[[ 6, 7, 8, 6, 7, 8, 6, 7, 8],
[ 9, 10, 11, 9, 10, 11, 9, 10, 11]],
[[12, 13, 14, 12, 13, 14, 12, 13, 14],
[15, 16, 17, 15, 16, 17, 15, 16, 17]],
[[18, 19, 20, 18, 19, 20, 18, 19, 20],
[21, 22, 23, 21, 22, 23, 21, 22, 23]]]])
numpy的repeat和tile 用來復(fù)制數(shù)組
repeat和tile都可以用來復(fù)制數(shù)組的,但是有一些區(qū)別
關(guān)鍵區(qū)別在于repeat是對于元素的復(fù)制,tile是以整個(gè)數(shù)組為單位的 ,repeat復(fù)制時(shí)元素依次復(fù)制,注意不要用錯(cuò),區(qū)別類似于[1,1,2,2]和[1,2,1,2]
repeat
用法
np.repeat(a, repeats, axis=None)
重復(fù)復(fù)制數(shù)組a的元素,元素的定義與axis有關(guān),axis不指定時(shí),數(shù)組會(huì)被展開進(jìn)行復(fù)制,每個(gè)元素就是一個(gè)值,指定axis時(shí),就是aixis指定維度上的一個(gè)元素
a = np.array([[1,2],
[3,4]])
不指定axis,默認(rèn)None,這時(shí)候數(shù)組會(huì)被展開成1維,再進(jìn)行復(fù)制
np.repeat(a, 2) # 所有元素依次復(fù)制相同的次數(shù)
參數(shù)是列表
np.repeat(a, [1, 2, 1, 2]) # 如果第二個(gè)參數(shù)是列表,列表長度必須和a的復(fù)制可選元素?cái)?shù)目相等,這里都是4
指定axis
指定時(shí),就是指定了復(fù)制元素沿的維度,這時(shí)候就不會(huì)把數(shù)組展平,會(huì)維持原來的維度數(shù)
np.repeat(a, 2, axi=0) # 所有沿著0維的元素依次復(fù)制相同的次數(shù)
np.repeat(a, [1, 2], axis=1) # 第二個(gè)參數(shù)是列表,列表長度必須和a的復(fù)制可選元素?cái)?shù)目相等,這里是2
結(jié)果如下,復(fù)制元素從第1維度算,可以看到第一列被復(fù)制了一次,第二列被復(fù)制了兩次
tile
用法
復(fù)制數(shù)組,repeats可以是整數(shù)或者元組、數(shù)組
repeats是整數(shù)
示例如下,它會(huì)將數(shù)組復(fù)制兩份,并且在最后一維將兩個(gè)元素疊加在一起,數(shù)組的維數(shù)不變,最后一維根據(jù)復(fù)制次數(shù)加倍
repeats是列表或元組
如果列表長度是1,和整數(shù)時(shí)相同。
列表長度不為1時(shí),列表從后向前看,最后一項(xiàng)是2,所以復(fù)制兩個(gè)數(shù)組,在最后一維進(jìn)行疊加,倒數(shù)第二項(xiàng)是3,將前步的結(jié)果進(jìn)行復(fù)制,并在倒數(shù)第二維,結(jié)果如下
當(dāng)列表的長度超過數(shù)組的維數(shù)時(shí),和前面類似,從后向前復(fù)制,復(fù)制結(jié)果會(huì)增加維度與列表的維數(shù)匹配,結(jié)果如下,在上面的基礎(chǔ)上,增加了一維
復(fù)制結(jié)果的shape
但是對于 簡單的單個(gè)數(shù)組重復(fù),個(gè)人更喜歡使用stack和concatenate將同一個(gè)數(shù)組堆疊起來
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- Python Numpy數(shù)組擴(kuò)展repeat和tile使用實(shí)例解析
- python3中numpy函數(shù)tile的用法詳解
- python numpy數(shù)組中的復(fù)制知識解析