2017-09-12 5 views
-2

모든 문서에서 각 토큰에 대해 tf * idf를 계산하고 각 문서에 대해 벡터를 생성했습니다 (n 차원, n은 각각 코퍼스의 고유 단어 수입니다). sklearn.cluster.MeanShift를 사용하여 벡터에서 클러스터를 작성하는 방법을 파악할 수 없습니다.Mean Shift를 사용한 문서 클러스터링

+0

tfidf를 계산 한 후 숫자 값의 행렬 (예 : 행 및 열이있는 데이터 테이블)이 있습니까? 희박하거나 밀도가 있습니까? 일반적으로 어떤 유형입니까? sklearn의 TfidfVectorizer()를 사용 했습니까? – Jarad

+0

예, 저는 TfidfVectorizer()를 사용하여 희소 행렬로 끝났습니다. sklearn.clister.MeanShift에 입력으로 제공하는 방법을 이해하지 못합니다. –

답변

1

TfidfVectorizer는 문서를 숫자의 "희소 매트릭스"로 변환합니다. MeanShift는 데이터를 "밀집"으로 전달해야합니다. 아래 파이프 라인 (credit)에서 변환하는 방법을 보여 주지만, 메모리가 허용되는 경우 희박한 행렬을 toarray() 또는 todense()으로 밀도를 변환 할 수 있습니다.

from sklearn.feature_extraction.text import TfidfVectorizer 
from sklearn.cluster import MeanShift 
from sklearn.pipeline import Pipeline 
from sklearn.preprocessing import FunctionTransformer 

documents = ['this is document one', 
      'this is document two', 
      'document one is fun', 
      'document two is mean', 
      'document is really short', 
      'how fun is document one?', 
      'mean shift... what is that'] 

pipeline = Pipeline(
    steps=[ 
    ('tfidf', TfidfVectorizer()), 
    ('trans', FunctionTransformer(lambda x: x.todense(), accept_sparse=True)), 
    ('clust', MeanShift()) 
    ]) 

pipeline.fit(documents) 
pipeline.named_steps['clust'].labels_ 

result = [(label,doc) for doc,label in zip(documents, pipeline.named_steps['clust'].labels_)] 

for label,doc in sorted(result): 
    print(label, doc) 

인쇄 :

0 document two is mean 
0 this is document one 
0 this is document two 
1 document one is fun 
1 how fun is document one? 
2 mean shift... what is that 
3 document is really short 

당신은 "하이퍼 파라미터"를 수정할 수 있지만 당신에게 내가 생각하는 일반적인 아이디어를 제공합니다.

관련 문제