sklearn 管道拟合:AttributeError:未找到较低值

2024-01-07

我想在 sklearn 中使用管道,如下所示:

corpus = load_files('corpus/train')

stop_words = [x for x in open('stopwords.txt', 'r').read().split('\n')]  # 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

我看过这个帖子:

属性错误:未找到下层;在 scikit-learn 中使用带有 CountVectorizer 的 Pipeline https://stackoverflow.com/questions/33605946/attributeerror-lower-not-found-using-a-pipeline-with-a-countvectorizer-in-scik

但我将字节列表传递给矢量化器,所以这不应该是问题。

EDIT

corpus = load_files('corpus')

stop_words = [x for x in open('stopwords.txt', 'r').read().split('\n')]

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

2ND EDIT

corpus = load_files('corpus')

stop_words = [y for x in open('stopwords.txt', 'r').read().split('\n') 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])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

sklearn 管道拟合:AttributeError:未找到较低值 的相关文章

随机推荐