排序

numpy.sort()

  • numpy.sort(a[, axis=-1, kind='quicksort', order=None])Return a sortedcopyof an array.
    • axis:排序沿数组的(轴)方向,0表示按列,1表示按行,None表示展开来排序,默认为-1,表示沿最后的轴排序。
    • kind:排序的算法,提供了快排’quicksort’、混排’mergesort’、堆排’heapsort’, 默认为‘quicksort’。
    • order:排序的字段名,可指定字段排序,默认为None。

【例】

  1. import numpy as np
  2. np.random.seed(20200612)
  3. x = np.random.rand(5, 5) * 10
  4. x = np.around(x, 2)
  5. print(x)
  6. # [[2.32 7.54 9.78 1.73 6.22]
  7. # [6.93 5.17 9.28 9.76 8.25]
  8. # [0.01 4.23 0.19 1.73 9.27]
  9. # [7.99 4.97 0.88 7.32 4.29]
  10. # [9.05 0.07 8.95 7.9 6.99]]
  11. y = np.sort(x)
  12. print(y)
  13. # [[1.73 2.32 6.22 7.54 9.78]
  14. # [5.17 6.93 8.25 9.28 9.76]
  15. # [0.01 0.19 1.73 4.23 9.27]
  16. # [0.88 4.29 4.97 7.32 7.99]
  17. # [0.07 6.99 7.9 8.95 9.05]]
  18. y = np.sort(x, axis=0)
  19. print(y)
  20. # [[0.01 0.07 0.19 1.73 4.29]
  21. # [2.32 4.23 0.88 1.73 6.22]
  22. # [6.93 4.97 8.95 7.32 6.99]
  23. # [7.99 5.17 9.28 7.9 8.25]
  24. # [9.05 7.54 9.78 9.76 9.27]]
  25. y = np.sort(x, axis=1)
  26. print(y)
  27. # [[1.73 2.32 6.22 7.54 9.78]
  28. # [5.17 6.93 8.25 9.28 9.76]
  29. # [0.01 0.19 1.73 4.23 9.27]
  30. # [0.88 4.29 4.97 7.32 7.99]
  31. # [0.07 6.99 7.9 8.95 9.05]]

【例】

  1. import numpy as np
  2. dt = np.dtype([('name', 'S10'), ('age', np.int)])
  3. a = np.array([("Mike", 21), ("Nancy", 25), ("Bob", 17), ("Jane", 27)], dtype=dt)
  4. b = np.sort(a, order='name')
  5. print(b)
  6. # [(b'Bob', 17) (b'Jane', 27) (b'Mike', 21) (b'Nancy', 25)]
  7. b = np.sort(a, order='age')
  8. print(b)
  9. # [(b'Bob', 17) (b'Mike', 21) (b'Nancy', 25) (b'Jane', 27)]

如果排序后,想用元素的索引位置替代排序后的实际结果,该怎么办呢?

numpy.argsort()

  • numpy.argsort(a[, axis=-1, kind='quicksort', order=None]) Returns the indices that would sort an array.

【例】对数组沿给定轴执行间接排序,并使用指定排序类型返回数据的索引数组。这个索引数组用于构造排序后的数组。

  1. import numpy as np
  2. np.random.seed(20200612)
  3. x = np.random.randint(0, 10, 10)
  4. print(x)
  5. # [6 1 8 5 5 4 1 2 9 1]
  6. y = np.argsort(x)
  7. print(y)
  8. # [1 6 9 7 5 3 4 0 2 8]
  9. print(x[y])
  10. # [1 1 1 2 4 5 5 6 8 9]
  11. y = np.argsort(-x)
  12. print(y)
  13. # [8 2 0 3 4 5 7 1 6 9]
  14. print(x[y])
  15. # [9 8 6 5 5 4 2 1 1 1]

【例】

  1. import numpy as np
  2. np.random.seed(20200612)
  3. x = np.random.rand(5, 5) * 10
  4. x = np.around(x, 2)
  5. print(x)
  6. # [[2.32 7.54 9.78 1.73 6.22]
  7. # [6.93 5.17 9.28 9.76 8.25]
  8. # [0.01 4.23 0.19 1.73 9.27]
  9. # [7.99 4.97 0.88 7.32 4.29]
  10. # [9.05 0.07 8.95 7.9 6.99]]
  11. y = np.argsort(x)
  12. print(y)
  13. # [[3 0 4 1 2]
  14. # [1 0 4 2 3]
  15. # [0 2 3 1 4]
  16. # [2 4 1 3 0]
  17. # [1 4 3 2 0]]
  18. y = np.argsort(x, axis=0)
  19. print(y)
  20. # [[2 4 2 0 3]
  21. # [0 2 3 2 0]
  22. # [1 3 4 3 4]
  23. # [3 1 1 4 1]
  24. # [4 0 0 1 2]]
  25. y = np.argsort(x, axis=1)
  26. print(y)
  27. # [[3 0 4 1 2]
  28. # [1 0 4 2 3]
  29. # [0 2 3 1 4]
  30. # [2 4 1 3 0]
  31. # [1 4 3 2 0]]
  32. y = np.array([np.take(x[i], np.argsort(x[i])) for i in range(5)])
  33. #numpy.take(a, indices, axis=None, out=None, mode='raise')沿轴从数组中获取元素。
  34. print(y)
  35. # [[1.73 2.32 6.22 7.54 9.78]
  36. # [5.17 6.93 8.25 9.28 9.76]
  37. # [0.01 0.19 1.73 4.23 9.27]
  38. # [0.88 4.29 4.97 7.32 7.99]
  39. # [0.07 6.99 7.9 8.95 9.05]]

如何将数据按照某一指标进行排序呢?

numpy.lexsort()

  • numpy.lexsort(keys[, axis=-1]) Perform an indirect stable sort using a sequence of keys.(使用键序列执行间接稳定排序。)

给定多个可以在电子表格中解释为列的排序键,lexsort返回一个整数索引数组,该数组描述了按多个列排序的顺序。序列中的最后一个键用于主排序顺序,倒数第二个键用于辅助排序顺序,依此类推。keys参数必须是可以转换为相同形状的数组的对象序列。如果为keys参数提供了2D数组,则将其行解释为排序键,并根据最后一行,倒数第二行等进行排序。

【例】按照第一列的升序或者降序对整体数据进行排序。

  1. import numpy as np
  2. np.random.seed(20200612)
  3. x = np.random.rand(5, 5) * 10
  4. x = np.around(x, 2)
  5. print(x)
  6. # [[2.32 7.54 9.78 1.73 6.22]
  7. # [6.93 5.17 9.28 9.76 8.25]
  8. # [0.01 4.23 0.19 1.73 9.27]
  9. # [7.99 4.97 0.88 7.32 4.29]
  10. # [9.05 0.07 8.95 7.9 6.99]]
  11. index = np.lexsort([x[:, 0]])
  12. print(index)
  13. # [2 0 1 3 4]
  14. y = x[index]
  15. print(y)
  16. # [[0.01 4.23 0.19 1.73 9.27]
  17. # [2.32 7.54 9.78 1.73 6.22]
  18. # [6.93 5.17 9.28 9.76 8.25]
  19. # [7.99 4.97 0.88 7.32 4.29]
  20. # [9.05 0.07 8.95 7.9 6.99]]
  21. index = np.lexsort([-1 * x[:, 0]])
  22. print(index)
  23. # [4 3 1 0 2]
  24. y = x[index]
  25. print(y)
  26. # [[9.05 0.07 8.95 7.9 6.99]
  27. # [7.99 4.97 0.88 7.32 4.29]
  28. # [6.93 5.17 9.28 9.76 8.25]
  29. # [2.32 7.54 9.78 1.73 6.22]
  30. # [0.01 4.23 0.19 1.73 9.27]]

【例】

  1. import numpy as np
  2. x = np.array([1, 5, 1, 4, 3, 4, 4])
  3. y = np.array([9, 4, 0, 4, 0, 2, 1])
  4. a = np.lexsort([x])
  5. b = np.lexsort([y])
  6. print(a)
  7. # [0 2 4 3 5 6 1]
  8. print(x[a])
  9. # [1 1 3 4 4 4 5]
  10. print(b)
  11. # [2 4 6 5 1 3 0]
  12. print(y[b])
  13. # [0 0 1 2 4 4 9]
  14. z = np.lexsort([y, x])
  15. print(z)
  16. # [2 0 4 6 5 3 1]
  17. print(x[z])
  18. # [1 1 3 4 4 4 5]
  19. z = np.lexsort([x, y])
  20. print(z)
  21. # [2 4 6 5 3 1 0]
  22. print(y[z])
  23. # [0 0 1 2 4 4 9]

numpy.partition()

  • numpy.partition(a, kth, axis=-1, kind='introselect', order=None) Return a partitioned copy of an array.

Creates a copy of the array with its elements rearranged in such a way that the value of the element in k-th position is in the position it would be in a sorted array. All elements smaller than the k-th element are moved before this element and all equal or greater are moved behind it. The ordering of the elements in the two partitions is undefined.
【例】以索引是 kth 的元素为基准,将元素分成两部分,即大于该元素的放在其后面,小于该元素的放在其前面,这里有点类似于快排。

  1. import numpy as np
  2. np.random.seed(100)
  3. x = np.random.randint(1, 30, [8, 3])
  4. print(x)
  5. # [[ 9 25 4]
  6. # [ 8 24 16]
  7. # [17 11 21]
  8. # [ 3 22 3]
  9. # [ 3 15 3]
  10. # [18 17 25]
  11. # [16 5 12]
  12. # [29 27 17]]
  13. y = np.sort(x, axis=0)
  14. print(y)
  15. # [[ 3 5 3]
  16. # [ 3 11 3]
  17. # [ 8 15 4]
  18. # [ 9 17 12]
  19. # [16 22 16]
  20. # [17 24 17]
  21. # [18 25 21]
  22. # [29 27 25]]
  23. z = np.partition(x, kth=2, axis=0)
  24. print(z)
  25. # [[ 3 5 3]
  26. # [ 3 11 3]
  27. # [ 8 15 4]
  28. # [ 9 22 21]
  29. # [17 24 16]
  30. # [18 17 25]
  31. # [16 25 12]
  32. # [29 27 17]]

【例】选取每一列第三小的数

  1. import numpy as np
  2. np.random.seed(100)
  3. x = np.random.randint(1, 30, [8, 3])
  4. print(x)
  5. # [[ 9 25 4]
  6. # [ 8 24 16]
  7. # [17 11 21]
  8. # [ 3 22 3]
  9. # [ 3 15 3]
  10. # [18 17 25]
  11. # [16 5 12]
  12. # [29 27 17]]
  13. z = np.partition(x, kth=2, axis=0)
  14. print(z[2])
  15. # [ 8 15 4]

【例】选取每一列第三大的数据

  1. import numpy as np
  2. np.random.seed(100)
  3. x = np.random.randint(1, 30, [8, 3])
  4. print(x)
  5. # [[ 9 25 4]
  6. # [ 8 24 16]
  7. # [17 11 21]
  8. # [ 3 22 3]
  9. # [ 3 15 3]
  10. # [18 17 25]
  11. # [16 5 12]
  12. # [29 27 17]]
  13. z = np.partition(x, kth=-3, axis=0)
  14. print(z[-3])
  15. # [17 24 17]

numpy.argpartition()

  • numpy.argpartition(a, kth, axis=-1, kind='introselect', order=None)

Perform an indirect partition along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as a that index data along the given axis in partitioned order.
【例】

  1. import numpy as np
  2. np.random.seed(100)
  3. x = np.random.randint(1, 30, [8, 3])
  4. print(x)
  5. # [[ 9 25 4]
  6. # [ 8 24 16]
  7. # [17 11 21]
  8. # [ 3 22 3]
  9. # [ 3 15 3]
  10. # [18 17 25]
  11. # [16 5 12]
  12. # [29 27 17]]
  13. y = np.argsort(x, axis=0)
  14. print(y)
  15. # [[3 6 3]
  16. # [4 2 4]
  17. # [1 4 0]
  18. # [0 5 6]
  19. # [6 3 1]
  20. # [2 1 7]
  21. # [5 0 2]
  22. # [7 7 5]]
  23. z = np.argpartition(x, kth=2, axis=0)
  24. print(z)
  25. # [[3 6 3]
  26. # [4 2 4]
  27. # [1 4 0]
  28. # [0 3 2]
  29. # [2 1 1]
  30. # [5 5 5]
  31. # [6 0 6]
  32. # [7 7 7]]

【例】选取每一列第三小的数的索引

  1. import numpy as np
  2. np.random.seed(100)
  3. x = np.random.randint(1, 30, [8, 3])
  4. print(x)
  5. # [[ 9 25 4]
  6. # [ 8 24 16]
  7. # [17 11 21]
  8. # [ 3 22 3]
  9. # [ 3 15 3]
  10. # [18 17 25]
  11. # [16 5 12]
  12. # [29 27 17]]
  13. z = np.argpartition(x, kth=2, axis=0)
  14. print(z[2])
  15. # [1 4 0]

【例】选取每一列第三大的数的索引

  1. import numpy as np
  2. np.random.seed(100)
  3. x = np.random.randint(1, 30, [8, 3])
  4. print(x)
  5. # [[ 9 25 4]
  6. # [ 8 24 16]
  7. # [17 11 21]
  8. # [ 3 22 3]
  9. # [ 3 15 3]
  10. # [18 17 25]
  11. # [16 5 12]
  12. # [29 27 17]]
  13. z = np.argpartition(x, kth=-3, axis=0)
  14. print(z[-3])
  15. # [2 1 7]

搜索

numpy.argmax()

  • numpy.argmax(a[, axis=None, out=None])Returns the indices of the maximum values along an axis.

【例】

  1. import numpy as np
  2. np.random.seed(20200612)
  3. x = np.random.rand(5, 5) * 10
  4. x = np.around(x, 2)
  5. print(x)
  6. # [[2.32 7.54 9.78 1.73 6.22]
  7. # [6.93 5.17 9.28 9.76 8.25]
  8. # [0.01 4.23 0.19 1.73 9.27]
  9. # [7.99 4.97 0.88 7.32 4.29]
  10. # [9.05 0.07 8.95 7.9 6.99]]
  11. y = np.argmax(x)
  12. print(y) # 2
  13. y = np.argmax(x, axis=0)
  14. print(y)
  15. # [4 0 0 1 2]
  16. y = np.argmax(x, axis=1)
  17. print(y)
  18. # [2 3 4 0 0]

numpy.argmin()

  • numpy.argmin(a[, axis=None, out=None])Returns the indices of the minimum values along an axis.

【例】

  1. import numpy as np
  2. np.random.seed(20200612)
  3. x = np.random.rand(5, 5) * 10
  4. x = np.around(x, 2)
  5. print(x)
  6. # [[2.32 7.54 9.78 1.73 6.22]
  7. # [6.93 5.17 9.28 9.76 8.25]
  8. # [0.01 4.23 0.19 1.73 9.27]
  9. # [7.99 4.97 0.88 7.32 4.29]
  10. # [9.05 0.07 8.95 7.9 6.99]]
  11. y = np.argmin(x)
  12. print(y) # 10
  13. y = np.argmin(x, axis=0)
  14. print(y)
  15. # [2 4 2 0 3]
  16. y = np.argmin(x, axis=1)
  17. print(y)
  18. # [3 1 0 2 1]

numppy.nonzero()

  • numppy.nonzero(a) Return the indices of the elements that are non-zero.其值为非零元素的下标在对应轴上的值。
  1. 只有a中非零元素才会有索引值,那些零值元素没有索引值。
  2. 返回一个长度为a.ndim的元组(tuple),元组的每个元素都是一个整数数组(array)。
  3. 每一个array均是从一个维度上来描述其索引值。比如,如果a是一个二维数组,则tuple包含两个array,第一个array从行维度来描述索引值;第二个array从列维度来描述索引值。
  4. np.transpose(np.nonzero(x)) 函数能够描述出每一个非零元素在不同维度的索引值。
  5. 通过a[nonzero(a)]得到所有a中的非零值。

【例】一维数组

  1. import numpy as np
  2. x = np.array([0, 2, 3])
  3. print(x) # [0 2 3]
  4. print(x.shape) # (3,)
  5. print(x.ndim) # 1
  6. y = np.nonzero(x)
  7. print(y) # (array([1, 2], dtype=int64),)
  8. print(np.array(y)) # [[1 2]]
  9. print(np.array(y).shape) # (1, 2)
  10. print(np.array(y).ndim) # 2
  11. print(np.transpose(y))
  12. # [[1]
  13. # [2]]
  14. print(x[np.nonzero(x)])
  15. #[2, 3]

【例】二维数组

  1. import numpy as np
  2. x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
  3. print(x)
  4. # [[3 0 0]
  5. # [0 4 0]
  6. # [5 6 0]]
  7. print(x.shape) # (3, 3)
  8. print(x.ndim) # 2
  9. y = np.nonzero(x)
  10. print(y)
  11. # (array([0, 1, 2, 2], dtype=int64), array([0, 1, 0, 1], dtype=int64))
  12. print(np.array(y))
  13. # [[0 1 2 2]
  14. # [0 1 0 1]]
  15. print(np.array(y).shape) # (2, 4)
  16. print(np.array(y).ndim) # 2
  17. y = x[np.nonzero(x)]
  18. print(y) # [3 4 5 6]
  19. y = np.transpose(np.nonzero(x))
  20. print(y)
  21. # [[0 0]
  22. # [1 1]
  23. # [2 0]
  24. # [2 1]]

【例】三维数组

  1. import numpy as np
  2. x = np.array([[[0, 1], [1, 0]], [[0, 1], [1, 0]], [[0, 0], [1, 0]]])
  3. print(x)
  4. # [[[0 1]
  5. # [1 0]]
  6. #
  7. # [[0 1]
  8. # [1 0]]
  9. #
  10. # [[0 0]
  11. # [1 0]]]
  12. print(np.shape(x)) # (3, 2, 2)
  13. print(x.ndim) # 3
  14. y = np.nonzero(x)
  15. print(np.array(y))
  16. # [[0 0 1 1 2]
  17. # [0 1 0 1 1]
  18. # [1 0 1 0 0]]
  19. print(np.array(y).shape) # (3, 5)
  20. print(np.array(y).ndim) # 2
  21. print(y)
  22. # (array([0, 0, 1, 1, 2], dtype=int64), array([0, 1, 0, 1, 1], dtype=int64), array([1, 0, 1, 0, 0], dtype=int64))
  23. print(x[np.nonzero(x)])
  24. #[1 1 1 1 1]

【例】nonzero()将布尔数组转换成整数数组进行操作。

  1. import numpy as np
  2. x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  3. print(x)
  4. # [[1 2 3]
  5. # [4 5 6]
  6. # [7 8 9]]
  7. y = x > 3
  8. print(y)
  9. # [[False False False]
  10. # [ True True True]
  11. # [ True True True]]
  12. y = np.nonzero(x > 3)
  13. print(y)
  14. # (array([1, 1, 1, 2, 2, 2], dtype=int64), array([0, 1, 2, 0, 1, 2], dtype=int64))
  15. y = x[np.nonzero(x > 3)]
  16. print(y)
  17. # [4 5 6 7 8 9]
  18. y = x[x > 3]
  19. print(y)
  20. # [4 5 6 7 8 9]

numpy.where()

  • numpy.where(condition, [x=None, y=None]) Return elements chosen from x or y depending on condition.

【例】满足条件condition,输出x,不满足输出y

  1. import numpy as np
  2. x = np.arange(10)
  3. print(x)
  4. # [0 1 2 3 4 5 6 7 8 9]
  5. y = np.where(x < 5, x, 10 * x)
  6. print(y)
  7. # [ 0 1 2 3 4 50 60 70 80 90]
  8. x = np.array([[0, 1, 2],
  9. [0, 2, 4],
  10. [0, 3, 6]])
  11. y = np.where(x < 4, x, -1)
  12. print(y)
  13. # [[ 0 1 2]
  14. # [ 0 2 -1]
  15. # [ 0 3 -1]]

【例】只有condition,没有xy,则输出满足条件 (即非0) 元素的坐标 (等价于numpy.nonzero)。这里的坐标以tuple的形式给出,通常原数组有多少维,输出的tuple中就包含几个数组,分别对应符合条件元素的各维坐标。

  1. import numpy as np
  2. x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  3. y = np.where(x > 5)
  4. print(y)
  5. # (array([5, 6, 7], dtype=int64),)
  6. print(x[y])
  7. # [6 7 8]
  8. y = np.nonzero(x > 5)
  9. print(y)
  10. # (array([5, 6, 7], dtype=int64),)
  11. print(x[y])
  12. # [6 7 8]
  13. x = np.array([[11, 12, 13, 14, 15],
  14. [16, 17, 18, 19, 20],
  15. [21, 22, 23, 24, 25],
  16. [26, 27, 28, 29, 30],
  17. [31, 32, 33, 34, 35]])
  18. y = np.where(x > 25)
  19. print(y)
  20. # (array([3, 3, 3, 3, 3, 4, 4, 4, 4, 4], dtype=int64), array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4], dtype=int64))
  21. print(x[y])
  22. # [26 27 28 29 30 31 32 33 34 35]
  23. y = np.nonzero(x > 25)
  24. print(y)
  25. # (array([3, 3, 3, 3, 3, 4, 4, 4, 4, 4], dtype=int64), array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4], dtype=int64))
  26. print(x[y])
  27. # [26 27 28 29 30 31 32 33 34 35]

numpy.searchsorted()

  • numpy.searchsorted(a, v[, side='left', sorter=None])Find indices where elements should be inserted to maintain order.
    • a:一维输入数组。当sorter参数为None的时候,a必须为升序数组;否则,sorter不能为空,存放a中元素的index,用于反映a数组的升序排列方式。
    • v:插入a数组的值,可以为单个元素,list或者ndarray
    • side:查询方向,当为left时,将返回第一个符合条件的元素下标;当为right时,将返回最后一个符合条件的元素下标。
    • sorter:一维数组存放a数组元素的 index,index 对应元素为升序。

【例】

  1. import numpy as np
  2. x = np.array([0, 1, 5, 9, 11, 18, 26, 33])
  3. y = np.searchsorted(x, 15)
  4. print(y) # 5
  5. y = np.searchsorted(x, 15, side='right')
  6. print(y) # 5
  7. y = np.searchsorted(x, -1)
  8. print(y) # 0
  9. y = np.searchsorted(x, -1, side='right')
  10. print(y) # 0
  11. y = np.searchsorted(x, 35)
  12. print(y) # 8
  13. y = np.searchsorted(x, 35, side='right')
  14. print(y) # 8
  15. y = np.searchsorted(x, 11)
  16. print(y) # 4
  17. y = np.searchsorted(x, 11, side='right')
  18. print(y) # 5
  19. y = np.searchsorted(x, 0)
  20. print(y) # 0
  21. y = np.searchsorted(x, 0, side='right')
  22. print(y) # 1
  23. y = np.searchsorted(x, 33)
  24. print(y) # 7
  25. y = np.searchsorted(x, 33, side='right')
  26. print(y) # 8

【例】

  1. import numpy as np
  2. x = np.array([0, 1, 5, 9, 11, 18, 26, 33])
  3. y = np.searchsorted(x, [-1, 0, 11, 15, 33, 35])
  4. print(y) # [0 0 4 5 7 8]
  5. y = np.searchsorted(x, [-1, 0, 11, 15, 33, 35], side='right')
  6. print(y) # [0 1 5 5 8 8]

【例】

  1. import numpy as np
  2. x = np.array([0, 1, 5, 9, 11, 18, 26, 33])
  3. np.random.shuffle(x)
  4. print(x) # [33 1 9 18 11 26 0 5]
  5. x_sort = np.argsort(x)
  6. print(x_sort) # [6 1 7 2 4 3 5 0]
  7. y = np.searchsorted(x, [-1, 0, 11, 15, 33, 35], sorter=x_sort)
  8. print(y) # [0 0 4 5 7 8]
  9. y = np.searchsorted(x, [-1, 0, 11, 15, 33, 35], side='right', sorter=x_sort)
  10. print(y) # [0 1 5 5 8 8]

计数

numpy.count_nonzero()

  • numpy.count_nonzero(a, axis=None) Counts the number of non-zero values in the array a.

【例】返回数组中的非0元素个数。

  1. import numpy as np
  2. x = np.count_nonzero(np.eye(4))
  3. print(x) # 4
  4. x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]])
  5. print(x) # 5
  6. x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=0)
  7. print(x) # [1 1 1 1 1]
  8. x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=1)
  9. print(x) # [2 3]