集合运算 2018/11/11
======================================================================
1.np.unique 唯一值
# 它用干找出数组中的唯一值并返回已排序的结果
names= np.array( [ 'Bob','Joe ',' Will', ' Bob ' ])
np.unique(names) # array([' Bob ', ' Will', 'Bob', 'Joe '], dtype='<U5')
sorted(set(names))# 等价Python
ints = np.array([3, 3, 3, 2, 2, 1, 1, 4, 4])
np.unique( ints ) #array([1, 2, 3, 4])
2.np.in1d
# 用于测试一个数组中值在另一个数组中的成员资格,返回一个布尔型数组
values = np.array( [6, 0, 0, 3, 2, 5, 6])
np.in1d(values,[2,3,6]) #array([ True, False, False, True, True, False, True])
========================================================================
3.集合函数
- 1 unique(x) 计算x中唯一元素,并返回有序结果
- 2 intersect1d(x,y) 计算x和y中公共元素,并返回有序结果
- 3 union1d(x,y) 计算x和y并集,并返回有序结果
- 4 in1d(x,y) 得到一个表示x的元素是否包含于y的布尔型数组
- 5 setdiff1d(x,y) 集合差,即元素在x中且不在y中
- 6 setxor1d(x,y) 集合对称差,即存在于一个数组中但不同时存在于两个数组中的元素