sklearn pipeline fit: AttributeError: lower not found(SkLearning管道匹配:属性错误:找不到下部)
本文介绍了SkLearning管道匹配:属性错误:找不到下部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在skLearning中使用管道,如下所示:
corpus = load_files('corpus/train')
stop_words = [x for x in open('stopwords.txt', 'r').read().split('
')] # Uppercase!
countvec = CountVectorizer(stop_words=stop_words, ngram_range=(1, 2))
X_train, X_test, y_train, y_test = train_test_split(corpus.data, corpus.target, test_size=0.9,
random_state=0)
x_train_counts = countvec.fit_transform(X_train)
x_test_counts = countvec.transform(X_test)
k_fold = KFold(n=len(corpus.data), n_folds=6)
confusion = np.array([[0, 0], [0, 0]])
pipeline = Pipeline([
('vectorizer', CountVectorizer(stop_words=stop_words, ngram_range=(1, 2))),
('classifier', MultinomialNB()) ])
for train_indices, test_indices in k_fold:
pipeline.fit(x_train_counts, y_train)
predictions = pipeline.predict(x_test_counts)
但是,我收到此错误:
AttributeError: lower not found
我看过这个帖子:
AttributeError: lower not found; using a Pipeline with a CountVectorizer in scikit-learn
但我将字节列表传递给向量器,所以这不应该是问题所在。
编辑
corpus = load_files('corpus')
stop_words = [x for x in open('stopwords.txt', 'r').read().split('
')]
X_train, X_test, y_train, y_test = train_test_split(corpus.data, corpus.target, test_size=0.5,
random_state=0)
k_fold = KFold(n=len(corpus.data), n_folds=6)
confusion = np.array([[0, 0], [0, 0]])
pipeline = Pipeline([
('vectorizer', CountVectorizer(stop_words=stop_words, ngram_range=(1, 2))),
('classifier', MultinomialNB())])
for train_indices, test_indices in k_fold:
pipeline.fit(X_train[train_indices], y_train[train_indices])
predictions = pipeline.predict(X_test[test_indices])
现在我收到错误:
TypeError: only integer arrays with one element can be converted to an index
第二次编辑
corpus = load_files('corpus')
stop_words = [y for x in open('stopwords.txt', 'r').read().split('
') for y in (x, x.title())]
k_fold = KFold(n=len(corpus.data), n_folds=6)
confusion = np.array([[0, 0], [0, 0]])
pipeline = Pipeline([
('vectorizer', CountVectorizer(stop_words=stop_words, ngram_range=(1, 2))),
('classifier', MultinomialNB())])
for train_indices, test_indices in k_fold:
pipeline.fit(corpus.data, corpus.target)
推荐答案
您没有正确使用管道。您不需要将数据矢量化,其想法是管道将数据矢量化。
# This is done by the pipeline
# x_train_counts = countvec.fit_transform(X_train)
# x_test_counts = countvec.transform(X_test)
k_fold = KFold(n=len(corpus.data), n_folds=6)
confusion = np.array([[0, 0], [0, 0]])
pipeline = Pipeline([
('vectorizer', CountVectorizer(stop_words=stop_words, ngram_range=(1, 2))),
('classifier', MultinomialNB()) ])
# also you are not using the indices...
for train_indices, test_indices in k_fold:
pipeline.fit(corpus.data[train_indices], corpus.target[train_indices])
predictions = pipeline.predict(corpus.data[test_indices])
这篇关于SkLearning管道匹配:属性错误:找不到下部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:SkLearning管道匹配:属性错误:找不到下部


猜你喜欢
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01