2016-11-10 12 views
1

を見つけていない、私はこのように、sklearnにパイプラインを使用したいと思います:sklearnパイプラインフィット:はAttributeError:下

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 

私が見てきましたこの記事では:

AttributeError: lower not found; using a Pipeline with a CountVectorizer in scikit-learn

が、私はバイトのリストを渡していますベクタライザ、それで問題ではありません。

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) 
+0

待って、なぜあなたは列車を作って、分割してテストしたのですか?折りたたみは、テストするのに十分でないときに通常使用されます。また、corpus.dataを折りたたんでいます。これはX_trainと50%短くなる点が異なります。 – Ale

+0

@Ale:さて、私はkfoldを誤解したと思います。私はそれがテストやトレーニングのデータをシャッフルする方法だと思っていました。 2番目の編集はエラーをスローしませんが、どうすれば今はpredict()関数を使用できますか? – user3813234

+0

ここではkfoldのインデックスを使って電車とテストデータを抽出しています。私の編集した答えを見てください。または、X_trainでkfoldとtrainを使用せず、X_testでテストしてください。 – Ale

答えて

3

あなたは正しくパイプラインを使用していません。データをベクトル化する必要はありません。考えられるのは、パイプラインがデータをベクトル化するということです。

# 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]) 
+0

あなたの返信をありがとう!コードを変更しましたが、今は新しいエラーが発生しています。もう一回見てもらえますか? – user3813234

+0

@ user3813234折り畳みはcorpus.dataとcorpus.targetに行う必要があります – Ale

関連する問題