Boolean indexing
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
data = np.random.randn(7, 4)
print(names)
print(data)
# 对 names 和 字符串 'Bob' 的比较运算符会产生一个布尔型数组
print(names == 'Bob')
# 这个布尔型数组可以用于索引
print(data[names == 'Bob'])
# data 数组的第一个下标对应 行数,第二个下标 对应 列数
print(data[names == 'Bob', 2:])
print(data[names == 'Bob', 3])
# 不等于
print(names != 'Bob')
# 非
print(data[~(names == 'Bob')])
# 或
mask = (names == 'Bob') | (names == 'Will')
print(mask)
print(data[mask])
# 令 data 数组中,小于 0 的数据,变为 0
data[data < 0] = 0
print(data)
#
data[names != 'Joe'] = 7
print(data)
names = np.array([0, 0, 0, 0, 1, 0, 1])
# 打印一个由 data 数组的 0,0,0,0,1,0,1 行 组成的数组
print(data[names])
# -----------------------------------------------------------
['Bob' 'Joe' 'Will' 'Bob' 'Will' 'Joe' 'Joe']
[[-0.2305 1.2412 0.2335 -0.8078]
[-1.1944 0.5652 -1.1813 -0.223 ]
[-1.2092 0.0819 0.0676 -1.2792]
[ 1.3819 1.6011 1.5403 -0.3922]
[ 1.5291 -1.2984 -0.8702 -0.4224]
[-0.8074 1.5405 -0.2572 0.0881]
[-0.3954 -0.9237 -0.7061 -0.0459]]
[ True False False True False False False]
[[-0.2305 1.2412 0.2335 -0.8078]
[ 1.3819 1.6011 1.5403 -0.3922]]
[[ 0.2335 -0.8078]
[ 1.5403 -0.3922]]
[-0.8078 -0.3922]
[False True True False True True True]
[[-1.1944 0.5652 -1.1813 -0.223 ]
[-1.2092 0.0819 0.0676 -1.2792]
[ 1.5291 -1.2984 -0.8702 -0.4224]
[-0.8074 1.5405 -0.2572 0.0881]
[-0.3954 -0.9237 -0.7061 -0.0459]]
[ True False True True True False False]
[[-0.2305 1.2412 0.2335 -0.8078]
[-1.2092 0.0819 0.0676 -1.2792]
[ 1.3819 1.6011 1.5403 -0.3922]
[ 1.5291 -1.2984 -0.8702 -0.4224]]
[[ 0. 1.2412 0.2335 0. ]
[ 0. 0.5652 0. 0. ]
[ 0. 0.0819 0.0676 0. ]
[ 1.3819 1.6011 1.5403 0. ]
[ 1.5291 0. 0. 0. ]
[ 0. 1.5405 0. 0.0881]
[ 0. 0. 0. 0. ]]
[[ 7. 7. 7. 7. ]
[ 0. 0.5652 0. 0. ]
[ 7. 7. 7. 7. ]
[ 7. 7. 7. 7. ]
[ 7. 7. 7. 7. ]
[ 0. 1.5405 0. 0.0881]
[ 0. 0. 0. 0. ]]
[[ 7. 7. 7. 7. ]
[ 7. 7. 7. 7. ]
[ 7. 7. 7. 7. ]
[ 7. 7. 7. 7. ]
[ 0. 0.5652 0. 0. ]
[ 7. 7. 7. 7. ]
[ 0. 0.5652 0. 0. ]]
Process finished with exit code 0
Fancy indexing
# # fancy indexing 总是将数据复制到新数组中,与切片不同
arr = np.empty((8, 4))
for i in range(8):
arr[i] = i
print(arr)
# fancy indexing ,利用整数数组进行索引
print(arr[[4, 3, 0, 6]])
print(arr[[-3, -5, -7]])
# more on reshape in Chapter 12
arr = np.arange(32).reshape((8, 4))
print(arr)
# 选出的元素为 (1,0) (5,3) (7,1) (2,2)
print(arr[[1, 5, 7, 2], [0, 3, 1, 2]])
# 选出的矩阵由 1,5,7,2 行构成,每行中,列的顺序为 0,3,1,2
print(arr[[1, 5, 7, 2]][:, [0, 3, 1, 2]])
# np.ix_ 函数可以将 两个一维数组转换 为 一个用于选择方形区域的 索引器
print(arr[np.ix_([1, 5, 7, 2], [0, 3, 1, 2])])
# -----------------------------------------------------
[[ 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.]]
[[ 4. 4. 4. 4.]
[ 3. 3. 3. 3.]
[ 0. 0. 0. 0.]
[ 6. 6. 6. 6.]]
[[ 5. 5. 5. 5.]
[ 3. 3. 3. 3.]
[ 1. 1. 1. 1.]]
[[ 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]]
[ 4 23 29 10]
[[ 4 7 5 6]
[20 23 21 22]
[28 31 29 30]
[ 8 11 9 10]]
[[ 4 7 5 6]
[20 23 21 22]
[28 31 29 30]
[ 8 11 9 10]]
Process finished with exit code 0
Transposing arrays and swapping axes
arr = np.arange(15).reshape((3, 5))
print(arr)
# 转置
print(arr.T)
arr = np.random.randn(6, 3)
# 点乘
print(np.dot(arr.T, arr))
# 高维数组需要一个 由轴编号 组成的元组才能对这些轴进行转置(费脑子)
arr = np.arange(16).reshape((2, 2, 4))
print(arr)
print(arr.transpose((1, 0, 2)))
# np.ndarray.swapaxes() 接受一对 轴编号
print(arr)
print(arr.swapaxes(1, 2))
# -----------------------------------------------------------
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
[[ 0 5 10]
[ 1 6 11]
[ 2 7 12]
[ 3 8 13]
[ 4 9 14]]
[[ 3.9802 -0.2502 3.8607]
[ -0.2502 7.7333 2.5982]
[ 3.8607 2.5982 10.7432]]
[[[ 0 1 2 3]
[ 4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]]
[[[ 0 1 2 3]
[ 8 9 10 11]]
[[ 4 5 6 7]
[12 13 14 15]]]
[[[ 0 1 2 3]
[ 4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]]
[[[ 0 4]
[ 1 5]
[ 2 6]
[ 3 7]]
[[ 8 12]
[ 9 13]
[10 14]
[11 15]]]
Process finished with exit code 0
Universal Functions: Fast element-wise array functions
快速的元素级数组函数
arr = np.arange(10)
# 开方
print(np.sqrt(arr))
# 自然对数的底数 e = 2.718..... 的 x 次幂
print(np.exp(arr))
x = np.random.randn(8)
y = np.random.randn(8)
print(x)
print(y)
# 接受两个数组
print(np.maximum(x, y)) # element-wise maximum
arr = np.random.randn(7) * 5
print(arr)
# 接受 一个数组,返回 两个数组,(将输入数组的 小数部分 与 整数部分 拆分)
print(np.modf(arr))
# -----------------------------------------------------------
[ 0. 1. 1.4142 1.7321 2. 2.2361 2.4495 2.6458 2.8284
3. ]
[ 1. 2.7183 7.3891 20.0855 54.5982 148.4132
403.4288 1096.6332 2980.958 8103.0839]
[-0.9039 0.6059 0.1219 -1.4536 -1.1021 -0.0681 0.2509 0.7939]
[ 0.9495 -1.8458 1.0095 -1.2951 0.3019 -1.5155 -0.1693 -0.4673]
[ 0.9495 0.6059 1.0095 -1.2951 0.3019 -0.0681 0.2509 0.7939]
[ 0.6628 -7.8998 -6.6542 3.9569 -1.4966 2.8784 3.5424]
(array([ 0.6628, -0.8998, -0.6542, 0.9569, -0.4966, 0.8784, 0.5424]), array([ 0., -7., -6., 3., -1., 2., 3.]))
Process finished with exit code 0
Data processing using arrays
先不看了,累了…