zoukankan      html  css  js  c++  java
  • pandas的Categorical方法

      对于数据样本的标签,如果我们事先不知道这个样本有多少类别,那么可以对数据集的类别列进行统计,这时我们用pandas的Categorical方法就非常快的实现。

    1.说明: 

      你的数据最好是一个series,之后,pd.Categorical(series),得到分类的对象,使用categories或者codes进行查看

    2.操作:

    pd.Categorical( list ).codes 这样就可以直接得到原始数据的对应的序号列表,通过这样的处理可以将类别信息转化成数值信息 ,这样就可以应用到模型中去了 

    代码:

      

     1 import tensorflow
     2 import lightgbm as lgb
     3 import pandas as pd
     4 
     5 
     6 class Deng(object):
     7     def __init__(self):
     8         pass
     9 
    10     def main(self):
    11         temp = ['a', 'a', 'b', 'c', 'c']
    12         st = pd.Categorical(temp)
    13         print(st)
    14         # [a, a, b, c, c]
    15         # Categories(3, object): [a, b, c]
    16 
    17         # 遍历temp指出temp中每个字符所属类别的位置索引
    18         st2 = st.codes
    19         print(st2)
    20         # [0 0 1 2 2]
    21 
    22 
    23 if __name__ == '__main__':
    24     obj = Deng()
    25     obj.main()

      

  • 相关阅读:
    详解vue生命周期
    浅谈前端中的mvvm与mvc
    实际项目开发需要注意的tips
    toFixed()一不小心踩了一个坑
    git学习(持续踩坑中🤣)
    webpack基础
    创建git仓库并发布
    注册npm账号
    Invalid left-hand side in assignment
    关于项目中js原型的使用
  • 原文地址:https://www.cnblogs.com/demo-deng/p/9614377.html
Copyright © 2011-2022 走看看