我正在尝试创建一个使用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
数据的字符串?这可能与管道有关吗?
发布于 2018-07-07 05:40:23
您可以使用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)),])
https://stackoverflow.com/questions/51217367
复制相似问题