为了账号安全,请及时绑定邮箱和手机立即绑定

numpy:数组中唯一值的最有效频率计数

numpy:数组中唯一值的最有效频率计数

白猪掌柜的 2019-08-26 09:55:52
numpy:数组中唯一值的最有效频率计数在numpy/中scipy,是否有一种有效的方法来获取数组中唯一值的频率计数?这些方面的东西:x = array( [1,1,1,2,2,2,5,25,1,1] )y = freq_count( x )print y>> [[1, 5], [2,3], [5,1], [25,1]](对你来说,R用户在那里,我基本上都在寻找这个table()功能)
查看完整描述

3 回答

?
白衣染霜花

TA贡献1796条经验 获得超10个赞

多维频率计数,即计数阵列。


>>> print(color_array    )

  array([[255, 128, 128],

   [255, 128, 128],

   [255, 128, 128],

   ...,

   [255, 128, 128],

   [255, 128, 128],

   [255, 128, 128]], dtype=uint8)



>>> np.unique(color_array,return_counts=True,axis=0)

  (array([[ 60, 151, 161],

    [ 60, 155, 162],

    [ 60, 159, 163],

    [ 61, 143, 162],

    [ 61, 147, 162],

    [ 61, 162, 163],

    [ 62, 166, 164],

    [ 63, 137, 162],

    [ 63, 169, 164],

   array([     1,      2,      2,      1,      4,      1,      1,      2,

         3,      1,      1,      1,      2,      5,      2,      2,

       898,      1,      1,  


查看完整回答
反对 回复 2019-08-26
?
动漫人物

TA贡献1815条经验 获得超10个赞

更新:原始答案中提到的方法已弃用,我们应该使用新方法:

>>> import numpy as np>>> x = [1,1,1,2,2,2,5,25,1,1]>>> np.array(np.unique(x, return_counts=True)).T
    array([[ 1,  5],
           [ 2,  3],
           [ 5,  1],
           [25,  1]])

原始答案:

你可以使用scipy.stats.itemfreq

>>> from scipy.stats import itemfreq>>> x = [1,1,1,2,2,2,5,25,1,1]>>> itemfreq(x)/usr/local/bin/python:1: DeprecationWarning: `itemfreq` is deprecated! `itemfreq` is deprecated and will be removed in a future version. Use instead `np.unique(..., return_counts=True)`array([[  1.,   5.],
       [  2.,   3.],
       [  5.,   1.],
       [ 25.,   1.]])


查看完整回答
反对 回复 2019-08-26
  • 3 回答
  • 0 关注
  • 1442 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信