我正在尝试构建一个用于主题提取的NMF模型。为了重新训练模型,我需要向nmf函数传递一个参数,而这个参数需要从算法返回的给定点中提取x坐标。以下是参考代码:
no_features = 1000no_topics = 9print ('Old number of topics: ', no_topics)tfidf_vectorizer = TfidfVectorizer(max_df = 0.95, min_df = 2, max_features = no_features, stop_words = 'english')tfidf = tfidf_vectorizer.fit_transform(documents)tfidf_feature_names = tfidf_vectorizer.get_feature_names()no_topics = tfidf.shapeprint('New number of topics :', no_topics)# nmf = NMF(n_components = no_topics, random_state = 1, alpha = .1, l1_ratio = .5, init = 'nndsvd').fit(tfidf)
倒数第三行中,tfidf.shape返回一个点(3,1000)给变量’no_topics’,然而我希望这个变量只设置为x坐标,即(3)。我如何只提取这个点的x坐标呢?
回答:
你可以使用no_topics[0]
来选择第一个值
print('New number of topics : {}'.format(no_topics[0]))