- 一维数组的索引与切片
>>> import numpy as np
>>> a = np.arange(8)
>>> print a
[0 1 2 3 4 5 6 7]
>>> myslice = slice(3,7,2)
>>> print a[myslice]
[3 5]
>>> a = np.arange(9)
>>> print a[3:7]
[3 4 5 6]
>>> print[:7:2]
SyntaxError: invalid syntax
>>> print a[:7:2]
[0 2 4 6]
>>> print a[::-1]
[8 7 6 5 4 3 2 1 0]
>>> myslice = slice(3,7,2)
>>> print a[myslice]
[3 5]
>>> myslice1 = slice(None,None,-1)
>>> print a[myslice1]
[8 7 6 5 4 3 2 1 0]
- 多维数组的索引
>>> b = np.arange(24).reshape(2,3,4) #(块,行,列)
>>> print b
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
>>> print b.shape
(2, 3, 4)
>>> print b[0,0,0]
0
>>> print b[:,0,0]
[ 0 12]
>>> print b[0]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
>>> print b[0,:,:]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
>>> print b[0, ...]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
>>> print b[0,1]
[4 5 6 7]
>>> print b[0,1,::2]
[4 6]
>>> print b[... ,1]
[[ 1 5 9]
[13 17 21]]
>>> print b[:,1]
[[ 4 5 6 7]
[16 17 18 19]]
>>> print b[0,:,-1]
[ 3 7 11]
>>> print b[0,::-1,-1]
[11 7 3]
>>> print b[0,::2,-1]
[ 3 11]
>>> print b[::-1]
[[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]]
>>> s = slice(None,None,-1)
>>> print b[s,s,s]
[[[23 22 21 20]
[19 18 17 16]
[15 14 13 12]]
[[11 10 9 8]
[ 7 6 5 4]
[ 3 2 1 0]]]
>>>
3 布尔检索
>>> arr = np.arange(36).reshape(6,6)
>>> arr
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],
[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]])
>>> x = np.array([0,1,2,1,4,5])
>>> x == 1 #通过比较得到一个布尔数组
array([False, True, False, True, False, False], dtype=bool)
>>> arr[x == 1] #布尔索引
array([[ 6, 7, 8, 9, 10, 11],
[18, 19, 20, 21, 22, 23]])
从结果上看,布尔索引取出了布尔值为True的行。
布尔型数组的长度和索引的数组的行数(轴长度)必须一致。
布尔型数组可与切片,整数(整数序列)一起使用。
4 花式检索
花式索引(Fancy indexing),指的是利用整数数组进行索引。
>>> arr = np.empty((8,4))# 创建新数组,只分配内存空间,不填充值
>>> for i in range(8):#给每一行赋值
arr[i] = i
>>> arr
array([[ 0., 0., 0., 0.],
[ 1., 1., 1., 1.],
[ 2., 2., 2., 2.],
[ 3., 3., 3., 3.],
[ 4., 4., 4., 4.],
[ 5., 5., 5., 5.],
[ 6., 6., 6., 6.],
[ 7., 7., 7., 7.]])
>>> arr[[4,3,0,6]]
array([[ 4., 4., 4., 4.],
[ 3., 3., 3., 3.],
[ 0., 0., 0., 0.],
[ 6., 6., 6., 6.]])
>>> arr[[-3,-5,-7]]
array([[ 5., 5., 5., 5.],
[ 3., 3., 3., 3.],
[ 1., 1., 1., 1.]])
'''
我们可以看到花式索引的结果,以一个特定的顺序排列。
而这个顺序,就是我们所传入的整数列表或者ndarray。
这也为我们以特定的顺序来选取数组子集,提供了思路。
'''
>>> arr = np.arange(32).reshape((8,4))
>>> print arr
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]
[20 21 22 23]
[24 25 26 27]
[28 29 30 31]]
>>> arr[[1,5,7,2],[0,3,1,2]]
'''
这个返回的是,第一行第0个元素,第5行第3个元素,依次类推
'''
array([ 4, 23, 29, 10])
经过对比可以发现,返回的一维数组中的元素,分别对应(1,2)、(3,0)….
这一样一下子就清晰了,我们传入来两个索引数组,相当于传入了一组平面坐标,从而进行了定位。
此处,照我这样理解的话,那么一个N维数组,我传入N个索引数组的话,是不是相当于我传入了一个N维坐标。
>>> arr[[1,5,7,2]][:,[0,3,1,2]]
array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]])
>>> arr[np.ix_([1,5,7,2],[0,3,1,2])]
array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]])
>>> ar = np.arange(27).reshape(3,3,3)
>>> ar
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],
[24, 25, 26]]])
>>> ar[[1,2],[0,1],[2,2]]
array([11, 23])
那么应该如何得到一个矩形区域呢。可以这样做:
>>> arr[[1,5,7,2]][:,[0,3,1,2]]
array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]])
10 | 13 | 11 | 12 |
---|---|---|---|
5 0 | 53 | 51 | 52 |
70 | 73 | 71 | 72 |
20 | 23 | 21 | 22 |
上表中的70指的是第7行第0列那个元素
必须明白,arr7[2][3]
等价于arr7[2,3]
那么上面这种得到矩形区域的方法,就相当于行与列去了交集。
此外还可用np.ix_
函数,它的作用与上面的方法类似,只不过是将两个一维的数组转换为了一个可以选择矩形区域的索引器。
>>> arr[np.ix_([1,5,7,2],[0,3,1,2])]
array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]])
>>>
5 数组的转置
>>> arr = np.arange(15).reshape(3,5)
>>> arr
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
>>> arr.T
array([[ 0, 5, 10],
[ 1, 6, 11],
[ 2, 7, 12],
[ 3, 8, 13],
[ 4, 9, 14]])
6 改变数组的维度
>>> b = np.arange(24).reshape(2,3,4)
>>> print b
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
>>> print b.ravel()
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
>>> print b.flatten()
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
>>> b.shape
(2, 3, 4)
>>> b.reshape(6,4)
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]])
>>> print b.transpose()
[[[ 0 12]
[ 4 16]
[ 8 20]]
[[ 1 13]
[ 5 17]
[ 9 21]]
[[ 2 14]
[ 6 18]
[10 22]]
[[ 3 15]
[ 7 19]
[11 23]]]
>>> b.resize((2,12))
>>>> print b.resize((2,12))
None
>>> print b
[[ 0 1 2 3 4 5 6 7 8 9 10 11]
[12 13 14 15 16 17 18 19 20 21 22 23]]
7 结合数组
>>> a = np.arange(9).reshape(3,3)
>>> print a
[[0 1 2]
[3 4 5]
[6 7 8]]
>>> b = 2*a
>>> print b
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> print np.hstack((a,b))
[[ 0 1 2 0 2 4]
[ 3 4 5 6 8 10]
[ 6 7 8 12 14 16]]
>>> print np.concatenate((a,b),axis = 1)
[[ 0 1 2 0 2 4]
[ 3 4 5 6 8 10]
[ 6 7 8 12 14 16]]
>>> print np.vstack((a,b))
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> print np.concatenate((a,b),axis = 0)
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> print np.dstack((a,b))
[[[ 0 0]
[ 1 2]
[ 2 4]]
[[ 3 6]
[ 4 8]
[ 5 10]]
[[ 6 12]
[ 7 14]
[ 8 16]]]
>>> oned = np.arange(2)
>>> print oned
[0 1]
>>> twice_oned = 2*oned
>>> print twice_oned
[0 2]
>>> print np.column_stack((oned,twice_oned))
[[0 0]
[1 2]]
>>> print np.column_stack((a,b))
[[ 0 1 2 0 2 4]
[ 3 4 5 6 8 10]
[ 6 7 8 12 14 16]]
>>> print np.column_stack((a,b))
[[ 0 1 2 0 2 4]
[ 3 4 5 6 8 10]
[ 6 7 8 12 14 16]]
>>> print np.column_stack((a,b)) == np.hstack((a,b))
[[ True True True True True True]
[ True True True True True True]
[ True True True True True True]]
>>> print np.row_stack((oned,twice_oned))
[[0 1]
[0 2]]
>>> print np.row_stack((a,b))
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> print np.row_stack((a,b)) == np.vstack((a,b))
[[ True True True]
[ True True True]
[ True True True]
[ True True True]
[ True True True]
[ True True True]]
>>>
8 数组的分割
>>> a = np.arange(9).reshape(3,3)
>>> print a
[[0 1 2]
[3 4 5]
[6 7 8]]
>>> print np.hsplit(a,3)
[array([[0],
[3],
[6]]), array([[1],
[4],
[7]]), array([[2],
[5],
[8]])]
>>> print np.split(a,3,axis = 1)
[array([[0],
[3],
[6]]), array([[1],
[4],
[7]]), array([[2],
[5],
[8]])]
>>> print np.vsplit(a,3)
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
>>> print np.split(a,3,axis=1)
[array([[0],
[3],
[6]]), array([[1],
[4],
[7]]), array([[2],
[5],
[8]])]
>>> print np.split(a,3,axis = 0)
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
>>> c = np.arange(27).reshape(3,3,3)
>>> print c
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
>>> print np.dsplit(c,3)
[array([[[ 0],
[ 3],
[ 6]],
[[ 9],
[12],
[15]],
[[18],
[21],
[24]]]), array([[[ 1],
[ 4],
[ 7]],
[[10],
[13],
[16]],
[[19],
[22],
[25]]]), array([[[ 2],
[ 5],
[ 8]],
[[11],
[14],
[17]],
[[20],
[23],
[26]]])]
>>> print np.dsplit(c,3)
[array([[[ 0],
[ 3],
[ 6]],
[[ 9],
[12],
[15]],
[[18],
[21],
[24]]]), array([[[ 1],
[ 4],
[ 7]],
[[10],
[13],
[16]],
[[19],
[22],
[25]]]), array([[[ 2],
[ 5],
[ 8]],
[[11],
[14],
[17]],
[[20],
[23],
[26]]])]
>>> print np.split(a,3,axis=1)
[array([[0],
[3],
[6]]), array([[1],
[4],
[7]]), array([[2],
[5],
[8]])]
>>> print np.split(c,3,axis = 1)
[array([[[ 0, 1, 2]],
[[ 9, 10, 11]],
[[18, 19, 20]]]), array([[[ 3, 4, 5]],
[[12, 13, 14]],
[[21, 22, 23]]]), array([[[ 6, 7, 8]],
[[15, 16, 17]],
[[24, 25, 26]]])]
>>> print np.split(c,3,axis = 0)
[array([[[0, 1, 2],
[3, 4, 5],
[6, 7, 8]]]), array([[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]]]), array([[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])]
>>>
9数组的属性
>>> b = np.arange(24).reshape(2,12)
>>> b.ndim
2
>>> b.size
24
>>> b.itemsize
4
>>> b.nbytes
96
>>> b = np.array([1.0+1.0j,3.0+2.0j])
>>> b.real
array([ 1., 3.])
>>> b.imag
array([ 1., 2.])
>>> b = np.arange(4).reshape(2,3)
Traceback (most recent call last):
File "<pyshell#142>", line 1, in <module>
b = np.arange(4).reshape(2,3)
ValueError: cannot reshape array of size 4 into shape (2,3)
>>> b = np.arange(4).reshape(2,2)
>>> b.flat
<numpy.flatiter object at 0x03D08F38>
>>> b.flat[2]
2
>>>
10数组的转换
>>> b = np.array([1.0+1.0j,3.0+2.0j])
>>> print b
[ 1.+1.j 3.+2.j]
>>> print b.tolist()
[(1+1j), (3+2j)]
>>> print b.tostring()
>>> print np.fromstring('\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x00@', dtype=complex)
[ 1.+1.j 3.+2.j]
>>> print np.fromstring('20:42:52',sep=':', dtype=int)
[20 42 52]
>>> print b
[ 1.+1.j 3.+2.j]
>>> print b.astype(int)
Warning (from warnings module):
File "__main__", line 2
ComplexWarning: Casting complex values to real discards the imaginary part
[1 3]
>>> print b.astype('complex')
[ 1.+1.j 3.+2.j]
>>>