2016-06-17 2 views
1

이 부분은 post을 참조하십시오. 나는 우리가 어휘의 어휘를 CountVectorizer 모델에 어떻게 제공하는지 궁금하다. distributed systems 또는 machine learning? 우리는 모델 여기scikit-learn에 공간이있는 단어로 단어 제공 CountVectorizer

from sklearn.feature_extraction.text import CountVectorizer 
vec = CountVectorizer(vocabulary=vocabulary) 
print(vec.fit_transform(tags).toarray()) 

이 어휘 목록을 제공 할 수

import numpy as np 
from itertools import chain 

tags = [ 
    "python, tools", 
    "linux, tools, ubuntu", 
    "distributed systems, linux, networking, tools", 
] 

vocabulary = list(map(lambda x: x.split(', '), tags)) 
vocabulary = list(np.unique(list(chain(*vocabulary)))) 

, 나는 단어 distributed systems (첫 번째 열)의 수를 잃은 예를 들면 다음과 같습니다. 결과는 다음과 같습니다.

[[0 0 0 1 1 0] 
[0 1 0 0 1 1] 
[0 1 1 0 1 0]] 

token_pattern 또는 다른 곳으로 변경해야합니까?

답변

2

본질적으로 이미 분석 할 어휘를 미리 정의했으며 ','로 태그를 토큰 화하려고합니다. 제공

from sklearn.feature_extraction.text import CountVectorizer 
vec = CountVectorizer(vocabulary=vocabulary, tokenizer=lambda x: x.split(', ')) 
print(vec.fit_transform(tags).toarray()) 

, :

[[0 0 0 1 1 0] 
[0 1 0 0 1 1] 
[1 1 1 0 1 0]] 
+0

정말 고마워요 @Zichen을, 이것은 내가 무엇을 찾고있다

다음과 같은 방법으로 그렇게 할 수 CountVectorizer을 속일 수 있습니다. 'tokenizer '를 사용하여 문제를 매우 편리하게 만든다. – titipata

관련 문제