首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用数组的Python Sklearn管道

使用数组的Python Sklearn管道
EN

Stack Overflow用户
提问于 2018-07-07 04:43:49
回答 1查看 467关注 0票数 1

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

代码语言:javascript
复制
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

当我有这样的东西的时候:

代码语言:javascript
复制
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

当我尝试用下面的管道训练分类器时:

代码语言:javascript
复制
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)),])

我得到了错误:

代码语言:javascript
复制
AttributeError: 'list' object has no attribute 'lower'

在这一行中:

代码语言:javascript
复制
text_clf.fit(X_train, y_train)

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-07 05:40:23

您可以使用tokenizer属性将每个列表的CountVectorizer作为单个文档,并将lowercase选项设置为False,如下所示

代码语言:javascript
复制
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)),])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51217367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档