current position:Home>Function calls and PLTs cannot be plotted

Function calls and PLTs cannot be plotted

2022-01-27 02:35:12 CSDN Q & A

……………………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(' current topic Number :{}'.format(i))            print(' Current data volume :{}'.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)

When running this code, why def choose_topic(self): Don't execute , And plt The drawing is blank

if t.perplexity_visible_model(1,6) Change it to t.perplexity_visible_model(‘topic_num’, ‘data_num’) False report :
TypeError: 'str' object cannot be interpreted as an integer

t.perplexity_visible_model() False report :
TypeError: perplexity_visible_model() missing 2 required positional arguments: 'topic_num' and 'data_num'

Please advise how to modify




Refer to the answer 1:

def choose_topic(self): Not executed because this method is not called
perplexity_visible_model This method is defined as def perplexity_visible_model(self, topic_num, data_num), look topic_num, and data_num It should be two numbers , You change to t.perplexity_visible_model(‘topic_num’, ‘data_num’), It is wrong that the parameter passed by the call is a string
The drawing is blank because there is no data ,40 OK, add it on top plt.plot(x_list,y_list)
In addition, due to for i in range(1,topic_num), If topic_num yes 1 Words ,i The range of phi is zero i>=1 and i<1 It won't enter for Cyclic




Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270235095238.html

Random recommended