2017-05-20 10 views
1

私はテキスト処理のためにscikit-learnを使用していますが、私のCountVectorizerは期待した出力を得ていません。パンダのデータフレームを持つCountVectorizer

私のCSVファイルには、次のようになりますように

"Text";"label" 
"Here is sentence 1";"label1" 
"I am sentence two";"label2" 

と。

だから、私はPythonのSVMの仕組みを理解するために、まずBag of Wordsを使いたいと思う。

import pandas as pd 
from sklearn import svm 
from sklearn.feature_extraction.text import CountVectorizer 

data = pd.read_csv(open('myfile.csv'),sep=';') 

target = data["label"] 
del data["label"] 

# Creating Bag of Words 
count_vect = CountVectorizer() 
X_train_counts = count_vect.fit_transform(data) 
X_train_counts.shape 
count_vect.vocabulary_.get(u'algorithm') 

そして、私は

print(X_train_counts.shape) 

を行うとき、私は文章で1048行を持っている間、私は、出力(1,1)を参照してください。私は出力の見直しよりも

count_vect.vocabulary_.get(u'algorithm') 

そしてこれはNoneです。

私が間違っていることを教えてもらえますか?私はthisチュートリアルに従っています。

+2

なぜ、 'count_vect.vocabulary_.get(u'algorithm ')'が 'None'にならないのですか?この用語は、あなたの例ではどこにも定義されていません。 –

+0

@aryamccarthy ok。それは理にかなっています。しかし、その形は何ですか? – ZverArt

答えて

4

問題はcount_vect.fit_transform(data)です。この関数は、文字列を生成するイテラブルが必要です。残念ながら、これらは間違った文字列であり、簡単な例で検証できます。

for x in data: 
    print(x) 
# Text 

列名のみが印刷されます。反復は、data['Text']の値の代わりに列を与えます。

X_train_counts = count_vect.fit_transform(data.Text) 
X_train_counts.shape 
# (2, 5) 
count_vect.vocabulary_ 
# {'am': 0, 'here': 1, 'is': 2, 'sentence': 3, 'two': 4} 
関連する問題