2017-02-27 17 views
1

というtfidfの行列は何ですか?私はこのように見えるtfidf行列を返した文書の集合に対してtfidfを実行しました。理想的には

(1, 12) 0.656240233446 
    (1, 11) 0.754552023393 
    (2, 6) 1.0 
    (3, 13) 1.0 
    (4, 2) 1.0 
    (7, 9) 1.0 
    (9, 4) 0.742540927053 
    (9, 5) 0.66980069547 
    (11, 19) 0.735138466738 
    (11, 7) 0.677916982176 
    (12, 18) 1.0 
    (13, 14) 0.697455191865 
    (13, 11) 0.716628394177 
    (14, 5) 1.0 
    (15, 8) 1.0 
    (16, 17) 1.0 
    (18, 1) 1.0 
    (19, 17) 1.0 
    (22, 13) 1.0 
    (23, 3) 1.0 
    (25, 6) 1.0 
    (26, 19) 0.476648253537 
    (26, 7) 0.879094103268 
    (28, 10) 0.532672175403 
    (28, 7) 0.523456282204 

これが何であるか知りたいのですが、これがどのように提供されているのか理解できません。 私がデバッグモードに入っていたとき、私はindices、indptr、dataについて知りました。これは何? 数字に混乱があります。私が括弧内の最初の要素が私の予測に基づく文書であると言うと、0番目、5番目、6番目の文書は表示されません。 ここでどのように働いているか把握してください。しかし、私はwikiのtfidfの一般的な作業を知っており、逆の文書やその他のものをログに記録しています。私はちょうどここでこれらの3つの異なる種類の数字が何であるかを知りたいのですが、それを参照するのは何ですか?

ソースコードは次のとおりTFIDFの

#This contains the list of file names 
_filenames =[] 
#This conatains the list if contents/text in the file 
_contents = [] 
#This is a dict of filename:content 
_file_contents = {} 
class KmeansClustering(): 
    def kmeansClusters(self): 
     global _report 
      self.num_clusters = 5 
      km = KMeans(n_clusters=self.num_clusters) 
      vocab_frame = TokenizingAndPanda().createPandaVocabFrame() 
      self.tfidf_matrix, self.terms, self.dist = TfidfProcessing().getTfidFPropertyData() 
      km.fit(self.tfidf_matrix) 
      self.clusters = km.labels_.tolist() 
      joblib.dump(km, 'doc_cluster2.pkl') 
      km = joblib.load('doc_cluster2.pkl') 

class TokenizingAndPanda(): 

    def tokenize_only(self,text): 
     ''' 
     This function tokenizes the text 
     :param text: Give the text that you want to tokenize 
     :return: it gives the filter tokes 
     ''' 
     # first tokenize by sentence, then by word to ensure that punctuation is caught as it's own token 
     tokens = [word.lower() for sent in nltk.sent_tokenize(text) for word in nltk.word_tokenize(sent)] 
     filtered_tokens = [] 
     # filter out any tokens not containing letters (e.g., numeric tokens, raw punctuation) 
     for token in tokens: 
      if re.search('[a-zA-Z]', token): 
       filtered_tokens.append(token) 
     return filtered_tokens 

    def tokenize_and_stem(self,text): 
     # first tokenize by sentence, then by word to ensure that punctuation is caught as it's own token 
     tokens = [word.lower() for sent in nltk.sent_tokenize(text) for word in nltk.word_tokenize(sent)] 
     filtered_tokens = [] 
     # filter out any tokens not containing letters (e.g., numeric tokens, raw punctuation) 
     for token in tokens: 
      if re.search('[a-zA-Z]', token): 
       filtered_tokens.append(token) 
     stems = [_stemmer.stem(t) for t in filtered_tokens] 
     return stems 

    def getFilnames(self): 
     ''' 

     :return: 
     ''' 
     global _path 
     global _filenames 
     path = _path 
     _filenames = FileAccess().read_all_file_names(path) 


    def getContentsForFilenames(self): 
     global _contents 
     global _file_contents 
     for filename in _filenames: 
      content = FileAccess().read_the_contents_from_files(_path, filename) 
      _contents.append(content) 
      _file_contents[filename] = content 

    def createPandaVocabFrame(self): 
     global _totalvocab_stemmed 
     global _totalvocab_tokenized 
     #Enable this if you want to load the filenames and contents from a file structure. 
     # self.getFilnames() 
     # self.getContentsForFilenames() 

     # for name, i in _file_contents.items(): 
     #  print(name) 
     #  print(i) 
     for i in _contents: 
      allwords_stemmed = self.tokenize_and_stem(i) 
      _totalvocab_stemmed.extend(allwords_stemmed) 

      allwords_tokenized = self.tokenize_only(i) 
      _totalvocab_tokenized.extend(allwords_tokenized) 
     vocab_frame = pd.DataFrame({'words': _totalvocab_tokenized}, index=_totalvocab_stemmed) 
     print(vocab_frame) 
     return vocab_frame 


class TfidfProcessing(): 

    def getTfidFPropertyData(self): 
     tfidf_vectorizer = TfidfVectorizer(max_df=0.4, max_features=200000, 
              min_df=0.02, stop_words='english', 
              use_idf=True, tokenizer=TokenizingAndPanda().tokenize_and_stem, ngram_range=(1, 1)) 
     # print(_contents) 
     tfidf_matrix = tfidf_vectorizer.fit_transform(_contents) 
     terms = tfidf_vectorizer.get_feature_names() 
     dist = 1 - cosine_similarity(tfidf_matrix) 

     return tfidf_matrix, terms, dist 
+0

あなたは[scikit-learn tf-idf](http://scikit-learn.org/stable /modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html)? あなたのコードの一部と情報を抽出したい文書からの抜粋を入れることができますか? – LoicM

+0

yaそのscikit-tf-idfは、私のコードの一部が上に掲載されていることを話していたものです。 –

答えて

1

結果がデータに適用される通常A_ijはi番目の文書に記載されている正規化されたj番目の用語(単語)周波数である2D行列Aです。 (持っているいくつかの語彙に応じ

(1, 12) 0.656240233446 

はその第12回単語を意味しますので、プリントアウトされている非ゼロである要素のみ、 - あなたはあなたの出力に表示すると、他の言葉で、この行列のスパース表現ですsklearnによって構築された)は最初の文書で正規化された周波数0.656240233446を持っています。 「欠落」ビットはゼロであり、例えば、第1の文書(第(1,3)は存在しないので)において第3ワードが見つからないということを意味する。

いくつかのドキュメントが欠落しているという事実は、(あなたが含まなかった)特定のコード/データの結果です。おそらくあなたは手で語彙を設定しますか?または、考慮するフィーチャの最大数? TfidfVectorizerにはそれを引き起こす多くのパラメータがありますが、あなたの正確なコード(およびいくつかの例示的なデータ)がないと、他に何も言えません。たとえば、min_dfを設定すると、それは(まれな言葉をドロップすると)同様にmax_features(同じ効果)

+0

こんにちは、@lejlotの説明に感謝し、上記の私のソースコードの一部も掲載しました。私がmin_df、max_df、max_featuresに関してガイドするのを助けてくれることを願っています。どのようにして、クラスタリングの精度を上げることができ、なぜいくつかの文書が欠けているような仕組みです。 –

+0

基本的にmax_df、min_df、max_featuresを削除してください。すべてのドキュメントを取得する必要があります。このようなことを設定する "魔法の"方法はありません。試しても何らかの方法で試してみるだけで、設定することなく始めることができます。 – lejlot

+0

確かに私はその方法を試して、それがどうなるかを教えてくれるでしょう...ありがとうございます –

関連する問題