网上已经有很多对于这几个函数的讲解资料,但总感觉讲得有些乱,特别是对于高维数组,如果指定了某个轴,就会变得很复杂。这篇文章根据”拆分“原理,对numpy数据的这几个函数进行简单而又详细地讲解。
拆分原理:指定哪个轴,就对哪个轴进行拆分,以得到多个数组,然后对这些数组进行对应元素操作。即
假设三维numpy数组array1的shape为(c, m, n),其中axis=0对应“c”,axis=1对应“m”,axis=2对应“n”。
若指定轴0(axis = 0),则对轴0进行拆分,得到c个shape为(m, n)的ndarray:array1[0, :, :]、array1[1, :, :]、… 、array1[c-1, :, :],然后对array1[0, :, :]、array1[1, :, :]、… 、array1[c-1, :, :]进行对应元素操作,得到输出结果,其shape为(m, n)。其他轴类似。
如果您还没明白,请继续往下看。
np.max()、np.argmax()、np.maximum()和np.min()、np.argmin()、np.minimum()一一对应,前一组与”最大“有关,后一组与”最小“有关,所以这里只讲”最大“
函数形式
np.max(array, axis=None, out=None, keepdims=False)
ndarray的形状是从左往右的,即由外往里计算,这和list是一样。
keepdims默认为False,如果keepdims=True,则输出的ndarray与原nadarray具有相同的形状。
import numpy as np
array1 = np.array([1, 3, 2, 8, 5])
print(array1)
print(np.max(array1)) # 返回array1的最大值
print(np.max(array1, axis=0)) # 指定0轴(即array1本身,输出结果一样)
输出结果:
[1 3 2 8 5]
8
8
array1 = np.reshape(list(range(12)), (3, 4))
print('array1 =', array1)
print('array1[0, :] =', array1[0, :])
print('array1[1, :] =', array1[1, :])
print('array1[2, :] =', array1[2, :])
print('np.max(array1, axis=0)=', np.max(array1, axis=0))
输出结果:
array1= [[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
array1[0, :] = [0 1 2 3]
array1[1, :] = [4 5 6 7]
array1[2, :] = [8 9 10 11]
np.max(array1, axis=0) = [8 9 10 11]
(2)指定轴1:axis=1
np.max(array1, axis=1)的意思就是:按第二个维度(即,列)对array1进行拆分,得到array1[:, 0]、array1[:, 1]、array1[:, 2]、array1[:, 3],然后对array1[:, 0]、array1[:, 1]、array1[:, 2]、array1[:, 3]的对应元素进行逐位比较,并取其最大者,构成新的ndarray。
import numpy as np
array1 = np.reshape(list(range(12)), (3, 4))
print('array1 =', array1)
print('array1[:, 0] =', array1[:, 0])
print('array1[:, 1] =', array1[:, 1])
print('array1[:, 2] =', array1[:, 2])
print('array1[:, 3] =', array1[:, 3])
print('np.max(array1, axis=1) =', np.max(array1, axis=1))
输出结果:
array1 = [[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
array1[:, 0] = [0 4 8]
array1[:, 1] = [1 5 9]
array1[:, 2] = [2 6 10]
array1[:, 3] = [3 7 11]
np.max(array1, axis=1) = [3 7 11]
import numpy as np
array1 = np.reshape(list(range(24)), (2, 3, 4))
print('array1 =', array1)
print('array1[0, :, :] =', array1[0, :, :])
print('array1[1, :, :] =', array1[1, :, :])
print('np.max(array1, axis=0) =', np.max(array1, axis=0))
输出结果:
array1 = [[[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
array1[0, :, :] = [[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
array1[1, :, :] = [[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
np.max(array1, axis=0) = [[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
(2)指定轴1:axis=1
np.max(array1, axis=1)的意思就是:按第二个维度对array1进行拆分,得到array1[:, 0, :]、array1[:, 1, :]、array1[:, 2, :],然后对array1[:, 0, :]、array1[:, 1, :]、array1[:, 2, :]的对应元素进行逐位比较,并取其最大者,构成新的ndarray。
import numpy as np
array1 = np.reshape(list(range(24)), (2, 3, 4))
print('array1 =', array1)
print('array1[:, 0, :] =', array1[:, 0, :])
print('array1[:, 1, :] =', array1[:, 1, :])
print('array1[:, 2, :] =', array1[:, 2, :])
print('np.max(array1, axis=1) =', np.max(array1, axis=1))
输出结果:
array1 = [[[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
array1[:, 0, :] = [[ 0 1 2 3]
[12 13 14 15]]
array1[:, 1, :] = [[ 4 5 6 7]
[16 17 18 19]]
array1[:, 2, :] = [[ 8 9 10 11]
[20 21 22 23]]
np.max(array1, axis=1) = [[ 8 9 10 11]
[20 21 22 23]]
(3)指定轴2:axis=2
np.max(array1, axis=2)的意思就是:按第三个维度对array1进行拆分,得到array1[:, :, 0]、array1[:, :, 1]、array1[:, :, 2]、array1[:, :, 3],然后对array1[:, :, 0]、array1[:, :, 1]、array1[:, :, 2]、array1[:, :, 3]的对应元素进行逐位比较,并取其最大者,构成新的ndarray。
import numpy as np
array1 = np.reshape(list(range(24)), (2, 3, 4))
print('array1 =', array1)
print('array1[:, :, 0] =', array1[:, :, 0])
print('array1[:, :, 1] =', array1[:, :, 1])
print('array1[:, :, 2] =', array1[:, :, 2])
print('array1[:, :, 3] =', array1[:, :, 3])
print('np.max(array1, axis=2) =', np.max(array1, axis=2))
输出结果:
array1 = [[[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
array1[:, :, 0] = [[ 0 4 8]
[12 16 20]]
array1[:, :, 1] = [[ 1 5 9]
[13 17 21]]
array1[:, :, 2] = [[ 2 6 10]
[14 18 22]]
array1[:, :, 3] = [[ 3 7 11]
[15 19 23]]
np.max(array1, axis=2) = [[ 3 7 11]
[15 19 23]]
np.argmax()与np.max()类似:np.max()返回指定轴的最大值,而np.argmax()返回指定轴的最大值对应的索引,指定哪个轴,就按哪个轴进行拆分。
import numpy as np
array1 = np.array([1, 3, 2, 8, 5])
print(array1)
print(np.argmax(array1)) # 返回array1的最大值对应的索引
print(np.argmax(array1, axis=0)) # 指定0轴(即array1本身,输出结果一样)
输出结果:
[1 3 2 8 5]
3
3
import numpy as np
array1 = np.reshape(list(range(12)), (3, 4))
print('array1 =', array1)
print('array1[0, :] =', array1[0, :])
print('array1[1, :] =', array1[1, :])
print('array1[2, :] =', array1[2, :])
print('np.max(array1, axis=0)=', np.max(array1, axis=0))
输出结果:
array1= [[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
array1[0, :] = [0 1 2 3]
array1[1, :] = [4 5 6 7]
array1[2, :] = [8 9 10 11]
np.argmax(array1, axis=0)= [2 2 2 2]
(2)指定轴1:axis=1
np.arg(array1, axis=1)的意思就是:按第二个维度(即,列)对array1进行拆分,得到array1[:, 0]、array1[:, 1]、array1[:, 2]、array1[:, 3],然后对array1[:, 0]、array1[:, 1]、array1[:, 2]、array1[:, 3]的对应元素进行逐位比较,并取其最大者所在的数组的索引(即,若某一对应元素的最大值在array1[:, 0],则返回索引0,;若某一对应元素的最大值在array1[:, 1],则返回索引1;依此类推),构成新的ndarray。
import numpy as np
array1 = np.reshape(list(range(12)), (3, 4))
print('array1 =', array1)
print('array1[:, 0] =', array1[:, 0])
print('array1[:, 1] =', array1[:, 1])
print('array1[:, 2] =', array1[:, 2])
print('array1[:, 3] =', array1[:, 3])
print('np.argmax(array1, axis=1) =', np.argmax(array1, axis=1))
输出结果:
array1 = [[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
array1[:, 0] = [0 4 8]
array1[:, 1] = [1 5 9]
array1[:, 2] = [2 6 10]
array1[:, 3] = [3 7 11]
np.argmax(array1, axis=1) = [3 3 3]
(3)没有指定任何轴
import numpy as np
array1 = np.reshape(list(range(12)), (3, 4))
array1[2, 3] = 1
print('array1 =', array1)
print('np.argmax(array1) =', np.argmax(array1))
输出结果:
array1 = [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 1]]
np.argmax(array1) = 10
由于Numpy是按维度大小的顺序由最底层开始存储的,所以
m
×
n
m \times n
m×n 的ndarray最大值的索引是:(np.argmax(array1) // n, np.argmax(array1) % n)
。
import numpy as np
array1 = np.reshape(list(range(24)), (2, 3, 4))
print('array1 =', array1)
print('array1[0, :, :] =', array1[0, :, :])
print('array1[1, :, :] =', array1[1, :, :])
print('np.argmax(array1, axis=0) =', np.argmax(array1, axis=0))
输出结果:
array1 = [[[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
array1[0, :, :] = [[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
array1[1, :, :] = [[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
np.argmax(array1, axis=0) = [[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
(2)指定轴1:axis=1
np.argmax(array1, axis=1)的意思就是:按第二个维度对array1进行拆分,得到array1[:, 0, :]、array1[:, 1, :]、array1[:, 2, :],然后对array1[:, 0, :]、array1[:, 1, :]、array1[:, 2, :]的对应元素进行逐位比较,并取其最大者所在的数组的索引(即,若某一对应元素的最大值在array1array1[:, 0, :],则返回索引0,;若某一对应元素的最大值在[:, 1, :],则返回索引1;依此类推),构成新的ndarray。
import numpy as np
array1 = np.reshape(list(range(24)), (2, 3, 4))
print('array1 =', array1)
print('array1[:, 0, :] =', array1[:, 0, :])
print('array1[:, 1, :] =', array1[:, 1, :])
print('array1[:, 2, :] =', array1[:, 2, :])
print('np.argmax(array1, axis=1) =', np.argmax(array1, axis=1))
输出结果:
array1 = [[[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
array1[:, 0, :] = [[ 0 1 2 3]
[12 13 14 15]]
array1[:, 1, :] = [[ 4 5 6 7]
[16 17 18 19]]
array1[:, 2, :] = [[ 8 9 10 11]
[20 21 22 23]]
np.argmax(array1, axis=1) = [[2 2 2 2]
[2 2 2 2]]
(3)指定轴2:axis=2
np.argmax(array1, axis=2)的意思就是:按第三个维度对array1进行拆分,得到array1[:, :, 0]、array1[:, :, 1]、array1[:, :, 2]、array1[:, :, 3],然后对array1[:, :, 0]、array1[:, :, 1]、array1[:, :, 2]、array1[:, :, 3]的对应元素进行逐位比较,并取其最大者所在的数组的索引(即,若某一对应元素的最大值在array1array1[:, :, 0],则返回索引0,;若某一对应元素的最大值在[:, :, 1],则返回索引1;依此类推),构成新的ndarray。
import numpy as np
array1 = np.reshape(list(range(24)), (2, 3, 4))
print('array1 =', array1)
print('array1[:, :, 0] =', array1[:, :, 0])
print('array1[:, :, 1] =', array1[:, :, 1])
print('array1[:, :, 2] =', array1[:, :, 2])
print('array1[:, :, 3] =', array1[:, :, 3])
print('np.argmax(array1, axis=2) =', np.argmax(array1, axis=2))
输出结果:
array1 = [[[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
array1[:, :, 0] = [[ 0 4 8]
[12 16 20]]
array1[:, :, 1] = [[ 1 5 9]
[13 17 21]]
array1[:, :, 2] = [[ 2 6 10]
[14 18 22]]
array1[:, :, 3] = [[ 3 7 11]
[15 19 23]]
np.argmax(array1, axis=2) = [[3 3 3]
[3 3 3]]
np.maximum(array1, array2):逐位比较array1和array2,并输出两者的最大值。
import numpy as np
array1 = np.array([[2, 5, 8], [3, 4, 6]])
array2 = np.array([[3, 4, 9], [2, 1, 6]])
array3 = np.array([1, 6, 7])
print('array1 =', array1)
print('array2 =', array2)
print('array3 =', array3)
print('np.maximum(array1, array2) =', np.maximum(array1, array2))
print('np.maximum(array1, array3) =', np.maximum(array1, array3)) # numpy的broadcast机制
print('np.maximum(array1, 4) =', np.maximum(array1, 4)) # numpy的broadcast机制
输出结果:
array1 = [[2 5 8]
[3 4 6]]
array2 = [[3 4 9]
[2 1 6]]
array3 = [1 6 7]
np.maximum(array1, array2) = [[3 5 9]
[3 4 6]]
np.maximum(array1, array3) = [[2 6 8]
[3 6 7]]
np.maximum(array1, 4) = [[4 5 8]
[4 4 6]]
import numpy as np
array1 = np.array([1, 3, 2, 8, 5])
print(array1)
print(np.sum(array1)) # 返回对array1的所有元素进行求和
print(np.sum(array1, axis=0)) # 指定0轴(即array1本身,输出结果一样)
import numpy as np
array1 = np.reshape(list(range(12)), (3, 4))
print('array1 =', array1)
print('array1[0, :] =', array1[0, :])
print('array1[1, :] =', array1[1, :])
print('array1[2, :] =', array1[2, :])
print('np.sum(array1, axis=0)=', np.sum(array1, axis=0))
输出结果:
array1= [[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
array1[0, :] = [0 1 2 3]
array1[1, :] = [4 5 6 7]
array1[2, :] = [8 9 10 11]
np.sum(array1, axis=0)= [12 15 18 21]
(2)指定轴1:axis=1
np.sum(array1, axis=1)的意思就是:按第二个维度(即,列)对array1进行拆分,得到array1[:, 0]、array1[:, 1]、array1[:, 2]、array1[:, 3],然后对array1[:, 0]、array1[:, 1]、array1[:, 2]、array1[:, 3]的对应元素进行逐位相加,构成新的ndarray。即
np.sum(array1, axis=1) = array1[:, 0] + array1[:, 1] + array1[:, 2] + array1[:, 3]
import numpy as np
array1 = np.reshape(list(range(12)), (3, 4))
print('array1 =', array1)
print('array1[:, 0] =', array1[:, 0])
print('array1[:, 1] =', array1[:, 1])
print('array1[:, 2] =', array1[:, 2])
print('array1[:, 3] =', array1[:, 3])
print('np.sum(array1, axis=1) =', np.sum(array1, axis=1))
输出结果:
array1 = [[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
array1[:, 0] = [0 4 8]
array1[:, 1] = [1 5 9]
array1[:, 2] = [2 6 10]
array1[:, 3] = [3 7 11]
np.sum(array1, axis=1) = [6 22 38]
import numpy as np
array1 = np.reshape(list(range(24)), (2, 3, 4))
print('array1 =', array1)
print('array1[0, :, :] =', array1[0, :, :])
print('array1[1, :, :] =', array1[1, :, :])
print('np.sum(array1, axis=0) =', np.sum(array1, axis=0))
输出结果:
array1 = [[[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
array1[0, :, :] = [[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
array1[1, :, :] = [[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
np.sum(array1, axis=0) = [[12 14 16 18]
[20 22 24 26]
[28 30 32 34]]
(2)指定轴1:axis=1
npsum(array1, axis=1)的意思就是:按第二个维度对array1进行拆分,得到array1[:, 0, :]、array1[:, 1, :]、array1[:, 2, :],然后对array1[:, 0, :]、array1[:, 1, :]、array1[:, 2, :]的对应元素进行逐位相加,构成新的ndarray,即
np.sum(array1, axis=1) = array1[:, 0, :] + array1[:, 1, :] + array1[:, 2, :]
import numpy as np
array1 = np.reshape(list(range(24)), (2, 3, 4))
print('array1 =', array1)
print('array1[:, 0, :] =', array1[:, 0, :])
print('array1[:, 1, :] =', array1[:, 1, :])
print('array1[:, 2, :] =', array1[:, 2, :])
print('np.sum(array1, axis=1) =', np.sum(array1, axis=1))
输出结果:
array1 = [[[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
array1[:, 0, :] = [[ 0 1 2 3]
[12 13 14 15]]
array1[:, 1, :] = [[ 4 5 6 7]
[16 17 18 19]]
array1[:, 2, :] = [[ 8 9 10 11]
[20 21 22 23]]
np.sum(array1, axis=1) = [[12 15 18 21]
[48 51 54 57]]
(3)指定轴2:axis=2
np.sum(array1, axis=2)的意思就是:按第三个维度对array1进行拆分,得到array1[:, :, 0]、array1[:, :, 1]、array1[:, :, 2]、array1[:, :, 3],然后对array1[:, :, 0]、array1[:, :, 1]、array1[:, :, 2]、array1[:, :, 3]的对应元素进行逐位相加,构成新的ndarray,即
np.sum(array1, axis=2) = array1[:, :, 0] + array1[:, :, 1] + array1[:, :, 2] + array1[:, :, 3]
import numpy as np
array1 = np.reshape(list(range(24)), (2, 3, 4))
print('array1 =', array1)
print('array1[:, :, 0] =', array1[:, :, 0])
print('array1[:, :, 1] =', array1[:, :, 1])
print('array1[:, :, 2] =', array1[:, :, 2])
print('array1[:, :, 3] =', array1[:, :, 3])
print('np.sum(array1, axis=2) =', np.sum(array1, axis=2))
输出结果:
array1 = [[[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
array1[:, :, 0] = [[ 0 4 8]
[12 16 20]]
array1[:, :, 1] = [[ 1 5 9]
[13 17 21]]
array1[:, :, 2] = [[ 2 6 10]
[14 18 22]]
array1[:, :, 3] = [[ 3 7 11]
[15 19 23]]
np.sum(array1, axis=2) = [[ 6 22 38]
[54 70 86]]