Programing

scikit CountVectorizer에서 min_df 및 max_df 이해

lottogame 2020. 12. 10. 08:27
반응형

scikit CountVectorizer에서 min_df 및 max_df 이해


CountVectorizer에 입력 한 5 개의 텍스트 파일이 있습니다. CountVectorizer 인스턴스에 min_df 및 max_df를 지정할 때 최소 / 최대 문서 빈도는 정확히 무엇을 의미합니까? 특정 텍스트 파일에있는 단어의 빈도입니까, 아니면 전체 전체 말뭉치 (5 개의 txt 파일)에서 단어의 빈도입니까?

min_df 및 max_df가 정수 또는 부동 소수점으로 제공되는 경우 어떻게 다릅니 까?

설명서는 철저한 설명을 제공하지 않으며 min_df 및 / 또는 max_df의 사용을 보여주는 예제도 제공하지 않습니다. 누군가가 min_df 또는 max_df를 보여주는 설명이나 예를 제공 할 수 있습니까?


max_df너무 자주 나타나는 용어를 제거하는 데 사용 되며 "말뭉치 특정 불용어"라고도합니다. 예를 들면 :

  • max_df = 0.50" 문서의 50 % 이상에 나타나는 용어 ​​무시"를 의미 합니다.
  • max_df = 25" 25 개 이상의 문서에 나타나는 용어 ​​무시"를 의미 합니다.

기본값 max_df1.0" 문서의 100 % 이상에 나타나는 용어 ​​무시"를 의미 합니다. 따라서 기본 설정은 용어를 무시하지 않습니다.


min_df너무 드물게 나타나는 용어를 제거하는 데 사용됩니다 . 예를 들면 :

  • min_df = 0.01" 문서의 1 % 미만에 나타나는 용어 ​​무시"를 의미 합니다.
  • min_df = 5" 5 개 미만의 문서에 나타나는 용어 ​​무시"를 의미 합니다.

기본값 min_df1" 문서가 1 개 미만인 용어 무시"를 의미 합니다. 따라서 기본 설정은 용어를 무시하지 않습니다.


여기에 있는 CountVectorizer문서 에 따라 .

범위의 플로트를 사용하는 경우 [0.0, 1.0]그것들은 참고 문서 빈도. 이 용어가 포함 된 문서의 비율입니다.

int를 사용할 때이 용어를 보유하는 절대 문서 수를 나타냅니다.

5 개의 텍스트 파일 (또는 문서)이있는 예를 고려하십시오. 설정 max_df = 0.6하면 0.6*5=3문서로 번역됩니다 . 설정하면 max_df = 2간단히 2 개의 문서로 번역됩니다.

소스 코드의 예는 다음과 Github에서에서 복사 여기 가 어떻게 보여 max_doc_count으로부터 구성된다 max_df. 의 코드 min_df는 유사하며 GH 페이지에서 찾을 수 있습니다.

max_doc_count = (max_df
                 if isinstance(max_df, numbers.Integral)
                 else max_df * n_doc)

min_df의 기본값은 max_df각각 1과 1.0입니다. 기본적으로 "내 용어가 한 문서에서만 발견되면 무시됩니다. 마찬가지로 모든 문서 (100 % 또는 1.0)에서 발견되면 무시됩니다."

max_df그리고 min_df모두 계산하기 위해 내부적으로 사용 max_doc_count하고 min_doc_count, 용어가 발견되어야한다는 문서의 최대 및 최소 수입니다.이 다음에 전달되는 self._limit_features키워드 인수로 high하고 low, 각각에 대한 문서화 문자열 self._limit_featuresIS를

"""Remove too rare or too common features.

Prune features that are non zero in more samples than high or less
documents than low, modifying the vocabulary, and restricting it to
at most the limit most frequent.

This does not prune samples with zero features.
"""

min_df 및 max_df의 기본값은 각각 1과 1.0입니다. 이러한 기본값은 실제로 아무 작업도 수행하지 않습니다.

즉, @Ffisegydd 답변이 현재 받아 들여지는 답변이 정확하지 않다고 생각합니다.

예를 들어, 기본값을 사용하여 실행하여 언제 min_df=1을 확인한 max_df=1.0다음

1) 하나 이상의 문서에 나타나는 모든 토큰이 사용됩니다 (예 : 모든 토큰!).

2) 모든 문서에 나타나는 모든 토큰이 사용됩니다 (모든 곳에서 하나의 후보로 테스트합니다).

cv = CountVectorizer(min_df=1, max_df=1.0, lowercase=True) 
# here is just a simple list of 3 documents.
corpus = ['one two three everywhere', 'four five six everywhere', 'seven eight nine everywhere']
# below we call fit_transform on the corpus and get the feature names.
X = cv.fit_transform(corpus)
vocab = cv.get_feature_names()
print vocab
print X.toarray()
print cv.stop_words_

우리는 :

[u'eight', u'everywhere', u'five', u'four', u'nine', u'one', u'seven', u'six', u'three', u'two']
[[0 1 0 0 0 1 0 0 1 1]
 [0 1 1 1 0 0 0 1 0 0]
 [1 1 0 0 1 0 1 0 0 0]]
set([])

모든 토큰이 유지됩니다. 불용어가 없습니다.

인수를 더 엉망으로 만들면 다른 구성이 명확 해집니다.

재미와 통찰력을 위해 stop_words = 'english', 특이하게도 'seven'을 제외한 모든 단어가 제거 된 것을 가지고 놀면서 보는 것도 추천합니다 ! '모든 곳'을 포함합니다.


I would add this point also for understanding min_df and max_df in tf-idf better.

If you go with the default values, meaning considering all terms, you have generated definitely more tokens. So your clustering process (or any other thing you want to do with those terms later) will take a longer time.

BUT the quality of your clustering should NOT be reduced.

One might think that allowing all terms (e.g. too frequent terms or stop-words) to be present might lower the quality but in tf-idf it doesn't. Because tf-idf measurement instinctively will give a low score to those terms, effectively making them not influential (as they appear in many documents).

So to sum it up, pruning the terms via min_df and max_df is to improve the performance, not the quality of clusters (as an example).

And the crucial point is that if you set the min and max mistakenly, you would lose some important terms and thus lower the quality. So if you are unsure about the right threshold (it depends on your documents set), or if you are sure about your machine's processing capabilities, leave the min, max parameters unchanged.

참고URL : https://stackoverflow.com/questions/27697766/understanding-min-df-and-max-df-in-scikit-countvectorizer

반응형