关于Numpy Array的使用技巧整理
关于Numpy Array的使用技巧整理
1. 数组的扩展:repeat & tile
repeat
方法:实现按元素复制扩展
- 输入:(需要扩展的array),repeats向量,轴向axis(用于多维array情形)
- 输出:扩展后的array,需要赋值才能保存,并不修改原array本身
关于repeats向量的使用
:
- 若长度为1,则每个元素复制repeats次
- 若长度为array.shape[axis],则array[i]复制repeats[i]次
- 若长度与array.shape[axis]不等则报错
>>>import numpy as np
>>>a = np.arange(5)
>>>a
array([0, 1, 2, 3, 4])
>>>a.repeat(2)
array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4])
>>>np.repeat(a,2)
array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4])
>>>a.repeat(range(5))
array([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
>>>a.repeat(range(4))
Traceback (most recent call last):
File "<ipython-input-7-9ebd3b4fb8fd>", line 1, in <module>
a.repeat(range(4))
ValueError: operands could not be broadcast together with shape (5,) (4,)
对于多维array的情况
- 如果不指定axis,则系统自动将array转换成一维数组,然后根据repeats进行复制
- 如果指定了axis,则在对应维度下,将下一维度当做一个元素根据repeats进行复制
- 应当保证repeats维度为1,并且
len(repeats)==array.shape[axis]
>>>b = np.arange(5) + np.arange(5).reshape(5,1)
>>>b
array([[0, 1, 2, 3, 4],
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8]])
>>>b.repeat(2)
array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 2, 2, 3,
3, 4, 4, 5, 5, 6, 6, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 4, 4, 5, 5, 6, 6,
7, 7, 8, 8])
>>>b.repeat(2,axis=1)
array([[0, 0, 1, 1, 2, 2, 3, 3, 4, 4],
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5],
[2, 2, 3, 3, 4, 4, 5, 5, 6, 6],
[3, 3, 4, 4, 5, 5, 6, 6, 7, 7],
[4, 4, 5, 5, 6, 6, 7, 7, 8, 8]])
>>>b.repeat(2,axis=0)
array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8],
[4, 5, 6, 7, 8]])
>>>b.repeat(range(5),axis=0)
array([[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7],
[3, 4, 5, 6, 7],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8],
[4, 5, 6, 7, 8],
[4, 5, 6, 7, 8],
[4, 5, 6, 7, 8]])
tile方法(注意:此方法与repeat不同,只能用np.tile()方式调用)
- 输入:数组array,复制方式数组reps
- 输出:对整个数组进行复制操作后的结果数组
- 依旧需要赋值,否则不会保存
关于reps的详细说明(array为多维情形):
- 当reps维度小于array的维度时,默认从低到高的顺序对数组进行复制
- 相当于在reps左面补上1,将维度补齐
- 维度补齐以后,对应维度下,以数组为一个单位,进行扩展,具体见下面例子
>>>c=b.reshape(1,5,5)
>>>c
array([[[0, 1, 2, 3, 4],
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8]]])
>>>c.shape
(1L, 5L, 5L)
>>>np.tile(c,2)
array([[[0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5],
[2, 3, 4, 5, 6, 2, 3, 4, 5, 6],
[3, 4, 5, 6, 7, 3, 4, 5, 6, 7],
[4, 5, 6, 7, 8, 4, 5, 6, 7, 8]]])
>>>np.tile(c,(1,1,2))
array([[[0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5],
[2, 3, 4, 5, 6, 2, 3, 4, 5, 6],
[3, 4, 5, 6, 7, 3, 4, 5, 6, 7],
[4, 5, 6, 7, 8, 4, 5, 6, 7, 8]]])
>>>np.tile(c,(1,2))
array([[[0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5],
[2, 3, 4, 5, 6, 2, 3, 4, 5, 6],
[3, 4, 5, 6, 7, 3, 4, 5, 6, 7],
[4, 5, 6, 7, 8, 4, 5, 6, 7, 8]]])
>>>np.tile(c,(1,2,1))
array([[[0, 1, 2, 3, 4],
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8],
[0, 1, 2, 3, 4],
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8]]])
>>>np.tile(c,(2,1,1))
array([[[0, 1, 2, 3, 4],
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8]],
[[0, 1, 2, 3, 4],
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8]]])
未完待续