如何使用 Python 提取点的 x 坐标

How do I extract x co-ordinate of a point using Python

我正在尝试构建用于主题提取的 NMF 模型。对于模型的重新训练,我必须将一个参数传递给 nmf 函数,为此我需要从给定的点传递 x 坐标,算法 returns,这里是参考代码:

no_features = 1000
no_topics = 9
print ('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.shape
print('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 returns 指向变量 'no_topics' 的一个点 (3,1000),但是我希望该变量仅设置为 x坐标,即(3)。 我怎样才能从点中提取 x 坐标?

您可以 select 第一个值 no_topics[0]

print('New number of topics : {}'.format(no_topics[0]))

您可以使用

对您的 numpy 数组 tfidf 进行切片
topics = tfidf[0,:]