NumPy-排序搜索计数集合

import numpy as np
x=np.random.randint(1,10,10)
print(x)
[2 2 6 2 6 4 2 3 1 6]
x = np.random.rand(5, 5) * 10 
x = np.around(x, 2) 
print(x)

[[8.68 2.14 3.02 1.86 7.85]
 [1.76 5.14 6.52 4.49 7.2 ]
 [8.16 7.3  2.34 2.37 7.31]
 [1.58 9.48 0.14 9.29 6.6 ]
 [9.18 4.98 9.1  0.7  7.94]]
y=np.sort(x)#每行元素自己排序
print(y)
[[1.86 2.14 3.02 7.85 8.68]
 [1.76 4.49 5.14 6.52 7.2 ]
 [2.34 2.37 7.3  7.31 8.16]
 [0.14 1.58 6.6  9.29 9.48]
 [0.7  4.98 7.94 9.1  9.18]]
print(np.sort(x,axis=0))#每列元素各自排序
print(np.sort(x,axis=1))#每行元素各自排序
[[1.58 2.14 0.14 0.7  6.6 ]
 [1.76 4.98 2.34 1.86 7.2 ]
 [8.16 5.14 3.02 2.37 7.31]
 [8.68 7.3  6.52 4.49 7.85]
 [9.18 9.48 9.1  9.29 7.94]]
[[1.86 2.14 3.02 7.85 8.68]
 [1.76 4.49 5.14 6.52 7.2 ]
 [2.34 2.37 7.3  7.31 8.16]
 [0.14 1.58 6.6  9.29 9.48]
 [0.7  4.98 7.94 9.1  9.18]]
x = np.random.randint(0, 10, 10) 
print(x) 
y=np.argsort(x)#返回排序(升序)后的数组索引下标
z=np.argsort(-x)#降序
print(y)
print(x[y])
print(z)
print(x[z])
[2 2 2 2 0 6 1 7 8 5]
[4 6 0 1 2 3 9 5 7 8]
[0 1 2 2 2 2 5 6 7 8]
[8 7 5 9 0 1 2 3 6 4]
[8 7 6 5 2 2 2 2 1 0]
x=np.array([[1,2],[0,2]])
y=np.nonzero(x)
print(y,type(y))
z=np.transpose(y)
print(z)
(array([0, 0, 1], dtype=int64), array([0, 1, 1], dtype=int64)) <class 'tuple'>
[[0 0]
 [0 1]
 [1 1]]
x = np.array([1, 5, 1, 4, 3, 4, 4]) 
y = np.array([9, 4, 0, 4, 0, 2, 1]) 
a = np.lexsort([x]) 
b = np.lexsort([-y]) #降序
print(a)
print(b)
[0 2 4 3 5 6 1]
[0 1 3 5 6 2 4]

集合:
numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None) Find the unique elements of an array.
return_index=True 表示返回新列表元素在旧列表中的位置。
return_inverse=True表示返回旧列表元素在新列表中的位置。
return_counts=True表示返回新列表元素在旧列表中出现的次数。
numpy.in1d(ar1, ar2, assume_unique=False, invert=False) Test whether each element of a 1-D array is also present in a second array.
前面的数组是否包含于后面的数组,返回布尔值。返回的值是针对第一个参数的数组的,所以维数和第一个参数一致,布尔值与数组的元素位置也一一对应。

求两个集合的交集:
numpy.intersect1d(ar1, ar2, assume_unique=False, return_indices=False) Find the intersection of two arrays.
Return the sorted, unique values that are in both of the input arrays.
//求两个数组的唯一化+求交集+排序函数。

import numpy as np
from functools import reduce

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

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)  # [0 2 4]
print(y_ind)  # [1 0 2]
print(xy)  # [1 2 4]
print(x[x_ind])  # [1 2 4]
print(y[y_ind])  # [1 2 4]

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

求两个集合的并集:
numpy.union1d(ar1, ar2) Find the union of two arrays.
Return the unique, sorted array of values that are in either of the two input arrays.

//计算两个集合的并集,唯一化并排序。

import numpy as np
from functools import reduce

x = np.union1d([-1, 0, 1], [-2, 0, 2])
print(x)  # [-2 -1  0  1  2]
x = reduce(np.union1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
print(x)  # [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) Find the set difference of two arrays.
Return the unique values in ar1 that are not in ar2.
//集合的差,即元素存在于第一个函数不存在于第二个函数中。

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) Find the set exclusive-or of two arrays.
//集合的对称差,即两个集合的交集的补集。简言之,就是两个数组中各自独自拥有的元素的集合。

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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值