numpy 中的一些常用方法函数记录(1)

1.  np.argsort() 

numpy中的argsort函数是排序函数,返回的是每个元素的排序序号(数组值从小到大的索引序号)。

#One dimensional array:
>>> x = np.array([3, 1, 2])
>>> np.argsort(x)
array([1, 2, 0])
#Two-dimensional array:
>>> x = np.array([[0, 3], [2, 2]])
>>> x
array([[0, 3],
       [2, 2]])

>>> np.argsort(x, axis=0)
array([[0, 1],
       [1, 0]])

>>> np.argsort(x, axis=1)
array([[0, 1],
       [0, 1]])

 若需参看更加详细参数信息,请访问官网地址:numpy中argsort函数使用

 

2.  np.bincount(x)

统计函数,统计数组x中非负整数值出现的次数,并返回size为 max(x) +1 按数值从小到大出现的次数  的数组 。

>>> np.bincount(np.arange(5))
array([1, 1, 1, 1, 1])
>>> np.bincount(np.array([0, 1, 1, 3, 2, 1, 7]))
array([1, 3, 1, 1, 0, 0, 0, 1])
>>>
>>> x = np.array([0, 1, 1, 3, 2, 1, 7, 23])
>>> np.bincount(x).size == np.amax(x)+1
True

#The input array needs to be of integer dtype, otherwise a TypeError is raised:
>>>
>>> np.bincount(np.arange(5, dtype=float))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: array cannot be safely cast to required type

 若需参看更加详细参数信息,请访问官网地址:numpy 中 bincount函数用法

 

3.  np.argmax()

返回沿轴的最大值的索引。

>>> a = np.arange(6).reshape(2,3)
>>> a
array([[0, 1, 2],
       [3, 4, 5]])

>>> np.argmax(a)
5

>>> np.argmax(a, axis=0)
array([1, 1, 1])

>>> np.argmax(a, axis=1)
array([2, 2])
Indexes of the maximal elements of a N-dimensional array:
>>>

>>> ind = np.unravel_index(np.argmax(a, axis=None), a.shape)
>>> ind
(1, 2)
>>> a[ind]
5
>>>
>>> b = np.arange(6)
>>> b[1] = 5
>>> b
array([0, 5, 2, 3, 4, 5])

>>> np.argmax(b)  # Only the first occurrence is returned.
1

若需参看更加详细参数信息,请访问官网地址:numpy 中argmax函数使用

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值