# Numpy入门之 多维数组

>>> a = np.arange(10).reshape(-1,1)#第1轴变为1列，第0轴自动调整
>>> a
array([[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]])
>>> a = a + np.arange(10) #broadcast 广播，后面回介绍
>>> a
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10],
[ 2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
[ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12],
[ 4,  5,  6,  7,  8,  9, 10, 11, 12, 13],
[ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
[ 6,  7,  8,  9, 10, 11, 12, 13, 14, 15],
[ 7,  8,  9, 10, 11, 12, 13, 14, 15, 16],
[ 8,  9, 10, 11, 12, 13, 14, 15, 16, 17],
[ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]])
>>> a[(2,3)] #使用元组作为数组下标
5

>>> a[3: , [0, 2, 4]] # 第0轴取第3及之后所有行，第1轴取第0，2，4列
array([[ 3,  5,  7],
[ 4,  6,  8],
[ 5,  7,  9],
[ 6,  8, 10],
[ 7,  9, 11],
[ 8, 10, 12],
[ 9, 11, 13]])

>>> a[np.array([0,1,3])] #相当于a[[0,1,3]]
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10],
[ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12]])
>>> a[[0,1,3]]
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10],
[ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12]])

>>> a[np.array([0,1,3]), np.array([0,1,2])] #a[(0,0)]，a[(1,1)]，a[(3,2)]组成的数组
array([0, 2, 5])

138 篇文章26 人订阅

0 条评论