一、np.unique()
newar=unique(oldar, return_index=False, return_inverse=False, return_counts=False, axis=None, *, equal_nan=True)
1.1 简介
对于一维数组或者列表,np.unique() 函数 去除其中重复的元素 ,并按元素 由小到大 返回一个新的无元素重复的元组或者列表。
1.2 参数详解
旧列表是指参数值oldar,新列表是指unique函数的返回值 newar
- ar 待输入的数组
- return_index=False 如果为 True,返回新列表元素在旧列表中的位置(下标),并以列表形式存储。
- return_inverse=False 如果为True,返回旧列表元素在新列表中的位置(下标),并以列表形式存储。
- return_counts:如果为 True,返回新数组中的元素在原数组中的出现次数。
1.3 实例
>>> a=[1,2,2,3,5,6,7,8,4,4,9]
>>> new_a , a_index ,a_inverse = np.unique(a,return_index=True,return_inverse=True)
>>>
>>>
>>>
>>> print(new_a)
[1 2 3 4 5 6 7 8 9]
>>> print(a_index)
[ 0 1 3 9 5 6 7 8 11]
>>> print(a_inverse)
[0 1 1 2 2 4 5 6 7 3 3 8]
>>>
>>>> new_a , a_index ,a_inverse ,a_count= np.unique(a,return_index=True,retur
n_inverse=True,return_counts=True)
>>> print(a_count)
[1 2 2 2 1 1 1 1 1]
>>>
二、np.argmax()
argmax(a, axis=None, out=None, *, keepdims=<no value>)
返回a数组中最大值的索引(最靠前的索引),
>>> max=[1,2,5,3,3,4,4,4,5,4,4,5]
>>> print(np.argmax(max))
2
>>>