• python中统计计数的几种方法+ 统计label个数


    1 使用字典统计计数

    1.1 使用字典dict()

            循环遍历出一个可迭代对象中的元素;

            如果字典没有该元素,那么就让该元素作为字典的键,并将该值赋值为1;

            如果键中存在这个元素,则将对应的值加1;

    比如:

    1. >>> lists = ['a','a','b',5,6,7,5]
    2. >>> count_dict = dict()
    3. >>> for item in lists:
    4. ... if item in count_dict:
    5. ... count_dict[item] += 1
    6. ... else:
    7. ... count_dict[item] = 1
    8. ...
    9. >>> count_dict
    10. {'a': 2, 'b': 1, 5: 2, 6: 1, 7: 1}
    11. >>>

    1.2 使用defaultdict()

            defaultdict(parameter)可以接受一个类型参数,如str,int等;

            但传递进来的参数不是用来约束值的类型,更不是约束键的类型,而是当键不存在时,实现值的初始化;

    1. defaultdict(int):初始化为 0
    2. defaultdict(float):初始化为 0.0
    3. defaultdict(str):初始化为 ''
    1. >>> from collections import defaultdict
    2. >>> lists = ['a', 'a', 'b', 5, 6, 7, 5]
    3. >>> count_dict = defaultdict(int)
    4. >>> for item in lists:
    5. ... count_dict[item] += 1
    6. ...
    7. >>> count_dict
    8. defaultdict(<class 'int'>, {'a': 2, 'b': 1, 5: 2, 6: 1, 7: 1})

    与(1)方法结果一致;

    1.3 使用集合(set)和列表(list)

            先使用set去重;

            然后循环的把每一个元素对应的次数lists.count(item)组成一个元素放在列表里面。

    1. >>> lists = ['a', 'a', 'b', 5, 6, 7, 5]
    2. >>> count_set = set(lists)
    3. >>> count_list = list()
    4. >>> for item in count_set:
    5. ... count_list.append((item,lists.count(item)))
    6. ...
    7. >>> count_list
    8. [('b', 1), (5, 2), (6, 1), (7, 1), ('a', 2)]

    1.4 使用Counter

    Counter 是一个容器对象,主要的作用是用来统计散列对象,使用三种方式来初始化;

            (1)参数里面放:可迭代对象:Counter("success");

            (2)传入关键字参数Counter((s = 3,c = 2,e = 1,u = 1))   ;

            (3)传入字典 Counter({"s":3,"c":2,"e":1,"u":1})     ;

    1. >>> from collections import Counter
    2. >>> lists = ['a', 'a', 'b', 5, 6, 7, 5]
    3. >>> a = Counter(lists)
    4. >>> a
    5. Counter({'a': 2, 5: 2, 'b': 1, 6: 1, 7: 1})
    6. >>> a.elements()
    7. object at 0x7fbffc3a8310>
    8. >>> a.most_common(2) #前两个出现频率最高的元素已经他们的次数,返回的是列表里面嵌套元组
    9. [('a', 2), (5, 2)]
    10. >>> b = Counter("success")
    11. >>> b
    12. Counter({'s': 3, 'c': 2, 'u': 1, 'e': 1})

    参考:python中统计计数的几种方法和Counter的介绍_dgteu28864的博客-CSDN博客

    自己做记录使用,希望大家去看原作者!

    2 统计label个数(0,1)

    使用的为1.3中的方法

    1. >>> df_data = pd.concat([df_train,df_dev,df_test],ignore_index= True)
    2. Python 3.8.1 (default, Jan 8 2020, 16:15:59)
    3. Type 'copyright', 'credits' or 'license' for more information
    4. IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.
    5. PyDev console: using IPython 7.19.0
    6. >>> lists = list(df_data['label'])
    7. >>> count_set = set(lists)
    8. >>> count_list = list()
    9. >>> for item in count_set:
    10. ... count_list.append((item, lists.count(item)))
    11. >>> count_list
    12. Out[6]: [(0, 2046), (1, 1743)]

  • 相关阅读:
    Ts是什么?
    C#将字符串日期转DateTime类型
    【C语言进阶篇】什么还没学会指针? 一篇文章让你彻底搞懂指针的奥秘
    Java调用第三方库JNA(C/C++)
    ZCMU--1379: The Black Hole of Numbers(C语言)
    使用postman 调用 Webservice 接口
    OneOS 下的 GUI 框架测试
    信号(软中断)
    LeetCode 刷题系列 -- 98. 验证二叉搜索树
    HLS基础issue
  • 原文地址:https://blog.csdn.net/qq_40671063/article/details/125996998