1

I'm trying to build an NMF model for topic extraction. For re-training of the model, I've to pass a parameter to the nmf function, for which I need to pass the x co-ordinate from a given point that the algorithm returns, here is the code for reference:

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)

On the third last line, the tfidf.shape returns a point (3,1000) to the variable 'no_topics', however I want that variable to be set to only the x co-ordinate, i.e (3). How can I extract just the x co-ordinate from the point?

Pv66
  • 136
  • 1
  • 9

2 Answers2

1

you can select the first values with no_topics[0]

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

Kenan
  • 13,156
  • 8
  • 43
  • 50
1

You can do a slicing on your numpy array tfidf with

topics = tfidf[0,:]
Kalpit
  • 891
  • 1
  • 8
  • 24