标签: topic-modeling

主题建模:如何使用我的拟合LDA模型来预测R中新数据集的新主题?

我在R中使用'lda'包进行主题建模.我想使用针对新数据集的拟合Latent Dirichlet分配(LDA)模型来预测新主题(文档中相关单词的集合).在这个过程中,我遇到了predictive.distribution()函数.但该函数将document_sums作为输入参数,它是拟合新模型后的结果输出.我需要帮助来理解在新数据集上使用现有模型并预测主题.以下是Johnathan Chang为该软件包编写的文档中的示例代码:以下是代码:

#Fit a model
data(cora.documents)
data(cora.vocab)

K <- 10 ## Num clusters

result <- lda.collapsed.gibbs.sampler(cora.documents,K, cora.vocab,25, 0.1, 0.1) 

# Predict new words for the first two documents
predictions <-  predictive.distribution(result$document_sums[,1:2], result$topics, 0.1, 0.1)

# Use top.topic.words to show the top 5 predictions in each document.
top.topic.words(t(predictions), 5)
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激

感谢和问候,

ANKIT

r lda topic-modeling

5
推荐指数
1
解决办法
4727
查看次数

从R中的文档语料库中删除"空"字符项?

我正在使用R中的包tmlda主题模型新闻文章的语料库.但是,我得到一个"非角色"问题,因为""这会弄乱我的主题.这是我的工作流程:

text <- Corpus(VectorSource(d$text))
newtext <- lapply(text, tolower)
sw <- c(stopwords("english"), "ahram", "online", "egypt", "egypts", "egyptian")
newtext <- lapply(newtext, function(x) removePunctuation(x))
newtext <- lapply(newtext, function(x) removeWords(x, sw))
newtext <- lapply(newtext, function(x) removeNumbers(x))
newtext <- lapply(newtext, function(x) stripWhitespace(x))
d$processed <- unlist(newtext)
corpus <- lexicalize(d$processed)
k <- 40
result <-lda.collapsed.gibbs.sampler(corpus$documents, k, corpus$vocab, 500, .02, .05,
compute.log.likelihood = TRUE, trace = 2L)
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我训练lda模型时,一切看起来都很棒,除了最常出现的单词是"".我尝试通过从下面给出的词汇中删除它并如上所述重新估计模型来解决这个问题:

newtext <- lapply(newtext, function(x) removeWords(x, ""))
Run Code Online (Sandbox Code Playgroud)

但是,它仍然存在,如下所示:

str_split(newtext[[1]], " ")

[[1]]
 [1] ""              "body" …
Run Code Online (Sandbox Code Playgroud)

r text-analysis text-mining lda topic-modeling

5
推荐指数
1
解决办法
6303
查看次数

用Python实现主题模型(numpy)

最近,我使用numpy在Python上实现了针对LDA主题模型的Gibbs采样,并将来自站点的一些代码作为参考.在Gibbs采样的每次迭代中,我们删除一个(当前)单词,根据从LDA模型推断的后验条件概率分布对该单词的新主题进行采样,并更新单词 - 主题计数,如下所示:

for m, doc in enumerate(docs): #m: doc id
  for n, t in enumerate(doc): #n: id of word inside document, t: id of the word globally
    # discount counts for word t with associated topic z
    z = z_m_n[m][n]
    n_m_z[m][z] -= 1
    n_z_t[z, t] -= 1 
    n_z[z] -= 1
    n_m[m] -= 1

    # sample new topic for multinomial                
    p_z_left = (n_z_t[:, t] + beta) / (n_z + V * beta)
    p_z_right = (n_m_z[m] + alpha) / ( n_m[m] …
Run Code Online (Sandbox Code Playgroud)

python numpy machine-learning lda topic-modeling

5
推荐指数
1
解决办法
2401
查看次数

潜在Dirichlet分配解决方案示例

我想了解Latent Dirichlet Allocation(LDA).我有机器学习和概率理论的基本知识,并基于这篇博客文章http://goo.gl/ccPvE我能够发展LDA背后的直觉.但是,我仍然没有完全理解其中的各种计算.我想知道有人可以使用一个非常小的语料库向我展示计算(比如说3-5个句子和2-3个主题).

lda topic-modeling

5
推荐指数
1
解决办法
4673
查看次数

在mahout 0.8中运行cvb

目前的Mahout 0.8-SNAPSHOT包括用于主题建模的折叠变分贝叶斯(cvb)版本,并删除了潜在Dirichlet分析(lda)方法,因为cvb可以更好地并行化.不幸的是,只有关于如何运行示例和生成有意义输出的lda文档.

因此,我想:

  • 正确处理一些文本
  • 运行cvb的cvb0_local版本
  • 通过查看每个生成主题中的前n个单词来检查结果

mahout lda topic-modeling

5
推荐指数
1
解决办法
4302
查看次数

如何使用gensim使用训练有素的LDA模型预测新查询的主题?

我使用gensim训练了一个用于LDA主题建模的语料库.

浏览gensim网站上的教程(这不是整个代码):

question = 'Changelog generation from Github issues?';

temp = question.lower()
for i in range(len(punctuation_string)):
    temp = temp.replace(punctuation_string[i], '')

words = re.findall(r'\w+', temp, flags = re.UNICODE | re.LOCALE)
important_words = []
important_words = filter(lambda x: x not in stoplist, words)
print important_words
dictionary = corpora.Dictionary.load('questions.dict')
ques_vec = []
ques_vec = dictionary.doc2bow(important_words)
print dictionary
print ques_vec
print lda[ques_vec]
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出:

['changelog', 'generation', 'github', 'issues']
Dictionary(15791 unique tokens)
[(514, 1), (3625, 1), (3626, 1), (3627, 1)]
[(4, 0.20400000000000032), (11, 0.20400000000000032), (19, 0.20263215848547525), …
Run Code Online (Sandbox Code Playgroud)

python nlp lda gensim topic-modeling

5
推荐指数
1
解决办法
1万
查看次数

Gensim字典实现

我只是对gensim字典的实现感到好奇.我有以下代码:

    def build_dictionary(documents):
        dictionary = corpora.Dictionary(documents)
        dictionary.save('/tmp/deerwester.dict') # store the dictionary
        return dictionary    
Run Code Online (Sandbox Code Playgroud)

我查看了文件deerwester.dict,它看起来像这样:

8002 6367 656e 7369 6d2e 636f 7270 6f72
612e 6469 6374 696f 6e61 7279 0a44 6963
7469 6f6e 6172 790a 7101 2981 7102 7d71
0328 5508 6e75 6d5f 646f 6373 7104 4b09
5508 ...
Run Code Online (Sandbox Code Playgroud)

但是,以下代码

my_dict = dictionary.load('/tmp/deerwester.dict') 
print my_dict.token2id #view dictionary
Run Code Online (Sandbox Code Playgroud)

得出这个:

{'minors': 30, 'generation': 22, 'testing': 16, 'iv': 29, 'engineering': 15, 'computer': 2, 'relation': 20, 'human': 3, 'measurement': 18, 'unordered': 25, 'binary': 21, 'abc': …
Run Code Online (Sandbox Code Playgroud)

python nlp gensim topic-modeling

5
推荐指数
1
解决办法
7916
查看次数

MALLET中主题模型的增量训练

根据MALLET 文档,可以逐步训练主题模型:

“ -output-model [FILENAME]此选项指定一个文件,用于写入序列化的MALLET主题培训者对象。这种类型的输出适合于暂停和重新开始培训”

我想在一组数据上训练主题,然后在另一组数据上增加模型。在两个训练步骤之后,我想输出两个数据集的状态(使用--output-state)。这是我尝试执行的操作:

# training on the first dataset
../mallet-2.0.7/bin/mallet import-dir --input input/ --keep-sequence --output input.mallet
../mallet-2.0.7/bin/mallet train-topics --input  input.mallet --num-topics 3 --output-state topic-state.gz --output-model model

# training on the second dataset
../mallet-2.0.7/bin/mallet import-dir --input input2/ --keep-sequence --output input2.mallet  --use-pipe-from input.mallet
../mallet-2.0.7/bin/mallet train-topics --input  input2.mallet --num-topics 3  --num-iterations 100 --output-state topic-state2.gz --input-model model
Run Code Online (Sandbox Code Playgroud)

在上一个命令中,如果我添加“ --input-model model”,则第二个数据集中的数据不会出现在输出状态文件中。如果我不添加它,则第一状态数据集中的数据不会出现在输出状态文件中。

如果我尝试在代码中向模型添加其他实例:

model.addInstances(instances);
model.setNumThreads(2);
model.setNumIterations(50);
model.estimate();

[...]

model.addInstances(instances2);
model.setNumThreads(2);
model.setNumIterations(50);
model.estimate();
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 30
    at cc.mallet.topics.ParallelTopicModel.buildInitialTypeTopicCounts(ParallelTopicModel.java:364)
    at …
Run Code Online (Sandbox Code Playgroud)

mallet topic-modeling

5
推荐指数
1
解决办法
993
查看次数

用于大型数据集的主题建模工具(30GB)

我正在寻找一些适用于大型数据集的主题建模工具.

我目前的培训数据集是30 GB.我尝试过MALLET主题建模,但总是得到OutOfMemoryError.

如果您有任何提示,请告诉我.

lda topic-modeling

5
推荐指数
1
解决办法
2454
查看次数

如何避免解码到str:在熊猫中需要类似字节的对象错误?

这是我的代码:

data = pd.read_csv('asscsv2.csv', encoding = "ISO-8859-1", error_bad_lines=False);
data_text = data[['content']]
data_text['index'] = data_text.index
documents = data_text
Run Code Online (Sandbox Code Playgroud)

看起来像

print(documents[:2])
                                              content  index
 0  Pretty extensive background in Egyptology and ...      0
 1  Have you guys checked the back end of the Sphi...      1
Run Code Online (Sandbox Code Playgroud)

我使用gensim定义了一个预处理函数

stemmer = PorterStemmer()
def lemmatize_stemming(text):
    return stemmer.stem(WordNetLemmatizer().lemmatize(text, pos='v'))
def preprocess(text):
    result = []
    for token in gensim.utils.simple_preprocess(text):
        if token not in gensim.parsing.preprocessing.STOPWORDS and len(token) > 3:
            result.append(lemmatize_stemming(token))
    return result
Run Code Online (Sandbox Code Playgroud)

当我使用此功能时:

processed_docs = documents['content'].map(preprocess)
Run Code Online (Sandbox Code Playgroud)

它出现

TypeError: decoding to …
Run Code Online (Sandbox Code Playgroud)

python python-3.x pandas gensim topic-modeling

5
推荐指数
1
解决办法
5516
查看次数