zoukankan      html  css  js  c++  java
  • 天池nlp新人赛_task2:数据预处理改进和一些思路

    今天想解决下面几个问题。
    1.lightgbm cpu太慢了,我装了gpu的版本,对比了之后发现训练速度从10min缩短到8min。感觉很少,不知道是不是我姿势错误。
    过程如下。

    • 安装软件依赖sudo apt-get install --no-install-recommends git cmake build-essential libboost-dev libboost-system-dev libboost-filesystem-dev
    • 安装需要的python库 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple setuptools wheel numpy scipy scikit-learn -U
    • 安装lightGBM-GPU pip install lightgbm --install-option=--gpu --install-option="--opencl-include-dir=/usr/local/cuda/include/" --install-option="--opencl-library=/usr/local/cuda-10.0/lib64/libOpenCL.so"

    2.针对数据预处理做些改进
    查看文章长度分布

    _ = plt.hist(train_data['text_len'], bins=200)
    plt.xlabel('Text char count')
    plt.title("Histogram of char count")
    

    查看类别分布

    train_data['label'].value_counts().plot(kind='bar')
    plt.title('News class count')
    plt.xlabel("category")
    

    统计所有文本中出现最多的词

    from collections import Counter
    all_lines = ' '.join(list(train_data['text']))
    word_count = Counter(all_lines.split(" "))
    word_count = sorted(word_count.items(), key=lambda d:d[1], reverse = True)
    
    print(len(word_count))
    
    print(word_count[0])
    
    print(word_count[-1])
    

    统计字符出现的文本个数

    from collections import Counter
    train_df['text_unique'] = train_df['text'].apply(lambda x: ' '.join(list(set(x.split(' ')))))
    all_lines = ' '.join(list(train_df['text_unique']))
    word_count = Counter(all_lines.split(" "))
    word_count = sorted(word_count.items(), key=lambda d:int(d[1]), reverse = True)
    
    print(word_count[0])
    
    print(word_count[1])
    
    print(word_count[2])
    

    假设最多的三个字符3750 648 900是句子的标点符号
    每篇新闻平均有多少句子构成,句子长度 Sentence length

    # 字符3750 648 900是句子的标点符号
    # 每篇新闻平均有多少句子构成,句子长度 Sentence length
    
    def Count(x):
        num = 0
        for i in x:
            if i in ['3750', '648', '900']:
                num += 1
        return num
    
    train_data['sentence_length'] = train_data['text'].apply(lambda x: Count(x.split(" ")))
    

    统计每类新闻中出现次数最多的几个字符

    # 统计每类新闻中出现次数最多的几个字符
    from collections import Counter
    
    train_data.groupby(['label'])['text'].apply(lambda x: ' '.join(list(x))).apply(lambda x:
                        sorted(Counter(x.split(" ")).items(), key = lambda d:d[1], reverse = True)).apply(lambda x: x[0:10])
    

    3.可能的改进
    尽可能分词,比如将经常成对出现的数字作为一个词,降低TF-IDF的维度
    调整TF-IDF的阈值
    对TF-IDF提取主成分
    对少数样本进行上采样
    等等

  • 相关阅读:
    js常用函数和事件
    100多个基础常用JS函数和语法集合大全
    JavaScript 使用构造法的正则表达式的注意要点
    终端运行node并设置node_env为production
    给dom元素绑定click等事件无效的问题
    cnpm,gulp等命令在zsh终端上报错的问题
    Node.js读写文件之批量替换图片
    DOM操作原生js 的bug,使用jQuery 可以消除
    js 比较版本号(二)
    js 比较版本号(一)
  • 原文地址:https://www.cnblogs.com/zuotongbin/p/13363402.html
Copyright © 2011-2022 走看看