2017-05-25 61 views
1

我有我正在读入火花数据框的文档的语料库。 我有tokeniked和矢量化的文本,现在我想喂养向量化的数据到mllib LDA模型。 LDA API文档似乎要求数据为:使用PySpark 1.6为LDA培训准备数据

rdd - 文档的RDD,它们是文档ID和词(词)计数向量的元组。术语计数向量是具有固定大小词汇表(其中词汇大小是向量的长度)的“词袋”。文件ID必须是唯一的且> = 0。

如何从我的数据框中获得合适的rdd?

from pyspark.mllib.clustering import LDA 
from pyspark.ml.feature import Tokenizer 
from pyspark.ml.feature import CountVectorizer 

#read the data 
tf = sc.wholeTextFiles("20_newsgroups/*") 

#transform into a data frame 
df = tf.toDF(schema=['file','text']) 

#tokenize 
tokenizer = Tokenizer(inputCol="text", outputCol="words") 
tokenized = tokenizer.transform(df) 

#vectorize 
cv = CountVectorizer(inputCol="words", outputCol="vectors") 
model = cv.fit(tokenized) 
result = model.transform(tokenized) 

#transform into a suitable rdd 
myrdd = ? 

#LDA 
model = LDA.train(myrdd, k=2, seed=1) 

PS:我使用Apache 1.6.3星火

+0

如果我可能会问,为什么使用MLlib的LDA? LDA提供spark-ml – eliasah

+0

只是试图跨几个教程stich片断。不反对采取不同的做法。 – ADJ

+0

然后我建议看一下spark-ml的官方文档。这很简单。正常情况下,您的价值结果已准备就绪。 – eliasah

回答

3

让我们先来组织导入,读取数据,做一些简单的特殊字符,并去除其转化为一个DataFrame

import re # needed to remove special character 
from pyspark import Row 

from pyspark.ml.feature import StopWordsRemover 
from pyspark.ml.feature import Tokenizer, CountVectorizer 
from pyspark.mllib.clustering import LDA 
from pyspark.sql import functions as F 
from pyspark.sql.types import StructType, StructField, LongType 

pattern = re.compile('[\W_]+') 

rdd = sc.wholeTextFiles("./data/20news-bydate/*/*/*") \ 
    .mapValues(lambda x: pattern.sub(' ', x)).cache() # ref. https://stackoverflow.com/a/1277047/3415409 

df = rdd.toDF(schema=['file', 'text']) 

我们需要为每个添加一个索引。下面的代码片段从这个问题的启发有关添加primary keys with Apache Spark

row_with_index = Row(*["id"] + df.columns) 

def make_row(columns): 
    def _make_row(row, uid): 
     row_dict = row.asDict() 
     return row_with_index(*[uid] + [row_dict.get(c) for c in columns]) 

    return _make_row 

f = make_row(df.columns) 

indexed = (df.rdd 
      .zipWithUniqueId() 
      .map(lambda x: f(*x)) 
      .toDF(StructType([StructField("id", LongType(), False)] + df.schema.fields))) 

一旦我们增加了指数,我们可以进行功能清洗,提取和转换:

# tokenize 
tokenizer = Tokenizer(inputCol="text", outputCol="tokens") 
tokenized = tokenizer.transform(indexed) 

# remove stop words 
remover = StopWordsRemover(inputCol="tokens", outputCol="words") 
cleaned = remover.transform(tokenized) 

# vectorize 
cv = CountVectorizer(inputCol="words", outputCol="vectors") 
count_vectorizer_model = cv.fit(cleaned) 
result = count_vectorizer_model.transform(cleaned) 

现在,让我们变换结果数据框中回RDD

corpus = result.select(F.col('id').cast("long"), 'vectors').rdd \ 
    .map(lambda x: [x[0], x[1]]) 

我们的数据现在已准备好进行培训:

# training data 
lda_model = LDA.train(rdd=corpus, k=10, seed=12, maxIterations=50) 
# extracting topics 
topics = lda_model.describeTopics(maxTermsPerTopic=10) 
# extraction vocabulary 
vocabulary = count_vectorizer_model.vocabulary 

我们可以把现在跟随打印主题描述:

for topic in range(len(topics)): 
    print("topic {} : ".format(topic)) 
    words = topics[topic][0] 
    scores = topics[topic][1] 
    [print(vocabulary[words[word]], "->", scores[word]) for word in range(len(words))] 

PS:这上面的代码与星火1.6.3进行了测试。