Numpy提供一些针对一维ndarray的基本集合运算,最常用的是np.unique,用于找出数组中的唯一值,并返回已排序结果

names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
np.unique(names)
ints = np.array([3, 3, 3, 2, 2, 1, 1, 4, 4])
np.unique(ints)

与之等价的python代码

sorted(set(names))

np.in1d用于测试一个数组中的值在另一个数组中的成员资格,并返回布尔型数组

values = np.array([6, 0, 0, 3, 2, 5, 6])
np.in1d(values, [2, 3, 6])

results matching ""

    No results matching ""