一维、二维、三维的数组如何索引?
import numpy as np
# 一维数组索引及切片
ar = np.arange(20)
print('ar = ', ar)
print('ar[4] = ', ar[4])
print('ar[3:6] = ', ar[3:6])
打印结果:
ar = [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
ar[4] = 4
ar[3:6] = [3 4 5]
# 二维数组索引及切片
ar = np.array([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
[12, 13, 14, 15]])
print('ar = {0}; ar数组轴数 = {1}'.format(ar, ar.ndim)) # 4*4的数组
print('\nar[2] = {0}; ar[2]数组轴数 = {1}'.format(ar[2], ar[2].ndim)) # 切片为下一维度的一个元素,所以是一维数组
print('\nar[2][1] = {0}; ar[2][1]数组轴数 = {1}'.format(ar[2][1], ar[2][1].ndim)) # 二次索引,得到一维数组中的一个值
print('\nar[1:3] = {0}; ar[1:3]数组轴数 = {1}'.format(ar[1:3], ar[1:3].ndim)) # 切片为两个一维数组组成的二维数组
print('\nar[2, 2] = {0}; ar[2, 2]数组轴数 = {1}'.format(ar[2, 2], ar[2, 2].ndim)) # 切片数组中的第三行第三列 → 10
print('\nar[:2, 1:] = {0}; ar[:2, 1:]数组轴数 = {1}'.format(ar[:2, 1:], ar[:2, 1:].ndim)) # 切片数组中的1,2行、2,3,4列 → 二维数组
打印结果:
ar = [
[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]; ar数组轴数 = 2
ar[2] = [ 8 9 10 11]; ar[2]数组轴数 = 1
ar[2][1] = 9; ar[2][1]数组轴数 = 0
ar[1:3] = [[ 4 5 6 7]
[ 8 9 10 11]]; ar[1:3]数组轴数 = 2
ar[2, 2] = 10; ar[2, 2]数组轴数 = 0
ar[:2, 1:] = [[1 2 3]
[5 6 7]]; ar[:2, 1:]数组轴数 = 2
二维数组索引方式:
# 二维的数组,两个维度
stock_change[0, 0:3]
返回结果:
array([-0.03862668, -1.46128096, -0.75596237])
# **三维数组索引及切片
ar = np.array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
print('ar = {0}; ar数组轴数 = {1}'.format(ar, ar.ndim)) # 2*2*2的数组
print('ar[0] = {0}; ar[0]数组轴数 = {1}'.format(ar[0], ar[0].ndim)) # 三维数组的下一个维度的第一个元素 → 一个二维数组
print('ar[0][0] = {0}; ar[0][0]数组轴数 = {1}'.format(ar[0][0], ar[0][0].ndim)) # 三维数组的下一个维度的第一个元素下的第一个元素 → 一个一维数组
print('ar[0][0][1] = {0}; ar[0][0][1]数组轴数 = {1}'.format(ar[0][0][1], ar[0][0][1].ndim))
打印结果:
ar = [[[0 1]
[2 3]]
[[4 5]
[6 7]]]; ar数组轴数 = 3
ar[0] = [[0 1]
[2 3]]; ar[0]数组轴数 = 2
ar[0][0] = [0 1]; ar[0][0]数组轴数 = 1
ar[0][0][1] = 1; ar[0][0][1]数组轴数 = 0
# 三维
a1 = np.array([ [[1,2,3],[4,5,6]], [[12,3,34],[5,6,7]]])
# 返回结果
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[12, 3, 34],
[ 5, 6, 7]]])
# 索引、切片
>>> a1[0, 0, 1] # 输出: 2
import numpy as np
ar = np.array([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]])
# 布尔型索引:以布尔型的矩阵去做筛选
i = np.array([True, False, True])
j = np.array([True, True, False, False])
print('ar = ', ar)
print('\ni = ', i)
print('\nj = ', j)
print('\nar[i, :] = ', ar[i, :]) # 在第一维度做判断,只保留True,这里第一维度就是行,ar[i,:] = ar[i](简单书写格式)
print('\nar[:, j] = ', ar[:, j]) # 在第二维度做判断,这里如果ar[:,i]会有警告,因为i是3个元素,而ar在列上有4个
print('-' * 100)
m = ar > 5
print('m = ', m) # 这里m是一个判断矩阵
print('\nar[m] = ', ar[m]) # 用m判断矩阵去筛选ar数组中>5的元素 → 重点!后面的pandas判断方式原理就来自此处
打印结果:
ar = [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
i = [ True False True]
j = [ True True False False]
ar[i, :] = [[ 0 1 2 3]
[ 8 9 10 11]]
ar[:, j] = [[0 1]
[4 5]
[8 9]]
----------------------------------------------------------------------------------------------------
m = [[False False False False]
[False False True True]
[ True True True True]]
ar[m] = [ 6 7 8 9 10 11]
Process finished with exit code 0
一个标量赋值给一个索引/切片时,会自动改变/传播原始数组
import numpy as np
# 一个标量赋值给一个索引/切片时,会自动改变/传播原始数组
ar = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print('ar = ', ar)
# 数组索引及切片的值更改、复制
ar[5] = 100
ar[7:9] = 200
print('ar = ', ar)
print('-' * 100)
# 复制
ar = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b = ar.copy()
b[7:9] = 200
print('ar = ', ar)
print('b = ', b)
打印结果:
ar = [0 1 2 3 4 5 6 7 8 9]
ar = [ 0 1 2 3 4 100 6 200 200 9]
----------------------------------------------------------------------------------------------------
ar = [0 1 2 3 4 5 6 7 8 9]
b = [ 0 1 2 3 4 5 6 200 200 9]
Process finished with exit code 0