……………………
class yy:
def weibo_lda(self):
#text = self.result()
dictionary = Dictionary(text)
corpus = [dictionary.doc2bow(tmp) for tmp in text]
return dictionary, corpus
def choose_topic(self):
dictionary, corpus = self.weibo_lda()
texts = text
for i in range(1,6):
print('目前的topic个数:{}'.format(i))
print('目前的数据量:{}'.format(len(texts)))
temp = 'lda_{}_{}'.format(i,len(texts))
tmp = gensim.models.ldamodel.LdaModel(corpus, num_topics=i, id2word=dictionary, passes=20)
file_path = './{}.model'.format(temp)
tmp.save(file_path)
print('------------------')
def perplexity_visible_model(self, topic_num, data_num):
# texts = self.fenci_data()
_, corpus = self.weibo_lda()
x_list = []
y_list = []
for i in range(1,topic_num):
model_name = './lda_{}_{}.model'.format(i, data_num)
try:
lda = gensim.models.ldamodel.LdaModel.load(model_name)
perplexity = lda.log_perplexity(corpus)
print(perplexity)
x_list.append(i)
y_list.append(perplexity)
except Exception as e:
print(e)
plt.xlabel('num topics')
plt.ylabel('perplexity score')
plt.legend(('perplexity_values'), loc='best')
plt.show()
if __name__=='__main__':
t = yy()
t.perplexity_visible_model(1,6)
运行这段代码时为什么def choose_topic(self):不执行,且plt画出的图是空白的
若 t.perplexity_visible_model(1,6)改为t.perplexity_visible_model(‘topic_num’, ‘data_num’)则报错:
TypeError: 'str' object cannot be interpreted as an integer
t.perplexity_visible_model()则报错:
TypeError: perplexity_visible_model() missing 2 required positional arguments: 'topic_num' and 'data_num'
请指教该如何修改