【问题标题】:Python Sklearn Pipeline with array带有数组的 Python Sklearn 管道
【发布时间】:2018-12-15 11:23:00
【问题描述】:

我正在尝试使用 Python 和 Sklearn 创建分类器。我目前已成功导入所有数据。我一直在尝试遵循here 的教程,并在进行过程中对其进行了一些更改。后来进入该项目,我意识到他们的训练和测试数据与我的大不相同。如果我理解正确,他们有这样的事情:

X_train = ['Article or News article here', 'Anther News Article or Article here', ...]
y_train = ['Article Type', 'Article Type', ...]
#Same for the X_test and y_test

虽然我有这样的事情:

X_train = [['Dylan went in the house. Robert left the house', 'Where is Dylan?'], ['Mary ate the apple. Tom ate the cake', 'Who ate the cake?'], ...]
y_train = ['In the house.', 'Tom ate the cake']
#Same for the X_test and y_test

当我尝试使用管道训练分类器时:

text_clf = Pipeline([('vect', CountVectorizer(stop_words='english')),
     ('tfidf', TfidfTransformer(use_idf=True)),
     ('clf', SGDClassifier(loss='hinge', penalty='l2', alpha=1e-3, random_state=42, 
     verbose=1)),])

我得到错误:

AttributeError: 'list' object has no attribute 'lower'

在这一行:

text_clf.fit(X_train, y_train)

经过研究,我现在知道这是因为我正在为我的 X_train 数据输入一个数组,而不是一个字符串。 所以我的问题是,我如何构建一个管道来接受我的X_train 数据的数组和我的y_train 数据的字符串?这可能与管道有关吗?

【问题讨论】:

    标签: python scikit-learn pipeline


    【解决方案1】:

    您可以使用tokenizer 属性将CountVectorizer 告知每个列表作为单个文档,并将lowercase 选项转换为False 像这样

    text_clf = Pipeline([('vect', CountVectorizer(tokenizer=lambda single_doc: single_doc,stop_words='english',lowercase=False)),
     ('tfidf', TfidfTransformer(use_idf=True)),
     ('clf', SGDClassifier(loss='hinge', penalty='l2', alpha=1e-3, random_state=42, 
     verbose=1)),])
    

    【讨论】:

      猜你喜欢
      • 2018-01-10
      • 2017-03-05
      • 2018-12-11
      • 1970-01-01
      • 2017-03-21
      • 2019-06-29
      • 2020-09-10
      • 2021-01-05
      • 2015-08-15
      相关资源
      最近更新 更多