Doc1: ['And that was the fallacy. Once I was free to talk with staff members']
Doc2: ['In the new, stripped-down, every-job-counts business climate, these human']
Doc3 : ['Another reality makes emotional intelligence ever more crucial']
Doc4: ['The globalization of the workforce puts a particular premium on emotional']
Doc5: ['As business changes, so do the traits needed to excel. Data tracking']
これは私の語彙のサンプルです:
my_vocabulary= [‘was the fallacy’, ‘free to’, ‘stripped-down’, ‘ever more’, ‘of the workforce’, ‘the traits needed’]
重要なのは、私の語彙のすべての単語がバイグラムまたはトリグラムであるということです。私の語彙には、ドキュメントセットに含まれる可能性のあるすべてのバイグラムとトリグラムが含まれています。ここでサンプルを示しました。アプリケーションに基づいて、これは私の語彙がどうあるべきかです。私は次のようにcountVectorizerを使用しようとしています:
from sklearn.feature_extraction.text import CountVectorizer
doc_set = [Doc1, Doc2, Doc3, Doc4, Doc5]
vectorizer = CountVectorizer( vocabulary=my_vocabulary)
tf = vectorizer.fit_transform(doc_set)
私はこのようなものを手に入れることを期待しています:
print tf:
(0, 126) 1
(0, 6804) 1
(0, 5619) 1
(0, 5019) 2
(0, 5012) 1
(0, 999) 1
(0, 996) 1
(0, 4756) 4
ここで、最初の列はドキュメントID、2番目の列は語彙の単語ID、3番目の列はそのドキュメント内のその単語の出現番号です。しかし、tfは空です。一日の終わりに、語彙のすべての単語を調べて出現を計算し、マトリックスを作成するコードを書くことができますが、私が持っているこの入力にcountVectorizerを使用して時間を節約できますか?私はここで何か間違ったことをしていますか?countVectorizerがそれを行う正しい方法ではない場合は、任意の推奨事項をいただければ幸いです。
CountVectorizerでngram_rangeパラメーターを指定することにより、可能なすべてのバイグラムとトライグラムの語彙を構築できます。fit_tranformの後、get_feature_names()メソッドとtoarray()メソッドを使用して語彙と頻度を表示できます。後者は、各ドキュメントの頻度マトリックスを返します。詳細情報:http://scikit-learn.org/stable/modules/feature_extraction.html#text-feature-extraction
from sklearn.feature_extraction.text import CountVectorizer
Doc1 = 'And that was the fallacy. Once I was free to talk with staff members'
Doc2 = 'In the new, stripped-down, every-job-counts business climate, these human'
Doc3 = 'Another reality makes emotional intelligence ever more crucial'
Doc4 = 'The globalization of the workforce puts a particular premium on emotional'
Doc5 = 'As business changes, so do the traits needed to excel. Data tracking'
doc_set = [Doc1, Doc2, Doc3, Doc4, Doc5]
vectorizer = CountVectorizer(ngram_range=(2, 3))
tf = vectorizer.fit_transform(doc_set)
vectorizer.vocabulary_
vectorizer.get_feature_names()
tf.toarray()
あなたがやろうとしたことに関しては、語彙でCountVectorizerをトレーニングしてから、ドキュメントを変換すればうまくいくでしょう。
my_vocabulary= ['was the fallacy', 'more crucial', 'particular premium', 'to excel', 'data tracking', 'another reality']
vectorizer = CountVectorizer(ngram_range=(2, 3))
vectorizer.fit_transform(my_vocabulary)
tf = vectorizer.transform(doc_set)
vectorizer.vocabulary_
Out[26]:
{'another reality': 0,
'data tracking': 1,
'more crucial': 2,
'particular premium': 3,
'the fallacy': 4,
'to excel': 5,
'was the': 6,
'was the fallacy': 7}
tf.toarray()
Out[25]:
array([[0, 0, 0, 0, 1, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0]], dtype=int64)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加