['int'在运行pyldavis时在python中不是可迭代的错误

问题描述 投票:1回答:1
  • 我正在ipython中运行我的代码(正在运行python版本2.7)。调用“ pyLDAvis”功能以可视化LDA生成的主题时发现错误。出现以下错误:

topics_df = pd.DataFrame([用于主题中元组的[dict((y,x)x,y中的x,y))])TypeError:'int'对象不可迭代

  • 输入数据采用表的形式,该表具有两列“ complaints_ID”和“ complaints_txt”。我正在尝试在每个投诉值[]上运行主题模型

这是我要传递给此函数的Python版本或参数的问题吗?下面是我的代码。

from stop_words import get_stop_words
import pandas as pd
import numpy as np
from nltk import bigrams
from lib.lda import lda, visualizeLDA
from nltk.tokenize import RegexpTokenizer
from gensim import corpora, models
import gensim
import pyLDAvis.gensim

#provide path name here
mypath = " "
allcomplaints = pd.read_csv(mypath)
#combining complaints for each ID
myremarks= allcomplaints.groupby(['complaint_ID'])['complaint_txt'].agg(lambda x: ''.join(x)).values 
#create English stop words list
en_stop = get_stop_words('en')

#including domain specific stop words
my_stopwords = ["xx","xxxx"]
my_stopwords= [i.decode('utf-8') for i in my_stopwords]
en_stop = en_stop +my_stopwords

texts = []
for doc in myremarks:
        raw = doc.lower()
        tokens = bigrams(i for i in tokenizer.tokenize(raw)if not i in en_stop and len(i)>1)
        mergedtokens = [i[0]+" "+i[1] for i in tokens]
        stopped_tokens = [i for i in mergedtokens if not i in en_stop]
        texts.append(stopped_tokens)
dictionary = corpora.Dictionary(texts)
print dictionary
    # convert tokenized documents into a document-term matrix
corpus = [dictionary.doc2bow(text) for text in texts]

    # generate LDA model
ldamodel = gensim.models.ldamodel.LdaModel(corpus, num_topics = 5 , id2word = dictionary, passes = 1)
print(ldamodel.print_topics(num_topics=5))

#     Visualize ldamodel
vis= pyLDAvis.gensim.prepare(ldamodel,corpus,dictionary)
pyLDAvis.display(vis)

#以下是我用于运行LDA的数据示例:

Complaint_ID| Complaint_txt
------------| --------------
4545        | cust has billing issue
4545        | for $480 
6878        | connct issue for a day ne
6878        | ed immediate resoltn
python-2.7 visualization lda
1个回答
0
投票

我正在经历同一件事,你知道吗?

© www.soinside.com 2019 - 2024. All rights reserved.