【问题标题】:Prepare data for scikit-learn为 scikit-learn 准备数据
【发布时间】:2015-04-07 19:05:59
【问题描述】:

我正在从事一个关于作者署名的小型 NLP 项目:我有一些来自两位作者的文本,我想知道是谁写的。

我有一些经过预处理的文本(标记化、后标记等),我想将其加载到 sciki-learn 中。

文件有这个形状:

Testo   -   SPN Testo   testare+v+indic+pres+nil+1+sing testo+n+m+sing  O
:   -   XPS colon   colon+punc  O
"   -   XPO "   quotation_mark+punc O
Buongiorno  -   I   buongiorno  buongiorno+inter buongiorno+n+m+_   O
a   -   E   a   a+prep  O
tutti   -   PP  tutto   tutto+adj+m+plur+pst+ind tutto+pron+_+m+_+plur+ind  O
.   <eos>   XPS full_stop   full_stop+punc  O
Ci  -   PP  pro loc+pron+loc+_+3+_+clit pro+pron+accdat+_+1+plur+clit   O
sarebbe -   VI  essere  essere+v+cond+pres+nil+2+sing   O
molto   -   B   molto   molto+adj+m+sing+pst+ind

所以它是一个制表符分隔的 6 列文本文件(单词、句尾标记、词性、引理、形态信息和命名实体识别标记)。

每个文件代表一个要分类的文档。

为 scikit learn 塑造它们的最佳方式是什么?

【问题讨论】:

    标签: dataset python scikit-learn nlp


    【解决方案1】:

    这里描述了他们在 scikit-learn 示例 https://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html# 中使用的结构 http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_files.html

    替换这个

    # Load some categories from the training set
    if opts.all_categories:
        categories = None
    else:
        categories = [
            'alt.atheism',
            'talk.religion.misc',
            'comp.graphics',
            'sci.space',
        ]
    
    if opts.filtered:
        remove = ('headers', 'footers', 'quotes')
    else:
        remove = ()
    
    print("Loading 20 newsgroups dataset for categories:")
    print(categories if categories else "all")
    
    data_train = fetch_20newsgroups(subset='train', categories=categories,
                                    shuffle=True, random_state=42,
                                    remove=remove)
    
    data_test = fetch_20newsgroups(subset='test', categories=categories,
                                   shuffle=True, random_state=42,
                                   remove=remove)
    

    使用您的数据加载语句,例如:

    # Load some categories from the training set
    categories = [
            'high',
            'low',
    ]
    
    print("loading dataset for categories:")
    print(categories if categories else "all")
    
    train_path='c:/Users/username/Documents/SciKit/train'
    data_train = load_files(train_path, encoding='latin1')
    
    test_path='c:/Users/username/Documents/SciKit/test'
    data_test = load_files(test_path, encoding='latin1')
    

    并在每个训练和测试目录中为您的类别文件创建“高”和“低”子目录。

    【讨论】:

    猜你喜欢
    • 2017-05-26
    • 2015-08-24
    • 2016-08-12
    • 2014-05-17
    • 2016-07-15
    • 2012-12-06
    • 2022-01-08
    • 2015-07-11
    • 2021-01-15
    相关资源
    最近更新 更多