Task 05 排列搜索计数及集合操作

本文详细介绍了NumPy库中的排序方法,包括axis、kind和order参数的使用。此外,还讲解了如何在数组中搜索非零元素的索引,以及使用argwhere获取它们的位置。计数部分涵盖了集合操作,如查找元素在列表中的位置和计数。最后,文章讨论了布尔运算在集合操作中的应用,如交集、并集、差集和异或。
摘要由CSDN通过智能技术生成

1.排序,搜索和计数

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

import numpy as np
np.random.seed(20201031)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
print()
y = np.sort(x)
print(y)
print()
y = np.sort(x, axis=0)
print(y)
print()
y = np.sort(x, axis=1)
print(y)
#  [[6.87 1.3  5.09 3.39 1.71]
#   [7.47 9.22 5.48 4.55 2.48]
#   [2.25 4.58 3.97 0.8  5.05]
#   [0.16 9.97 3.48 6.41 7.24]
#   [0.94 1.59 5.41 5.25 8.51]]

#  [[1.3  1.71 3.39 5.09 6.87]
#   [2.48 4.55 5.48 7.47 9.22]
#   [0.8  2.25 3.97 4.58 5.05]
#   [0.16 3.48 6.41 7.24 9.97]
#   [0.94 1.59 5.25 5.41 8.51]]

#  [[0.16 1.3  3.48 0.8  1.71]
#   [0.94 1.59 3.97 3.39 2.48]
#   [2.25 4.58 5.09 4.55 5.05]
#   [6.87 9.22 5.41 5.25 7.24]
#   [7.47 9.97 5.48 6.41 8.51]]

#  [[1.3  1.71 3.39 5.09 6.87]
#   [2.48 4.55 5.48 7.47 9.22]
#   [0.8  2.25 3.97 4.58 5.05]
#   [0.16 3.48 6.41 7.24 9.97]
#   [0.94 1.59 5.25 5.41 8.51]]
import numpy as np
dt = np.dtype([('name', 'S10'), ('age', np.int)])
a = np.array([("Li",21), ("Lin", 25), ("Chen", 17),("Ye",27)], dtype=dt)
b = np.sort(a, order='name')
print(b)
print()
b = np.sort(a, order='age')
print(b)
#  [(b'Chen', 17) (b'Li', 21) (b'Lin', 25) (b'Ye', 27)]

#  [(b'Chen', 17) (b'Li', 21) (b'Lin', 25) (b'Ye', 27)]

numpy.argsort(a[, axis=-1, kind='quicksort', order=None])

import numpy as np
np.random.seed(20200612)
x = np.random.randint(0, 10, 10)
print(x)
print()
y = np.argsort(x)
print(y)
print()
print(x[y])
print()
y = np.argsort(-x)
print(y)
print()
print(x[y])

#  [6 1 8 5 5 4 1 2 9 1]

#  [1 6 9 7 5 3 4 0 2 8]

#  [1 1 1 2 4 5 5 6 8 9]

#  [8 2 0 3 4 5 7 1 6 9]

#  [9 8 6 5 5 4 2 1 1 1]
import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
print()
y = np.argsort(x)
print(y)
print()
y = np.argsort(x, axis=0)
print(y)
print()
y = np.argsort(x, axis=1)
print(y)
print()
y = np.array([np.take(x[i], np.argsort(x[i])) for i in range(5)])
#numpy.take(a, indices, axis=None, out=None, mode='raise')沿轴从数组中获取元素
print(y)
#  [[2.32 7.54 9.78 1.73 6.22]
#   [6.93 5.17 9.28 9.76 8.25]
#   [0.01 4.23 0.19 1.73 9.27]
#  [7.99 4.97 0.88 7.32 4.29]
#   [9.05 0.07 8.95 7.9  6.99]]

#  [[3 0 4 1 2]
#   [1 0 4 2 3]
#   [0 2 3 1 4]
#   [2 4 1 3 0]
#   [1 4 3 2 0]]

#  [[2 4 2 0 3]
#   [0 2 3 2 0]
#   [1 3 4 3 4]
#   [3 1 1 4 1]
#   [4 0 0 1 2]]

#  [[3 0 4 1 2]
#   [1 0 4 2 3]
#   [0 2 3 1 4]
#   [2 4 1 3 0]
#   [1 4 3 2 0]]

#  [[1.73 2.32 6.22 7.54 9.78]
#   [5.17 6.93 8.25 9.28 9.76]
#   [0.01 0.19 1.73 4.23 9.27]
#   [0.88 4.29 4.97 7.32 7.99]
#   [0.07 6.99 7.9  8.95 9.05]]

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

import numpy as np

np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
print()
index = np.lexsort([x[:, 0]])
print(index)
print()
y = x[index]
print(y)
print()
index = np.lexsort([-1 * x[:, 0]])
print(index)
print()
y = x[index]
print(y)
#  [[2.32 7.54 9.78 1.73 6.22]
#   [6.93 5.17 9.28 9.76 8.25]
#   [0.01 4.23 0.19 1.73 9.27]
#   [7.99 4.97 0.88 7.32 4.29]
#   [9.05 0.07 8.95 7.9  6.99]]

#  [2 0 1 3 4]

# [[0.01 4.23 0.19 1.73 9.27]
#   [2.32 7.54 9.78 1.73 6.22]
#   [6.93 5.17 9.28 9.76 8.25]
#   [7.99 4.97 0.88 7.32 4.29]
#   [9.05 0.07 8.95 7.9  6.99]]

#  [4 3 1 0 2]

#  [[9.05 0.07 8.95 7.9  6.99]
#   [7.99 4.97 0.88 7.32 4.29]
#   [6.93 5.17 9.28 9.76 8.25]
#   [2.32 7.54 9.78 1.73 6.22]
#   [0.01 4.23 0.19 1.73 9.27]]

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

import numpy as np

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

#  [[ 3  5  3]
#   [ 3 11  3]
#   [ 8 15  4]
#   [ 9 17 12]
#   [16 22 16]
#   [17 24 17]
#   [18 25 21]
#   [29 27 25]]

#  [[ 3  5  3]
#   [ 3 11  3]
#   [ 8 15  4]
#   [ 9 22 21]
#   [17 24 16]
#   [18 17 25]
#   [16 25 12]
#   [29 27 17]]
  #  选取每一列第三大的数的索引
import numpy as np

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

#  [17 24 17]

搜索
numpy.argmax(a[, axis=None, out=None])

import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
print()
y = np.argmax(x)
print(y) 
print()
y = np.argmax(x, axis=0)
print(y)
print()
y = np.argmax(x, axis=1)
print(y)
#  [[2.32 7.54 9.78 1.73 6.22]
#   [6.93 5.17 9.28 9.76 8.25]
#   [0.01 4.23 0.19 1.73 9.27]
#   [7.99 4.97 0.88 7.32 4.29]
#   [9.05 0.07 8.95 7.9  6.99]]

#  2

#  [4 0 0 1 2]

#  [2 3 4 0 0]

numpy.argmin(a[, axis=None, out=None])

import numpy as np
np.random.seed(20200612)
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
print()
y = np.argmin(x)
print(y) 
print()
y = np.argmin(x, axis=0)
print(y)
print()
y = np.argmin(x, axis=1)
print(y)
#  [[2.32 7.54 9.78 1.73 6.22]
#   [6.93 5.17 9.28 9.76 8.25]
#   [0.01 4.23 0.19 1.73 9.27]
#   [7.99 4.97 0.88 7.32 4.29]
#   [9.05 0.07 8.95 7.9  6.99]]

#  10

#  [2 4 2 0 3]

#  [3 1 0 2 1]

numppy.nonzero(a)
其值为非零元素的下标在对应轴上的值。

  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 中的非零值。
  #  二维数组
import numpy as np

x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
print(x)
print()
print(x.shape)
print()
print(x.ndim) 
print()
y = np.nonzero(x)
print(y)
print()
# (array([0, 1, 2, 2], dtype=int64), array([0, 1, 0, 1], dtype=int64))
print(np.array(y))
print()
print(np.array(y).shape) 
print()
print(np.array(y).ndim) 
print()
y = x[np.nonzero(x)]
print(y) 
print()
y = np.transpose(np.nonzero(x))
print(y)
#  [[3 0 0]
#   [0 4 0]
#   [5 6 0]]

#  (3, 3)

#  2

#  (array([0, 1, 2, 2], dtype=int64), array([0, 1, 0, 1], dtype=int64))

#  [[0 1 2 2]
#   [0 1 0 1]]

#  (2, 4)

#  2

#  [3 4 5 6]

#  [[0 0]
#   [1 1]
#   [2 0]
#   [2 1]]
  #  nonzero()将布尔数组转换成整数组进行操作
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(x)
print()
y = x > 3
print(y)
print()
y = np.nonzero(x > 3)
print(y)
print()
y = x[np.nonzero(x > 3)]
print(y)
print()
y = x[x > 3]
print(y)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

#  [[False False False]
#   [ True  True  True]
#   [ True  True  True]]

#  (array([1, 1, 1, 2, 2, 2], dtype=int64), array([0, 1, 2, 0, 1, 2], dtype=int64))

#  [4 5 6 7 8 9]

#  [4 5 6 7 8 9]
  #  满足条件 condition ,输出 x ,不满足输出 y
import numpy as np
x = np.arange(10)
print(x)
print()
y = np.where(x < 5, x, 10 * x)
print(y)
print()
x = np.array([[0, 1, 2],
[0, 2, 4],
[0, 3, 6]])
y = np.where(x < 4, x, -1)
print(y)
#  [0 1 2 3 4 5 6 7 8 9]

#  [ 0  1  2  3  4 50 60 70 80 90]

#  [[ 0  1  2]
#   [ 0  2 -1]
#   [ 0  3 -1]]

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

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

#  [7 5 2 6 4 1 3 0]

#  [0 0 4 5 7 8]

#  [0 1 5 5 8 8]

计数
numpy.count_nonzero(a, axis=None)

  #  返回数组中的非0元素个数
import numpy as np
x = np.count_nonzero(np.eye(4))
print(x) 
print()
x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]])
print(x)
print()
x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=0)
print(x)
print()
x = np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=1)
print(x) 
#  4

#  5

#  [1 1 1 1 1]

#  [2 3]

集合操作

numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)
a. return_index=True 表示返回新列表元素在旧列表中的位置。
b. return_inverse=True 表示返回旧列表元素在新列表中的位置。
c. return_counts=True 表示返回新列表元素在旧列表中出现的次数。

  #  找出数组中的唯一值并返回已排序的结果
import numpy as np

x = np.unique([1, 1, 3, 2, 3, 3])
print(x)
print()
x = sorted(set([1, 1, 3, 2, 3, 3]))
print(x) 
print()
x = np.array([[1, 1], [2, 3]])
u = np.unique(x)
print(u) 
print()
x = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])
y = np.unique(x, axis=0)
print(y)
print()
x = np.array(['a', 'b', 'b', 'c', 'a'])
u, index = np.unique(x, return_index=True)
print(u) 
print()
print(index) 
print()
print(x[index]) 
print()
x = np.array([1, 2, 6, 4, 2, 3, 2])
u, index = np.unique(x, return_inverse=True)
print(u) 
print()
print(index) 
print()
print(u[index]) 
print()
u, count = np.unique(x, return_counts=True)
print(u) 
print()
print(count)
#  [1 2 3]

#  [1, 2, 3]

#  [1 2 3]

#  [[1 0 0]
#   [2 3 4]]

#  ['a' 'b' 'c']

#  [0 1 3]

#  ['a' 'b' 'c']

#  [1 2 3 4 6]

#  [0 1 4 3 1 2 1]

#  [1 2 6 4 2 3 2]

#  [1 2 3 4 6]

#  [1 3 1 1 1]

布尔运算
numpy.in1d(ar1, ar2, assume_unique=False, invert=False)

  #  前面的数组是否包含于后面的数组,返回布尔值。返回的值是针对第一个参数的数组的,所以维数和第一个参数一致,布尔值与数组的元素位置也一一对应。
import numpy as np

test = np.array([0, 1, 2, 5, 0])
states = [0, 2]
mask = np.in1d(test, states)
print(mask)  
print()
print(test[mask])  
print()

mask = np.in1d(test, states, invert=True)
print(mask)  
print()
print(test[mask]) 
#  [ True False  True False  True]

#  [0 2 0]

#  [False  True False  True False]

#  [1 5]

求两个集合的交集
numpy.intersect1d(ar1, ar2, assume_unique=False, return_indices=False)

  #  求两个数组的唯一化+求交集+排序函数
import numpy as np
from functools import reduce

x = np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])
print(x)  

x = np.array([1, 1, 2, 3, 4])
y = np.array([2, 1, 4, 6])
xy, x_ind, y_ind = np.intersect1d(x, y, return_indices=True)
print(x_ind) 
print(y_ind) 
print(xy) 
print(x[x_ind]) 
print(y[y_ind])  

x = reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
print(x) 
#  [1 3]
#  [0 2 4]
#  [1 0 2]
#  [1 2 4]
#  [1 2 4]
#  [1 2 4]
#  [3]

求两个集合的并集
numpy.union1d(ar1, ar2)

  #  计算两个集合的并集,唯一化并排序
import numpy as np
from functools import reduce

x = np.union1d([-1, 0, 1], [-2, 0, 2])
print(x) 
print()
x = reduce(np.union1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
print(x)
#  [-2 -1  0  1  2]

#  [1 2 3 4 6]
'''
functools.reduce(function, iterable[, initializer])
将两个参数的 function 从左至右积累地应用到 iterable 的条目,以便将该可迭代对象缩减为单一的值。 例如,reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 是计算 ((((1+2)+3)+4)+5) 的值。 左边的参数 x 是积累值而右边的参数 y 则是来自 iterable 的更新值。 如果存在可选项 initializer,它会被放在参与计算的可迭代对象的条目之前,并在可迭代对象为空时作为默认值。 如果没有给出 initializer 并且 iterable 仅包含一个条目,则将返回第一项。

大致相当于:
def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        value = next(it)
    else:
        value = initializer
    for element in it:
        value = function(value, element)
    return value
'''

求两个集合的差值
numpy.setdiff1d(ar1, ar2, assume_unique=False)

  #  集合的差,即元素存在于第一个函数不存在于第二个函数中中
import numpy as np

a = np.array([1, 2, 3, 2, 4, 1])
b = np.array([3, 4, 5, 6])
x = np.setdiff1d(a, b)
print(x)  
#  【1 2】

求两个集合的异或
setxor1d(ar1, ar2, assume_unique=False)

  #  集合的对称差,即两个集合的交集的补集。简言之,就是两个数组中各自独自拥有的元素的集合。
import numpy as np

a = np.array([1, 2, 3, 2, 4, 1])
b = np.array([3, 4, 5, 6])
x = np.setxor1d(a, b)
print(x) 
#  [1 2 5 6]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值