2017-10-12 4 views
1

에서 어휘를 추출하는 방법은 ID를의로 위의 코드는 인덱스 어휘의 목록을 인쇄합니다 다음과 같은 방법파이프 라인

fl = StopWordsRemover(inputCol="words", outputCol="filtered") 
df = fl.transform(df) 
cv = CountVectorizer(inputCol="filtered", outputCol="rawFeatures") 
model = cv.fit(df) 

print(model.vocabulary) 

에 의해 CountVecotizerModel에서 어휘를 추출 할 수 있습니다.

rm_stop_words = StopWordsRemover(inputCol="words", outputCol="filtered") 
count_freq = CountVectorizer(inputCol=rm_stop_words.getOutputCol(), outputCol="rawFeatures") 

pipeline = Pipeline(stages=[rm_stop_words, count_freq]) 
model = pipeline.fit(dfm) 
df = model.transform(dfm) 

print(model.vocabulary) # This won't work as it's not CountVectorizerModel 

는 다음과 같은 오류 파이프 라인의 모델 속성을 추출하는 방법 그래서

print(len(model.vocabulary)) 

AttributeError: 'PipelineModel' object has no attribute 'vocabulary'

가 발생합니다 :

는 이제 다음과 같은 위의 코드의 파이프 라인을 만들었습니다?

답변

1

같은 방법으로, 다른 단계의 속성으로, stages 추출 :

stages = model.stages 

은 하나를 찾을 (-s)에 관심이있어 :

from pyspark.ml.feature import CountVectorizerModel 

vectorizers = [s for s in stages if isinstance(s, CountVectorizerModel)] 

원하는 필드를 얻을 :

[v.vocabulary for v in vectorizers]