Predicting LDA topics for new data

前端 未结 1 359
时光说笑
时光说笑 2020-12-12 22:40

It looks like this question has may have been asked a few times before (here and here), but it has yet to be answered. I\'m hoping this is due to the previous ambiguity of t

相关标签:
1条回答
  • 2020-12-12 23:29

    With the help of Ben's superior document reading skills, I believe this is possible using the posterior() function.

    library(topicmodels)
    data(AssociatedPress)
    
    train <- AssociatedPress[1:100]
    test <- AssociatedPress[101:150]
    
    train.lda <- LDA(train,5)
    (train.topics <- topics(train.lda))
    #  [1] 4 5 5 1 2 3 1 2 1 2 1 3 2 3 3 2 2 5 3 4 5 3 1 2 3 1 4 4 2 5 3 2 4 5 1 5 4 3 1 3 4 3 2 1 4 2 4 3 1 2 4 3 1 1 4 4 5
    # [58] 3 5 3 3 5 3 2 3 4 4 3 4 5 1 2 3 4 3 5 5 3 1 2 5 5 3 1 4 2 3 1 3 2 5 4 5 5 1 1 1 4 4 3
    
    test.topics <- posterior(train.lda,test)
    (test.topics <- apply(test.topics$topics, 1, which.max))
    #  [1] 3 5 5 5 2 4 5 4 2 2 3 1 3 3 2 4 3 1 5 3 5 3 1 2 2 3 4 1 2 2 4 4 3 3 5 5 5 2 2 5 2 3 2 3 3 5 5 1 2 2
    
    0 讨论(0)
提交回复
热议问题